0

Suppose we are creating an ArrayList Object like

ArrayList<User> list=new ArrayList<User>();
System.out.println(list.size());   // it will be 0 at this time

Now my question is, what memory it will allocate when this list object will be created in the heap. I want to know how this memory size is allocated at run time if the size of the list grows dynamically.

11
  • The initial capacity is 10 for an ArrayList. 10*(memory size of reference to User object) + (overhead) is probably the initial memory allocated Commented May 28, 2014 at 16:42
  • @CyberneticTwerkGuruOrc Technically, that'd be 10 * (memory size of a reference) + (overhead) Commented May 28, 2014 at 16:44
  • Ok @CyberneticTwerkGuruOrc , but can you tell me how this works ? i mean how this will be chooses at runtime ?? Commented May 28, 2014 at 16:44
  • @user3580294 Ahh, yes you're right. Let me edit that Commented May 28, 2014 at 16:44
  • 2
    Your question is confusing (unclear). The size() method does not have anything to do the actual memory allocated by the JVM for your list. You should probably do some reading first docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html, and then come back with some clear question. Commented May 28, 2014 at 16:49

3 Answers 3

4
ArrayList<User> list=new ArrayList<User>();

will create an empty ArrayList. No objects of User are created or added to the ArrayList at this point, and will never be until you explicitly add to it.

size() at this point will return 0 as there are no elements in the ArrayList.

An ArrayList has a size and has a capacity. An ArrayList is backed by an array. The capacity is the size of the array at a point.

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

When an ArrayList is created via the default constructor, an ArrayList is created with capacity 10.

So technically, the actual memory occupied by the list will be based on the capacity. But note that the array/list is empty. So increass in capacity will have only a marginal impact on the overall size occupied.

You can use a tool like Java VisualVM (which is included with the JDK) to check the sizes occupied by objects.

Sign up to request clarification or add additional context in comments.

Comments

1

Part 1: What memory is allocated when the list object is created:

When an ArrayList is created with no default size, the following constructor is called:

/**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
    super();
    this.elementData = EMPTY_ELEMENTDATA;
}

super() here calls an AbstractList constructor which does nothing. this.elementData is set to an empty array of objects. Therefore, the memory allocated will be the sum of sizes of the different attributes of the ArrayList class + the 16 bytes allocated to the class itself:

public class ArrayList<E> extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;

/**
 * Default initial capacity.
 */
private static final int DEFAULT_CAPACITY = 10;

/**
 * Shared empty array instance used for empty instances.
 */
private static final Object[] EMPTY_ELEMENTDATA = {};

/**
 * The array buffer into which the elements of the ArrayList are stored.
 * The capacity of the ArrayList is the length of this array buffer. Any
 * empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
 * DEFAULT_CAPACITY when the first element is added.
 */
transient Object[] elementData; // non-private to simplify nested class access

/**
 * The size of the ArrayList (the number of elements it contains).
 *
 * @serial
 */
private int size;

 ....

You can checkout the default sizes of primitive data types and do the math to know the exact memory that is used.

But, no memory is used to store any data since an empty elementData array is initialized.

Part 2: how this memory size is allocated at run time if the size of the list grows dynamically.

Firstly we see that the ArrayList uses an array elementData to keep objects. There is a method that has amortized constant time that I can't exactly remember it's name.

But the idea is that when the array that the ArrayList class uses is filled, the size of the array is doubled. When the ArrayList elements are deleted and only 1/4 of the array is filled, the size of the array is halved.

Comments

1

To answer the 2nd part of your question, yes the allocated memory grows dynamically as you add elements to the ArrayList. You can estimate the size of an empty ArrayList by looking at the source code. ArrayList itself just declares three fields:

private static final long serialVersionUID = 8683452581122892189L;
private transient Object[] elementData;
private int size;

which for an empty list will consume 20 bytes in a 64-bit jvm. ArrayList extends AbstractList which (including its private classes) declares:

int cursor = 0;
int lastRet = -1;
int expectedModCount = modCount;
protected transient int modCount = 0;
private int offset;
private int size;
private int expectedModCount;

which adds 28 more bytes, so 48 bytes total.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.