views:

102

answers:

4

Hi all !

I am very confused over the time at which memory is allocated to Java programs. Is it "partially done" when the class is loaded ? I have read class lifecycle to be loading-> linking-> initialization->unloading. Wouldn't some memory be consumed in these processes, even though we are NOT creating object of that class ?

I am also keen to know whether the initialization step done during classloading or object creation ?

Thanks a lot !

+1  A: 

There is memory that is used by the VM and then there is memory that is used by Java objects within the VM. Class loaders as well as Class objects take up memory, but the memory for a particular instance of the class is allocated when you construct it with a "new" expression. But yes, there is some small, fixed amount of overhead memory for being able to refer to and instantiate a particular type.

Michael Aaron Safyan
Can you please explain what does a "Class object" contain ? Dont they contain instance variables like simple objects ??
Raj
@Raj, no. Instances of a class contain member variables. A class object is an instance of type "Class" and contains metadata that describes the functions, fields, etc. of a class. (Think of it as a programmatic representation of a ".class" file).
Michael Aaron Safyan
A: 

Jvm will allocate memory when you do new unless you are calling static method in which case also it will create the class.

fastcodejava
What do you mean by "it will also create the class"? Please explain.
Raj
This answer is misleading. Calling a `static` method does not "create" a class. It doesn't necessarily even cause class initialization to occur. (It might have occurred already.)
Stephen C
The class will be loaded (by the classloader), I'm sure that's what he wanted to express with "class creation" (which has nothing to do with "Instance creation")
Andreas_D
+3  A: 

There are three things that need to happen before you can "use" an instance of a class, each one of which entails allocation of heap memory:

  1. The classes bytecodes need to be loaded and linked to resolve any static dependencies on other classes.

  2. The class needs to be initialized.

  3. An instance of the class needs to be created.

The loading and linking of classes typically happens when you start the JVM, though it can be done "lazily" by the JVM, and it can be done dynamically; e.g. using Class.forName(...). This is when memory for the classes "code" is allocated.

Class initialization is normally done immediately before the first time that the class is actually used. (The precise details are set out in the JLS). This is when memory for a classes statics will be allocated.

Class creation happens when the new construct is used. This will also trigger class initialization for a class that has not yet been initialised. This is when memory for an instance is allocated.

In addition to the above, at some point the JVM may run the JIT compiler to turn the bytecodes for a class into native code. When (and indeed if) this happens depends on the JVM version and the JVM launch options. JIT compilation will of course allocate memory to hold the classes compiled native code.

Stephen C
Thanks a lot Stephen, for such a wonderful explanation !!
Raj
A: 

I am very confused over the time at which memory is allocated to Java programs

That's because there isn't a time. Memory allocation and deallocation happens continuously throughout the life of a Java program.

EJP