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
Related
As per the definition in Java. If class doesn't occupy memory and just acts like a template, then why are we creating objects(main method) inside curly braces of class. Doesn't that mean now class also occupies memory because of objects present inside of it?
Trying to understand the definition of a class
There are three concepts to keep separate here: the class, the instance, and the stack.
class SomeClass {
static int staticValue = 0;
/* non-static */ int instanceValue = 0;
int someMethod() {
int stackValue = 42;
SomeClass instance = new SomeClass();
// ...
}
}
The class acts as a template, yes. In some languages other than Java, the class takes up no memory of its own: it merely describes the memory layout of the class's instances. For a beginner definition of OOP concepts you can think of that as true.
In Java this is not quite true for three reasons:
There is an object instance for SomeClass, accessible via SomeClass.class, which does take up memory. This instance allows you to look up information about the class itself, which is sometimes called "reflection".
The static field staticValue is shared among all instances of SomeClass, so in a sense the class takes up a small amount of memory to contain this shared value.
SomeClass contains methods like someMethod, and that code has to be in memory in order to run. If you're willing to consider code as requiring memory, and that the code is associated with the class, then the class consumes memory. People talking about OOP concepts aren't usually talking about the memory consumed by the code itself, though.
This can be compared to instances of the class SomeClass, which at a minimum contain a separate value of instanceValue for every instance you create. Instances don't have their own code, and do (in Java) contain a reference to their Class instance accessible via getClass().
Finally, the method someMethod and your main example do use references and local variables that consume memory, but in a different place than the instances or classes. This place is called a "stack", in part because as you call methods and those call further methods, the stack grows like a stack of papers on a desk. This means that there may be many copies of stackValue existing at once, one for each time you have called someMethod that hasn't finished yet. Each value of stackValue is discarded whenever its corresponding invocation of someMethod returns. These aren't directly tied to classes or instances, other than that they are code that might be considered associated with a class as in #3 above. Disregarding the memory consumed by the compiled code itself, the instance does not contribute to SomeClass or its instances consuming any more memory in ways that matter to OOP.
(Instances created with new are not a part of a "stack" but rather are part of the "heap", at this level of explanation, and that includes the SomeClass.class instance and any instances of SomeClass. Some languages require careful management of the heap's memory, but Java manages it for you through a process called garbage collection. Primitives like stackValue and the reference named instance are kept on the stack, though.)
For example we have an instance of MyClass and it contains 1 method. This method should be kept in memory. When this instance of MyClass is being GC'ed, is a reference to this method being deleted as well? I want to figure out weather doing everything via Dependency Injection (hence creating new instances of every class) requires less memory and more efficient or simple Helper classes with a bunch of static methods are still good.
The instance methods of an object are stored in its class object (only one copy should exist), they are not "copied" with each new instance, instead, under the hood each instance holds a reference to the method implementation residing in the class object.
Instances are garbage collected not the class data. The class data gets stored in permgen space or metaspace depending on the java version.
Garbage collectors work specifically on the heap where the instances are created not on the permgen or metaspace.
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.
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.