Java has only two types of memory when it comes to JVM. Heap memory and Non-heap memory. All the other memory jargons you hear are logical part of either of these two.
Heap Memory
Class instances and arrays are stored in heap memory. Heap memory is also called as shared memory. As this is the place where multiple threads will share the same data.
Non-heap Memory
It comprises of ‘Method Area’ and other memory required for internal processing. So here the major player is ‘Method Area’.
Method Area
As given in the last line, method area is part of non-heap memory. It stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields.
The above three (heap memory, non-heap memory and method area) are the main jargon when it comes to memory and JVM. There are some other technical jargon you might have heard and I will summarize them below.
Memory Pool
Memory pools are created by JVM memory managers during runtime. Memory pool may belong to either heap or non-heap memory.
Runtime Constant Pool
A run time constant pool is a per-class or per-interface run time representation of the constant_pool table in a class file. Each runtime constant pool is allocated from the Java virtual machine’s method area.
Java Stacks or Frames
Java stacks are created private to a thread. Every thread will have a program counter (PC) and a java stack. PC will use the java stack to store the intermediate values, dynamic linking, return values for methods and dispatch exceptions. This is used in the place of registers.
Memory Generations
HotSpot VM’s garbage collector uses generational garbage collection. It separates the JVM’s memory into and they are called young generation and old generation.
Young Generation
Young generation memory consists of two parts, Eden space and survivor space. Shortlived objects will be available in Eden space. Every object starts its life from Eden space. When GC happens, if an object is still alive and it will be moved to survivor space and other dereferenced objects will be removed.
Old Generation – Tenured and PermGen
Old generation memory has two parts, tenured generation and permanent generation (PermGen). PermGen is a popular term. We used to error like PermGen space not sufficient.
GC moves live objects from survivor space to tenured generation. The permanent generation contains meta data of the virtual machine, class and method objects.
Discussion:
Java specification doesn’t give hard and fast rules about the design of JVM with respect to memory. So it is completely left to the JVM implementers. The types of memory and which kind of variable / objects and where they will be stored is specific to the JVM implementation.
Key Takeaways
- Local Variables are stored in Frames during runtime.
- Static Variables are stored in Method Area.
- Arrays are stored in heap memory.
References:
- MemoryPoolMXBean provides you api to explore the memory usage, threshold notifications, peak memory usage and memory usage monitoring.
- Java Docs API for JConsole
- Threads and Locks chapter of Java Language Specification talks lot about java memory
- Chapter 5 of Inside the Java Virtual Machine, The Java Virtual Machine by Bill Venners













Very interesting catch there , that maybe gives you a first “dive” into the monstrous JVM !
Keep up the good work :)
Very interesting!!
Sir,
I have come across so many different tutorial/articles about java memory.
Your style is unique and learned few new keywords which i did not learn it in others.
thanks
rams
Hi Joe,
This is really intresting and good startup tutorial/info for those for whom, knowing about JVM is not part of day-to-day activity.
Good going Joe..All the best.
Thanks,
Srikanth S
Hey Rams, thanks. Nice to see you commenting.
@Srikanth – thanks Srikanth for following.
Hey Joe,
Great stuff, i want to know more about JVM in depth, can you please refer me a book on it or link where i can find more about it.
Thanks again for sharing it helps a lot to know more about java in depth.
The Java Virtual Machine Specification
http://java.sun.com/docs/books/jvms/
is the first choice and next,
Inside the Java Virtual Machine, by Bill Venners
Great article!
I recommend our book Oracle JRockit – the definitive guide for an in depth JVM-agnostic (yes, really) overview of JVM memory management.
http://amzn.to/9Gpdb6
Dear joe,
before I read your topic,I was known that when I create any object.it’s stored in heap and after I removed it, it will stored in stack.
but after, reading I understood that any object I will create during run time stored an removed in stack .
please comment explain to me this part.
and I have another question what do you mean with”Arrays are stored in heap memory”?
are you meant arrays as a local variable.
sorry for along comment.
Thanks,
Static variables doesn’t belongs to methods, but how come they will be stored in Method Area.
@Lagergren – thanks for the comment. I haven’t read your book yet. Sure I will read it.
@Raghu – Name ‘method area’ doesn’t mean that only method information is stored there. Even, the class level information is also stored in java ‘method area’.
Its just a name and sure sure it is misleading.
Great tutorial!
As I always told that you blog rocks :)
Have a great time.
Pingback: Java Array
Fab one. learnt many new thngs.
Good precise tutorial
Great article and simple to follow.Thanks !! Keep up the good work
String constant pool.
Before going to this SCP(String Constant Pool) lemme say difference between
1) String s= new String (“hello”);
and
2) String s=”hello”;
In the first case two obects will be created. one in heap and the other in SCP.( s will always refer to heap object but not to SCP)
In the second case only one object will be created in SCP and s will always refer to the object in SCP.
Object creation in SCP is always optional, first JVM checks if there is any obect present with the required content , if exists then will use the same else create new object. i.e SCP do not allow duplicate objects.
GC(garbage collector) is not allowed to enter SCP. But when ever JVM shutdown, automatically SCP area gets flushes out.
The main adv of this approach is memory utilaization will be improved hence the perforamnce improves.(Coz object creation is lil costly in java.)
@ chandana : 1 thin i din’t understood in ua explanation i.e..What’s the use of creating 2 objects in the 1st case?
later u explained that “Object creation in SCP is always optional, first JVM checks if there is any obect present with the required content , if exists then will use the same else create new object”. according to ua statement 1 thin i concluded , object which is in heap is neva used!!..den wt’s the use of object in heap??
hey man …. thnks
great ways of explanation… keep the good work goining…
god bless u.
Really Nice Explanation And website design
very nice article. thanks.
it’s very helpfull
Very clear and a very good start up to explore more. Thanks for providing such nice notes.
I had never seen this type of blog..
It’s fantastic.
Job well Done…
Excellent explanation!!
Waiting to see more on frameworks like Spring, Hibernate Struts etc
Perfect for a starter..
Awesome Joe ,
It gives me lot of informations about JVM memory.
Thanks for your valuable time to provide those things.
God bless all the very best for your future.
By
Jerald
really interesting….JVM is whole power of java..
I also wrote about JVM basic…give your feed back
http://searchcrone.com/2011/06/java-virtual-machine
Can you please post some article on garbage collections. Not able to get one single tutorial , which provides complete insite.
The way you explained the memory concepts in Java are superb!! Thanks for that. I am an intermediate level Java programmer and would like to understand more about “how does Hashmap get() and put() work”. Whenever you get time, please explain that with examples.
Thanks,
Jyothi
Great work joe ….
Hi Joe!
As far as i have searched many websites to know about JAVA memory Types, but u fullfilled ma needs. thank u joe!. and yo way of Explanation s gud yo understand as a beginer.
And could u plz post me a sample program with source codes joe. it ll b more helpful to me understand java programs easily….
One more information: Not all static field information are stored in Method Area. Only numeric constants and String-constants are in Method Area. As the static objects live in the Heap they are treated like “normal” objects.
Really gud article to understand the jvm memory architeure .
Article on JVM memory is marvelous and SCP concept explained by chandana is superb.
where the local objects are created in jvm
very inteligent thinking..
very inteligent tutorial..
Hi,
You are very Intelligent.
Very Nice Explanation……
Your Web site also very good …. :-)
hey great blog.. content looks neat and very useful.. you can also visit the below site for more queries in java…
http://www.exceptionhandle.com/portal/category/java
I don’t know how can I thank for such a rare site you have created. Awesome topics, awesome explanation.
May God bless you to put many more topics on your site.
Thanking u,
-Santosh
4. The Rectangle class Design a class named Rectangle tp represent a rectangle. The class cointains:
• Two double data field named width and height taht specify the width and the height of the rectangle. The dafault values are 1 for both width and height.
• A string data field named color taht specified the color of a rectangle. Hypothetically, assume that all rectangles have the same color. The dafault color is white.
• A no-arg constructor that creates a default rectangle.
• A constructor that creates a rectangle with the specified width and height.
• The access and mutator methods for all three data fields.
• A method named getArea() that returns the area of this rectangle.
• A method named getPerimeter() that returns the perimeter.
HI Joe,
package core;
public class MemoryTesting {
int int1 =0;
transient int transient1 =0;
volatile int volatile1=0;
static int static1=0;
boolean bollean1=false;
final int final1 = 1;
public static void main(String[] args) {
MemoryTesting ref1 = new MemoryTesting();
ref1.LocalMethod(10);
}
public void LocalMethod(int param){
int l1 =0;
final int local_final =0;
MemoryTest ref2 = new MemoryTest();
}
}
class MemoryTest{
int int2 =0;
transient int transient2 =0;
volatile int volatile2=0;
static int static2=0;
boolean bollean2=false;
final int final2 = 2;
}
Requesting you specify memory of Static1,static2,
volatile1,volatile2 as they are shared. also tell
memory final1, final2, local_final.
Thanks !
Thanks for sharing such kind of wonderful information about java. Your blog is very informative. Please keep such beautiful postings.
Dear Joe,
Really Liked your Site…You have written on some really less known but important monstrous topics..Kudos buddy…
One Request, If You can index all the topic sin First page, That would be so great as iterating all pages will take time.
Thanks,
Amit
Hi Jeo,
Thank you very much ,I have gone through so many website , no sites will give real examples and pictures. And all the examples are real time , for example you posted the ice cream for the decorator pattern is so good.
Please keep posting the good articles.
And also please let me know do you have any group in linked in so that I can be in touch.
Thanks
Selvi
Hi Joe,
Thank you very much ,I have gone through so many website , no sites will give real examples and pictures. And all the examples are real time , for example you posted the ice cream for the decorator pattern is so good.
Please keep posting the good articles.
And also please let me know do you have any group in linked in so that I can be in touch.
Thanks
Selvi
Thank you! It really summarize well the basics, now after understanding I can move forward.
awesome one..
awesome one..
your explanation is so good. please provide some more information regarding abstract classes.
Hi Joe,
Quick question regarding younggen and permgen. Are these also part of non-heap memory? Also where are the thread pc and stacks maintaned?
Thanks a lot for an easy to read article.
Hi Joe,
Thank you very much.
ya it’s very useful and helpful content
Hi Dear,
This is simple and intreating tutorial with simple information. I like your blog. Keep Running.
HELLO Sir
i jst wanted to know why objects in java are stored in heap not in stacks.
If you could explain me where static variables, instance variables, Meta data, String pool can store. That would be great.
superb jhon sir you are doing a wonderful contribution of java…
I am my JVM Max heap size = 2GB and Max Perm Gen = 0.5GB. When i monitor the Solaris servers it reports the JVM using 4.5GB. What is being used by the additional 2GB is this the Method Area?
Good work done man
how to set permgen space for weblogic.please any one reply me
Good but need to be elaborated some more
Gud wrk
Explain the execution of a Java program with respect to the internals of JVM and the steps it takes.
When memory to static variables or methods or class are available and where?
In the The Java™
Language Specification
Third Edition, I found a keyword ‘strictfp’. I was never aware of this before. I want to know when did this keyword come?
really interesting….
Dear Joe,
Can you help write and share a small java code demonstrating when the object is stored in which memory and its life cycle line by line of the java code.
It is very good example.
Thanks for the blog. I’ve been reading “The Structure of the Java Virtual Machine” for Java 7 and page 13 states that: “Although the method area is logically part of the heap, …”. This conflicts with your idea that the method area belongs to the non-heap of the memory, right? Could you clear this?
Regards.
sir,
where the default values will get stored?where the default values are generated by JVM when the variables are not get initialized.
Hi..
How can we increase/decrease the size of jvm
@dhananjay,
Initial java heap size can be modified using parameter -Xms
Maximum java heap size can be modified using parameter -Xmx
how should i know the memory allocation for an object in heap
Hello,
The statement is “static variables/context can not see non static context/members directly”
So please explain me “how do we call constructor of current class directly from psv main( ){}
Class Animal{
public int x=10
psv main(String[] a){
int x=13; // not allowed
Animal an= new Animal();
//call to constructor
}
}
where does( memory area ) the jvm find and in which scope?
I am confused regarding permanent generation memory space in Java.
I want to know if it is a part of heap or not? Some links say it is a part of heap and some says it is not.
Please clarify.
Hi,
I browsed many articles in the internet, but i find a clear explanation in your blog. i would refer your blog first when i try to search for anything in JAVA…
Hi ,
I have query- What is need java to maintains two different types of memory-stack and Heap?
Why Primitive data types and Objects are not stored in same memory?
this is very good blog site to read more
this is good blogsite to read more
Very Nice and i got clear knowledge…
Thanks
If I have an interface with 5 constants. If there are 5 classes which implement the interface. How many constants are kept in the RAM…???
Working in a small company without any training on java. Its been 6 months. My question is.., what is the actual use of an Interface ? I have searched many sites but didn’t satisfy my “Million Dollar Doubt”. Seriously and honestly. I will be greatly thankful to you.
Good to see the unique technique to explain the JVM Memory Architecture
Hello Sir,
It is very useful tutorial. Expecting more and more in other topics too going forward.
Once again Thanks for providing a wonderful info.
Thanks & Regards,
Sridhar Goranti
USA
nice explaination
Pingback: Java Weak Reference
It is good & helpful. The description is short & simple.Easily understandable. Good Job Joe!
good work! thanks Joe. keep moving!
good job
This is a nice article however the current Java Virtual Machine Specification contradicts what you said. You stated that the Method Area is not part of the Heap. However, the specification states “Although the method is logically part of the heap, simple implementations may choose not to either garbage collect or compact it.”
AWESOME!!!!
i want information is heap generation. it is architecture. it is divided into five parts. these part is where class object will be stored. pls i want this answer.