Java (JVM) Memory Types

Last modified on September 12th, 2014 by Joe.

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.

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.

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.jvm memory

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 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.

Key Takeaways

References:

Comments on "Java (JVM) Memory Types"

  1. gchatzip says:

    Very interesting catch there , that maybe gives you a first “dive” into the monstrous JVM !

    Keep up the good work :)

  2. ram says:

    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

  3. Srikanth S says:

    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

  4. Joe says:

    Hey Rams, thanks. Nice to see you commenting.

    @Srikanth – thanks Srikanth for following.

  5. Vinod says:

    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.

  6. Joe says:

    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

  7. EMAHELSA says:

    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,

  8. Raghu says:

    Static variables doesn’t belongs to methods, but how come they will be stored in Method Area.

  9. Joe says:

    @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.

  10. Praveen says:

    Great tutorial!
    As I always told that you blog rocks :)

    Have a great time.

  11. Java Array says:

    […] In java arrays are objects. All methods of an Object can be invoked on an array. Arrays are stored in heap memory. […]

  12. swati says:

    Fab one. learnt many new thngs.

  13. Naveena says:

    Good precise tutorial

  14. Anand says:

    Great article and simple to follow.Thanks !! Keep up the good work

  15. chandana says:

    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.)

  16. kashif says:

    hey man …. thnks

    great ways of explanation… keep the good work goining…

    god bless u.

  17. Kumar says:

    Really Nice Explanation And website design

  18. Yudhi Karunia Surtan says:

    very nice article. thanks.

  19. Romit Mewada says:

    it’s very helpfull

  20. Anisha says:

    Very clear and a very good start up to explore more. Thanks for providing such nice notes.

  21. kishor says:

    I had never seen this type of blog..
    It’s fantastic.

  22. aneem says:

    Job well Done…

  23. sv says:

    Excellent explanation!!

    Waiting to see more on frameworks like Spring, Hibernate Struts etc

  24. Anonymous says:

    Perfect for a starter..

  25. Anton Michael Jerald says:

    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

  26. Arpitha says:

    Can you please post some article on garbage collections. Not able to get one single tutorial , which provides complete insite.

  27. KiranJyothi says:

    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

  28. Ram Gowtham says:

    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.

  29. Ram Gowtham says:

    And could u plz post me a sample program with source codes joe. it ll b more helpful to me understand java programs easily….

  30. Atiq Khan says:

    Great work joe ….

  31. Selva M says:

    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.

  32. Himanshu says:

    Really gud article to understand the jvm memory architeure .

  33. Naveen says:

    Article on JVM memory is marvelous and SCP concept explained by chandana is superb.

  34. RAM says:

    where the local objects are created in jvm

  35. sagar says:

    very inteligent thinking..

  36. SagaR says:

    very inteligent tutorial..

  37. CHANDRA SEKHAR KOLLI says:

    Hi,

    You are very Intelligent.

    Very Nice Explanation……

    Your Web site also very good …. :-)

  38. Santosh says:

    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

  39. jay says:

    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.

  40. Surender Reddy Palakala says:

    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.

  41. arun says:

    Thanks !

  42. h c pandey says:

    Thanks for sharing such kind of wonderful information about java. Your blog is very informative. Please keep such beautiful postings.

  43. Amit Shrivastava says:

    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

  44. selvi says:

    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

  45. selvi says:

    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

  46. Alona says:

    Thank you! It really summarize well the basics, now after understanding I can move forward.

  47. bipin says:

    awesome one..

  48. bipin says:

    awesome one..

  49. akbar says:

    your explanation is so good. please provide some more information regarding abstract classes.

  50. Venk says:

    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.

  51. Prashant says:

    Hi Joe,

    Thank you very much.

  52. g.sampath kumar reddy says:

    ya it’s very useful and helpful content

  53. Siddhartha Mukherjee says:

    Hi Dear,

    This is simple and intreating tutorial with simple information. I like your blog. Keep Running.

  54. Tania says:

    HELLO Sir
    i jst wanted to know why objects in java are stored in heap not in stacks.

  55. Divvela says:

    If you could explain me where static variables, instance variables, Meta data, String pool can store. That would be great.

  56. wasim says:

    superb jhon sir you are doing a wonderful contribution of java…

  57. Tom Singh says:

    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?

  58. Abhishek says:

    Good work done man

  59. Anonymous says:

    how to set permgen space for weblogic.please any one reply me

  60. Anonymous says:

    Good but need to be elaborated some more

  61. Anonymous says:

    Gud wrk

  62. Ajay says:

    Explain the execution of a Java program with respect to the internals of JVM and the steps it takes.

  63. Sahil says:

    When memory to static variables or methods or class are available and where?

  64. Alex Chen says:

    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?

  65. Vitthal Shirke says:

    really interesting….

  66. Mohtashim says:

    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.

  67. Anonymous says:

    It is very good example.

  68. Corindiano says:

    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.

  69. dinesh says:

    sir,

    where the default values will get stored?where the default values are generated by JVM when the variables are not get initialized.

  70. dhananjay says:

    Hi..

    How can we increase/decrease the size of jvm

  71. Joe says:

    @dhananjay,

    Initial java heap size can be modified using parameter -Xms
    Maximum java heap size can be modified using parameter -Xmx

  72. rama says:

    how should i know the memory allocation for an object in heap

  73. Shishir says:

    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?

  74. Anand says:

    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.

  75. Jhonsi says:

    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…

  76. Anonymous says:

    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?

  77. kumarchandan says:

    this is good blogsite to read more

  78. JP says:

    Very interesting!!

  79. rithin mithra says:

    @ 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??

  80. krishnan says:

    Very Nice and i got clear knowledge…

    Thanks

  81. Shubham says:

    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…???

  82. Little Rascal says:

    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.

  83. Shree Ram Sharma says:

    Good to see the unique technique to explain the JVM Memory Architecture

  84. Sridhar Goranti says:

    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

  85. Rinku Vats says:

    nice explaination

  86. […] 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 […]

  87. ASM says:

    It is good & helpful. The description is short & simple.Easily understandable. Good Job Joe!

  88. horace says:

    good work! thanks Joe. keep moving!

  89. suresh says:

    good job

  90. James Bloom says:

    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.”

  91. Anonymous says:

    AWESOME!!!!

  92. G.Jeevaraj says:

    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.

  93. […] 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 […]

  94. Anonymous says:

    very intrusting

  95. prem says:

    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

  96. sony says:

    very nice article. Explained clearly and straight forward

  97. sony says:

    very nice article. Explained clearly and straight forward

  98. Anonymous says:

    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.

  99. SANTHOSH S says:

    In which memory area stores the Instance variable as well as local variables..?? I’m Confusing this please anybody can clarify it..!!

  100. Pushpak Khatri says:

    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

  101. Anonymous says:

    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 :)

  102. Rajnish Jha says:

    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 :)

  103. Rajnish Jha says:

    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. :)

  104. Sajin K K says:

    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?

  105. NIL says:

    Hi loe

    Am learning a lot .

    Thanks

  106. Sowmiya says:

    wonderful tutorial

  107. Rajeev says:

    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.

  108. Saurabh Kumar says:

    GC moves live objects from survivor space to tenured generation.When?

  109. rajesh kumar says:

    I want to know interface memory alocated or not?

  110. Sreevidya says:

    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.

  111. rahul dev says:

    good description sir

  112. Rahul says:

    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

  113. Baskar says:

    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,

  114. rajdeo says:

    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

  115. […] 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 […]

  116. Java String says:

    […] 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 […]

  117. Ujjawal Nayak says:

    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.

  118. Anonymous says:

    I also learn …good topics…. thanks..

Comments are closed for "Java (JVM) Memory Types".