First things first: I am assuming your questions are coming out after reading this article (because over there I see a very similar diagram as yours) so I will not quote or highlight any of the points which are mentioned over there and will try to answer to your questions with points which were not so obvious in that post.
Reading to all your questions, my impression is that you are clear about how memory is allocated in stack and heap but have doubts about metadata of the classes i.e. where in the memory, methods of the classes would be stored and how they would be recycled. So, first let me try to explain JVM memory areas:
JVM Memory Areas
Let me start by putting these 2 diagrams depicting JVM memory areas:
Source of diagram
Source of diagram
Now, as clear from the above diagrams below is the tree structure of JVM memory and I will try to throw light on the same (@Adit: please note that area which concerns you is PermGen Space or permanent generation space of non-heap memory).
- Heap memory
- Young generation
- Eden Space
- Survivor Space
- Old generation
- NonHeap memory
- Permanent Generation
- Code Cache (I think included "only" by HotSpot Java VM)
Heap memory
Heap memory is the runtime data area from which the Java VM allocates memory for all class instances and arrays. The heap may be of a fixed or variable size. The garbage collector is an automatic memory management system that reclaims heap memory for objects.
Young generation
Young generation is the place where all the new objects are created. When young generation is filled, garbage collection is performed. This garbage collection is called Minor GC. Young Generation is divided into below 2 parts
Eden space: The pool from which memory is initially allocated for most objects.
Survivor space: The pool containing objects that have survived the garbage collection of the Eden space.
Old generation
Old Generation memory contains the objects that are long lived and survived after many rounds of Minor GC. Usually garbage collection is performed in Old Generation memory when itβs full. Old Generation Garbage Collection is called Major GC and usually takes longer time. Old generation contains below part:
Tenured space: The pool containing objects that have existed for some time in the survivor space.
Non-Heap memory
Non-heap memory includes a method area shared among all threads and memory required for the internal processing or optimization for the Java VM. It stores per-class structures such as a runtime constant pool, field and method data, and the code for methods and constructors. The method area is logically part of the heap but, depending on the implementation, a Java VM may not garbage collect or compact it. Like the heap memory, the method area may be of a fixed or variable size. The memory for the method area does not need to be contiguous.
Permanent generation
The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.
Code cache
The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.
Answering OP's questions specifically
Where are the methods of s stored?
Non-Heap memory --> Permanent Generation
Had I created another object of MemoryClass inside myMethod, would JVM
allocate memory for the same methods again inside the stack memory?
Stack memory only contains local variables so your ORV (object reference variable) of new MemoryClass
would still be created in stack frame of myMethod
, but JVM would not load all the methods, metadata etc. of MemoryClass
again in "Permanent Generation".
JVM loads class only once and when it loads the class then space is allocated on "Permanent Generation" for that class and that happens only once while the class is loaded by JVM.
Would JVM free the memory allocated to myMethod as soon as it's
execution is completed, if so, how would it manage the situation
mentioned in question 2(only applicable if JVM allocates memory
multiple times to the same method).
Stack frame created for myMethod
will be removed from stack memory, so all the memory created for local variables will be cleaned but this doesn't mean that JVM will clean up the memory allocated in "Permanent Generation" for the class those object you have created in myMethod
What would have been the case, if I had only declared s and did not
initialize it, would JVM still allocate memory to all the methods of
java.lang.String class, if so, why?
Specifically talking about String
class, JVM would have allocated space for String
in "Permanent Generation" way too early, while JVM is launched and whether you initialize your String variable or not, it doesn't matter from "Permanent Generation" perspective.
Talking about other user-defined classes, JVM would load the class and allocate memory in "Permanent Generation" as soon as you define the class, again even if you do not create an object of the class, memory is allocated in "Permanent Generation" (non-heap area) and when you create an object of the class then memory is allocated in "Eden Space" (heap area).
Sources of above information and further reading: