When is memory allocated during instance creating or using new keyword for object creation?

As I read somewhere, memory is allocated when instances are created and a space is allocated from the heap. If it is right than when and how much exactly memory is allocated during instances and objects creation?

5,148 3 3 gold badges 22 22 silver badges 35 35 bronze badges asked Jul 31, 2014 at 9:28 11 1 1 gold badge 1 1 silver badge 2 2 bronze badges

As you wrote twice: Memory is allocated, when instances are created. The exact size usually doesn't matter.

Commented Jul 31, 2014 at 9:31 Allocated memory size differs from object to object Commented Jul 31, 2014 at 9:31 Instances are created using new Keyword Commented Jul 31, 2014 at 9:32

5 Answers 5

Variables declared within a method are stored within the stack, while the actual objects are stored on the heap. Consider

Integer a = new Integer(10); 

In this example, an object of type Integer is created on the heap and a reference (either 32 or 64bits) is returned and stored within the methods stack as variable 'a'. The JVM is free to keep variables like this within the CPU registers if it prefers as an optimization.

Memory of an object is allocated when the new keyword is used. It is usually assigned within the TLAB (thread local allocation buffer) which is part of the eden heap reserved to the running thread. Thus reducing the overheads of object allocation to a simple 'bump of a pointer'. The two times when the TLAB is not used, is 1) when the object is too large for the space remaining, inwhich case it will be promoted straight to the old gen and 2) when a supporting JVM decides via escape analysis that it can avoid the object entirely and allocate directly on to the stack (or even break the object apart and only assign the fields required on the stack).

The amount of memory reserved consists of an object header, usually 2 words (3 for an array) and then space for each of the fields declared in the object and its parent classes. The total size of those fields depends on the JVM and the underlying platform (eg 32 bit or 64 bit) and JVM configuration such as compressed references.

------------------+------------------+------------------ +-------------------------- | mark word | klass pointer | array size (opt) | padding and fields | ------------------+------------------+-------------------+-------------------------- 

Asking the JVM for sizes is not officially supported, but EHCache sizeOf is a very good 'best guess' that uses knowledge of different JVMs and access to the underlying pointers via the use of Java Unsafe.

The starting place for understanding the size of each field is the size of primitives defined by the Java language, however this is only the minimum sizes as the JVM is designed to work with 32bits and so primitives smaller than this are often padded out to 32 bits. For example booleans.

The exact layout of the fields will vary by JVM, but they will tend to be grouped by the class that defines them starting from the root of the inheritence tree. For example, consider

enter image description here

enter image description here

The pictures above were taken from this very good blog post that describes the memory layout very well,