Java (JVM) Memory Types

06/01/2011

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

Ads by Google

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

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

  9. Ads by Google
  10. @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.

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

    Have a great time.

  12. Pingback: Java Array

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

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

  15. hey man …. thnks

    great ways of explanation… keep the good work goining…

    god bless u.

  16. Really Nice Explanation And website design

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

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

  19. Excellent explanation!!

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

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

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

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

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

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

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

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

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

  28. Hi,

    You are very Intelligent.

    Very Nice Explanation……

    Your Web site also very good …. :-)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  44. Good but need to be elaborated some more

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

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

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

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

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

  50. sir,

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

  51. Hi..

    How can we increase/decrease the size of jvm

  52. @dhananjay,

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

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

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

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

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

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

  58. Very Nice and i got clear knowledge…

    Thanks

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

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

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

  62. Pingback: Java Weak Reference

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

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

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

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>

ABOUT
I am Joe, author of this blog. I run javapapers with loads of passion. If you are into java, you may find lot of interesting things around.
Ads by Google
STAY in TOUCH:

Email:

Core Java | Servlet | JSP | Design Patterns | Android | Spring | Web Service | © 2008-2012 javapapers.