I'm wondering how the memory allocation works in Java.
I have a class Duck with two instance variables int size and String name. These variables are initialised. Does the memory for these variables get allocated on the heap during run time, if I'm not instantiating this class?
Thanks,
Gene
Several possible scenarios:
If you are not using a class, then
the class itself is not loaded in a
class loader at all.
If you are using a class, but not
instantiating it, then instance
variables are not occupying memory since there is no instance to begin with.
If you are using a class, and using
an object which is instance of that
class, then instance variables are
using up memory for each instance,
regardless of whether you use these
values or not.
IF you are not referencing the class Duck at all the class isn't even loaded via classloader so the answer is no.
Related
In Java If we instantiate an object then it will be located at Heap memory But what happens if we instantiate a singleton class object then it object locates where ???(Heap or class area as it static reference)
Instances that aren't purely local to methods are created and stored in the heap, even if they're only referenced from static fields.
(If the instance is local to a method and doesn't survive method termination, the JVM may, as an optimization, allocate it on the stack. Which isn't strictly applicable to your question, just explaining the caveat in the initial sentence above.)
All created instances will be stored in the heap. so even though it is a singleton class, the only instance will be created in the heap. but that reference will be kept by a static filed. because the static filed is a part of class definition (not the instance), that reference will be stored along with the class meta data which is stored in the PermGen which is actually a part of the heap
for every class, what things get loaded into memory with that class when we instantiate it with new keyword? as we know, every class is subclass of Object class, at the time of instantiating any class by new keyword , Object class data also gets loaded into ram. I read that, all classes of java.lang and java.util also gets loaded, whenever we instatiate any class by new keyword. I want to know that other than Object class data, all classes of java.lang and java.util which things gets loaded into memory?
The runtime memory is divided into the Heap and the Stack. The Heap stores all the objects that are instantiated during runtime and the Stack stores all the methods that are invoked during runtime. The created Object contains all the instance variables inside itself and all the Class's methods and goes to the Heap.
However, all the local variables, the variables that were created inside an invoked method all go to the Stack.
I come from operating systems and background of C where the world is simple when code is compiled. Need to deal and understand stack, heap text section etc.
When I started learning Java(I do know about JVM and garbage collector), I got amused by static methods. As per my understanding all the instances of a class do get created in the heap and then do get cleaned. However, for a static method, you don't need an instance of the class.
So, can some one please explain how non static methods and static methods differ in memory model. Do both of them reside in the text section of the memory. Or I am messing up things completely.
Thanks
In Java, the bytecode for the classes (and that includes their methods, both static and instance) is part of the heap (usually in a special "permanent generation" section for long-lived objects).
Classes can be garbage-collected, too, but this usually does not happen much (only when the class was loaded from a non-system classloader and that whole classloader becomes obsolete, for example when a web application is unloaded).
However, for a static method, you don't need an instance of the class.
Right. But all methods are part of the class definition and loaded together when the class is loaded. Even if you never make an instance of a class, the code for all instance methods will be loaded into heap memory.
And then there is JIT compilation to native code: With Hotspot, the bytecode for frequently used methods is compiled further into native machine code. The result of that does go somewhere outside of the heap into native memory, and that only happens for methods that are actually being used (static or not).
Your understanding that all instances of a class get created in the heap...
Not exactly correct, all classes get compiled into object byte code, otherwise the JVM has nothing to execute. Instance and static methods all generate the same object byte code. Even non-static classes only generate a single version of their object byte code. All instance classes have their own pointer to where their execution is in that single copy of byte code. The real difference is in the data members of the class. Each instance of a non-static class has to have its own copy of all the non-static data members (variables), but static data members only have a single copy in memory since static data members of a class are shared by all instances of that class static or non-static.
A static class or the static data members of a non-static class all have a single copy of themselves in memory.
A non-static class still has just a single copy of its object code in memory, it is only the non-static data that gets a copy in memory for each instance.
I am having a simple query.Basically its a theoritical question.I need to clear this idea.I have not found any suitable answer to it.The question is very simple.Suppose we have a class named as A
class A{
......
}
Now from my main function i am creating one object of A.
A obj = new A();
now after creating the object i will be able to access any methods,variables present in the class.
But for static methods or variables we can achieve this directly by using class name like
A.methodname();
now my question is if we are creating one object of any class then memory will be allocated for this.Now if we are using static methods are directly calling them via class name then we are not creating any objects,so in that case memory should not be allocated.But without allocating the memory how does we access the methods or any variable name?Using reference also require some memory allocation.so please explain how in this case memory allocation is happening or how we are accessing the methods or variables in the class.
Now if we are using static methods are directly calling them via class name then we are not creating any objects,so in that case memory should not be allocated.
Actually, you do. It is JVM dependent but the HotSpot JVM creates a special object for all static fields. You can see this object in a heap dump.
Making this an object makes it easier for the GC to trace which objects are being used. This class object is discarded when the ClassLoader is unloaded.
This is done by class loading.
We do not have any instance of object, but we have a class code loaded into memory so in fact there is memory allocation.
Static functions can only access static variables, and the memory for that will already have been allocated.
Static members (methods and variables) are allocated by the JVM when the class is loaded.
without allocating the memory how does we access the methods or any variable name?
You cant access an instance method or variable from an static method. It wont compile if you try.
From an static method, you can only access other static methods or static variables, (wich are allocated at class load time).
All the static variables of a class are loaded into memory at the time of class loading where as the instance variables are allocated with memory everytime you create a new object of that class.
However, the static variable values are not specific to a instance of an object but there are shared by all the objects of a class where as the instance variable values are specific to an instance(class object).
That is the reason you can access the static variable using class name as they are already loaded into the memory where as the instance variables and methods need object creating to access them.
Cheers!
For static during a class loading a memory allocation is done in permanent generation.Its initialization occurs only once .It is related to class not object.
If I have an object O with a gigantic method f(), and I load 10000 examples of O into memory. Are 10000 examples of f() loaded into memory as well? If so, does that mean that I would save memory by making this function static if possible?
Instance Methods are loaded in to Method Area in JVM. it is loaded once , but there will be many stack for every call u make to f() , to keep track of there own local variable values.
No. There is only one instance of the method loaded.
Instance method is just a template and is defined in a class (not in every instance). You wouldn't save memory by making it static.
No. Methods are not part of instances; they're part of classes. There would be no point in repeating the code for each instance (because it would never vary) so the implementation is, quite simply, smarter than that.