Holmes
Holmes

Reputation: 11

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?

Upvotes: 1

Views: 10874

Answers (5)

Kanke
Kanke

Reputation: 2747

Class Variables

When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables. In the case of the Bicycle class, the instance variables are cadence, gear, and speed. Each Bicycle object has its own values for these variables, stored in different memory locations.

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Upvotes: 0

Braj
Braj

Reputation: 46841

As I read somewhere that memory is allocated when instances are created and a space is allocated from the heap.

yes you are right, until and unless new is called it's just a null reference that is pointing to nothing.

If it is right than when and how much exactly memory is allocated when instances and objects are created?

It depends on the size of the object.

Have a look at Primitive Data Types to know about their size.

In Java, what is the best way to determine the size of an object?

Read more...

Upvotes: 1

Chris K
Chris K

Reputation: 11907

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

and

enter image description here

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

Upvotes: 8

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29213

I'm reading your question as: "Who actually allocates memory for an object - the new keyword or the constructor?" If that's the case, the answer is the new keyword.

Constructors are typically chained, meaning that at least two constructors will run when you're creating an instance. On the other hand, memory for an instance is allocated only once.

Also, the type of allocation is determined using analysis of the usage of the produced reference (escape analysis, for example). This means that the most obvious place for the allocation to happen is at the constructor call site (that is, the place of the new expression).

The size of the memory allocated is such that it can accomodate an instance of the type following the new keyword. That latter size (the size of an instance of a type) is

  • the aggregate size of the primitive types (int, float, double, etc) it consists of,
  • plus the aggregate size of the references to the reference types (class or interface instances) it consists of,
  • plus "secret" stuff embedded in instances so that Java features can work (like a virtual table pointer, to allow fast runtime resolution of virtual method calls)
  • plus possible padding between each of those (to align the various types to their optimal address number multiples in memory).

In any case, when you do T obj1 = new T(), where T is the name of a class:

  1. Memory is allocated somewhere to accomodate a T instance.
  2. The instance is constructed.
  3. If the instance is successfully constructed, a reference to the instance is returned.
  4. That reference is stored in the obj1 variable.

When you do R obj2 = new R(), a similar thing happens for the type R, and type R may have a different size than T.

But neither of these local variables contains the instance. They both contain a reference to their assigned object. (Thus, the variables themselves might even be of the same size, if all they have to do is store a memory address.)

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200138

"create instance" has the same meaning as "use new to create new object".

In a normal case, heap memory will be allocated at the time you ask for a new object, but that is not set in stone: HotSpot can also determine that it is safe to allocate the object on the call stack (by the process of Escape Analysis). This is more efficient as it doesn't need any garbage collection.

How much memory is allocated is highly implementation-specific, only Java arrays are guaranteed to be "packed" (modulo a fixed overhead). Boolean arrays, though, are specified as occupying a byte per element.

Upvotes: 2

Related Questions