Java (JVM) Memory Types

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.

Ads by Google

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

  • Local Variables are stored in Frames during runtime.
  • Static Variables are stored in Method Area.
  • Arrays are stored in heap memory.

References:

This Core Java tutorial was added on 06/01/2011.

previous post: Java Primitive

next post: Apache (ASF) Resigns from JCP Committee

122 comments on “Java (JVM) Memory Types

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

    Keep up the good work :)

  2. 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. 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. Hey Rams, thanks. Nice to see you commenting.

    @Srikanth – thanks Srikanth for following.

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

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

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

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

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

  9. @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. Great tutorial!
    As I always told that you blog rocks :)

    Have a great time.

  11. Pingback: Java Array

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

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

  14. hey man …. thnks

    great ways of explanation… keep the good work goining…

    god bless u.

  15. Really Nice Explanation And website design

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

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

  18. Excellent explanation!!

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

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

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

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

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

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

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

  25. Really gud article to understand the jvm memory architeure .

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

  27. Hi,

    You are very Intelligent.

    Very Nice Explanation……

    Your Web site also very good …. :-)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  43. Good but need to be elaborated some more

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

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

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

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

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

  49. sir,

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

  50. Hi..

    How can we increase/decrease the size of jvm

  51. @dhananjay,

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

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

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

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

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

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

  57. Very Nice and i got clear knowledge…

    Thanks

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

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

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

  61. Pingback: Java Weak Reference

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

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

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

  65. Pingback: Java Closures

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

  67. very nice article. Explained clearly and straight forward

  68. very nice article. Explained clearly and straight forward

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

  70. So how do you find the Acer Aspire AS5552-7677 on sale.
    Sometimes, the entire computer will crash and you know these are
    the tell tale signs that the computer has detected a driver problem
    with your Acer product. Web browsing is available through the HSDPA connection and Wi-Fi at speeds of up to
    7. The new Acer F900 has come up with a huge touch sensitive screen.

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

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

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

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

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

  76. I want to know interface memory alocated or not?

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

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

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

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

  81. Pingback: Java JVM Run-time Data Areas

  82. Pingback: Java String

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

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>