133

Until Java 7 there was an area in JVM memory called PermGen, where JVM used to keep its classes. In Java 8 it was removed and replaced by area called Metaspace.

What are the most important differences between PermGen and Metaspace?

The only difference I know is that java.lang.OutOfMemoryError: PermGen space can no longer be thrown and the VM parameter MaxPermSize is ignored.

2
  • first google result: infoq.com/articles/Java-PERMGEN-Removed
    – the8472
    Nov 25, 2014 at 15:57
  • 1
    @the8472 Yeah, but this (and many others) google results describe only Metaspace mechanism, not mentioning anything about exact differences between this and PermGen.
    – Kao
    Nov 25, 2014 at 16:04

5 Answers 5

159

The main difference from a user perspective - which I think the previous answer does not stress enough - is that Metaspace by default auto increases its size (up to what the underlying OS provides), while PermGen always has a fixed maximum size. You can set a fixed maximum for Metaspace with JVM parameters, but you cannot make PermGen auto-increase.

To a large degree it is just a change of name. Back when PermGen was introduced, there was no Java EE or dynamic class(un)loading, so once a class was loaded it was stuck in memory until the JVM shut down - thus Permanent Generation. Nowadays classes may be loaded and unloaded during the lifespan of the JVM, so Metaspace makes more sense for the area where the metadata is kept.

Both of them contain the java.lang.Class instances and both of them suffer from ClassLoader leaks. Only difference is that with Metaspace default settings, it takes longer until you notice the symptoms (since it auto increases as much as it can), i.e. you just push the problem further away without solving it. OTOH I imagine the effect of running out of OS memory can be more severe than just running out of JVM PermGen, so I'm not sure it is much of an improvement.

Whether you're using a JVM with PermGen or with Metaspace, if you are doing dynamic class unloading, you should to take measures against classloader leaks, for example by using my ClassLoader Leak Prevention library.

5
  • 27
    Neither Permgen, nor Metaspace contain instances of the class Class. They only keep meta information about loaded classes. Instances of the class Class are kept in regular heap, like instances of other classes. Jan 6, 2017 at 15:47
  • Nice comparison. Thanks
    – Sandeep
    May 23, 2017 at 15:52
  • 4
    By the way, OTOH means, "On the Other Hand"
    – sofs1
    Mar 18, 2019 at 18:25
  • btw, "btw" means "by the way"
    – Ricardo
    Sep 12, 2022 at 22:27
  • there was no Java EE or dynamic class(un)loading .. Is there an official document for this description?
    – HelloWorld
    Nov 30, 2022 at 15:16
51

Bye, Bye PermGen, Hello Metaspace

PermGen has been completely removed.

Metaspace garbage collection - Garbage collection of the dead classes and classloaders is triggered once the class metadata usage reaches the MaxMetaspaceSize.

The space Metadata was held is no longer contiguous to the Java heap, The metadata has now moved to native memory to an area known as the Metaspace.

In Simple words,

Since the class metadata is allocated out of native memory, the max available space is the total available system memory. Thus, you will no longer encounter OOM errors and could end up spilling into the swap space.

The removal of PermGen doesn’t mean that your class loader leak issues are gone. So, yes, you will still have to monitor your consumption and plan accordingly, since a leak would end up consuming your entire native memory.

Some other articles, with analysis: Link1, Link2 , and this

4
  • 6
    Instead of MaxPermGen you have MaxMetaspaceSize, so there is no reason it will use more, or less memory or you have less control. Nov 25, 2014 at 16:29
  • 2
    what memory are we talking about here? RAM memory or HDD memory.
    – Dinesh
    Apr 22, 2018 at 4:18
  • 1
    @Dinesh RAM(internal memory) Apr 3, 2019 at 5:06
  • OOM => "Out Of Memory" Aug 8, 2021 at 8:07
14

In short, Metaspace size auto increases in native memory as required to load class metadata if not restricted with -XX:MaxMetaspaceSize

3

PermGen

  • (Permanent Generation) is a special heap space separated from the main memory.
  • The JVM keeps track of class metadata in the PermGen. Also, the JVM stores all the static content in this.
  • Due to limited memory size, PermGen can throw OutOfMemoryError.

Metaspace

  • Metaspace is a new memory space.
  • It has replaced the older PermGen memory space.
  • It can now handle memory allocation.
  • Metaspace grows automatically by default.
2

Here to make it simple.

what is PermGen : PermGen is a special heap space separated from the main memory heap. Class metadata is loaded here. java 7 : PermGen is the space where JVM keeps track of metadata of the classes which have been loaded.
java 8 : PermGen is replaced by Metaspace with the capability to auto increase the native memory as per the requirement to load the class metadata.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.