Metaspace in Java8

Metaspace in Java8One of the new features that we will see in Java8 is the introduction of Metaspace. Metaspace is nothing but a replacement of our old friend PermGen (Permanent Generation) space.

What is PermGen?
The permanent Generation is part of your heap space that is actually used to store meta-data information for the classes created by users and prior to Java 7, it is also used to store interned Strings (interned strings – used to remove String duplicates by internalizing them to reduce memory consumption). As of Java 7, intern strings also moved as a part of heap space to make the more space available in Permanent Generation.

As a Java developer I am sure that all of us have seen this very famous OutOfMemoryError: PermGen Space error and it happens when Permanent Generation space is full , which is mostly happened by memory leak. Most common reason for a memory leak is when loaded classes and class loaders are not garbage collected when they are undeployed.

Metaspace in Java8:

PermGen has been completely replaced by MetaSpace in Java8, a native memory  to store class meta-data information and that grows automatically. Good thing is that as of Java 8, we will never see OutOfMemoryError : PermGen Space error , moreover we won’t have to tune PermSize and MaxPermSize arguments at the start-up of application. On the contrary these two jvm arguments are now replaced by MetaspaceSize and MaxMetaspaceSize. These two new flags gives us the flexibility to change the value for default size of metaspace and to change  the maximum value that this metaspace can take.

MetaspaceSize : This flag comes into the picture when you want to change the default size of your native memory that jvm can use. Use this flag only when you are sure that your application is going to need more memory than what is provided by default otherwise you will end up in suppressing some of the Garbage Collectors that were supposed to run in the default configuration.

MaxMetaspaceSize : This flag provides you flexibility to set the maximum size that you want to allocate for metaspace. You might want to do this to control the space used by your applications (multiple) running on a server or in a case of memory leak so that it won’t use all of available space in native memory. Now in the case when your native memory is fully and your application still needs more space in that case you will see a new error message : java.lang.OutOfMemoryError: Metadata space.

Overall if we see there is not a much difference between PermGen and Metaspace in Java8 except the new flags and the error message that we have seen earlier. Only advantage now you will have more space in the heap, but on the other side we don’t have any tool with which we can analyze this meta-data information as compared to the tools available e.g.jconsole ,HeapAnalyzer etc for analyzing heap.

For performance comparison of PermGen Vs Metaspace you can refer this post and this email thread.

Note : Java8 has been already officially released on 18th March 2014.

Disclaimer : All the above used images belong to their respective owners and used only for reference.


 

Let'sConnect

Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.
Let'sConnect

2 thoughts on “Metaspace in Java8

  1. PermGen and MaxPermGen are not the exact JVM args used to tune PermGen space. They are PermSize and MaxPermSize.

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

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=""> <s> <strike> <strong>