JVM memory area related jargons are key to understand the JVM on the whole. In this article let us discuss about the important memory areas in JVM.
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.
It comprises of ‘Method Area’ and other memory required for internal processing. So here the major player is ‘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.
Memory pools are created by JVM memory managers during runtime. Memory pool may belong to either heap or non-heap memory.
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 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.
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 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 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.
Java specification doesn’t give hard and fast rules about the design of JVM heap data area. So it is left to the JVM implementers and they can decide on things like whether to allocate fixed memory size or dynamic.
Comments are closed for "Java (JVM) Memory Types".
Very interesting catch there , that maybe gives you a first “dive” into the monstrous JVM !
Keep up the good work :)
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
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.
[…] In java arrays are objects. All methods of an Object can be invoked on an array. Arrays are stored in heap memory. […]
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.)
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
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
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….
Great work joe ….
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 …. :-)
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 good blogsite to read more
Very interesting!!
@ 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??
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
[…] to garbage collection. In java we need to not anything explicitly for garbage collection (GC), this memory management overhead is taken care by java run-time […]
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.
[…] you remember the memory management in java, you can recall that the local variables are stored in a stack. These java stacks are created when […]
very intrusting
Hi Ram, I was thinking that if I am creating object
ClassA hm= new ClassA();
sysout(hm);
hm=null;
hm= new ClassA();
sysout(hm);
–>[package].[Class]@7919298d
[package].[Class]@62f72617
…So how compiler understand this object value(@7919298d or @62f72617) and is it just unique code that the point to consider or
how compiler make this arrangement in memory. And since this is object creation by “new” keyword,how gc will come into picture.. Bit confuse. Your support much appreciated !! Thanks
very nice article. Explained clearly and straight forward
very nice article. Explained clearly and straight forward
Have you ever considered writing an e-book or guest authoring on other blogs?
I have a blog based on the same ideas you discuss and would
really like to have you share some stories/information.
I know my subscribers would appreciate your work.
If you are even remotely interested, feel free to shoot me an e-mail.
In which memory area stores the Instance variable as well as local variables..?? I’m Confusing this please anybody can clarify it..!!
Dear sir
i want to know that any software available that we can see that our variable store in which memory and which address? i want know that
for ex: if i have addition of two variable so how can i see that which address and which memory store which variable? and at which address?
so can u plz plz plz plz suggest me some of software that i can see how memory work on my program..
Thanks
Hello EMAHELSA,
I have just read this blog about JVM memory management. I try to answer your questions:
1)Any class inntances/objects are stored in Heap memory only. If it is no longer in use when GC happens then it will move from Eden space to Survivor space as per Young generation.
2) Since Arrays are objects in Java and not a variable so it will be stored in Heap memory only.
Please correct me if I am wrong :)
Hello EMAHELSA,
I have just read this blog about JVM memory management. I try to answer your questions:
1)Any class inntances/objects are stored in Heap memory only. If it is no longer in use when GC happens then it will move from Eden space to Survivor space as per Young generation.
2) Since Arrays are objects in Java and not a variable so it will be stored in Heap memory only.
Please correct me if I am wrong :)
Hi Raghu,
Sice Method(Class) Area stores Per-class structures, code for methods and constructors.
Per-class structure means Runtime Constant pools and static fields. So all the static variables will be stored in Class 0r Method Area obviously. :)
I would like to get following doubts clarified.
As per Oracle site http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html, the method area is created on virtual machine start-up. Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it.
So my doubt is whether the generational gc algoithm which is used by hotspot jvm collect garbage on method area? Please note that the runtime constant pool is part of method area.
And whether the young (Eden and survivor) and old (tenured and permgen) are part part of heap only?
What is the difference between tenured and permgen?
Or permgen is method area?
Hi loe
Am learning a lot .
Thanks
wonderful tutorial
Your writing style has some magic in it. I don’t think there is a better/simpler way to explain than how you do. Just stumbled on your site through a Google search, but ended up spending a couple of hours reading other stuff you posted. Great, keep blogging.
GC moves live objects from survivor space to tenured generation.When?
I want to know interface memory alocated or not?
I don’t understand why you placed brain’s pic. I guessed you might have related the functioning of brain with the computers. Looks kiddish.
good description sir
Hi All,
Can anyone reply what to answer if any interviewer asks How many memory areas are there in jvm???
Please do reply ASAP.
Thanks,
Rahul
Hi Joe,
It is good tutorial to understand the Java Memory Types. Now i can know about the memory types and what is stored in that.I get OutOfMemory Error often. Is it possible for you to explain what is the reason of getting this issue?
Thanks,
hi , i really like your very post. i am regular reader of your post . I want to strong part in java collection and thread. can you suggest be few good pdf or book. i want to make them strong . Deep concepts of java. please suggest
[…] the garbage collector comes into picture. In an earlier article I have written in detail about heap memory and memory generations like young gen, PermGen. Heap data area is created at VM startup. Claiming the memory back is done automatically by the […]
[…] of java String we shall see how that property is used for instantiating a Sting instance. JVM maintains a memory pool for String. When you create a String, first this memory pool is scanned. If the instance […]
Hi Joe,
It is very nice paper on JVM memory, it’s short and simple. I would like to Thank you for writing this.
I have observed a minor typo in a line “We used to error like PermGen space not sufficient.” I thought to share with you.
I also learn …good topics…. thanks..