Monday, 10 June 2019

What is metaspace in Java?

Until Java 7, there was an area in JVM memory called PermGen (Permanent Generation), it is a special heap space which is separate from the main Java heap where JVM keeps track of metadata of the classes which have been loaded and used to keep its classes.

In Java 8, the PermGen space has completely been removed and is kind of replaced by a new space called Metaspace. The consequences of the PermGen removal is that obviously the PermSize and MaxPermSize JVM arguments are ignored and you will never get a java.lang.OutOfMemoryError: PermGen error.

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.

Default maximum PermGen Size
Default maximum Metaspace size
32-bit client JVM
64 MB
32-bit server JVM
64 MB
64-bit JVM
82 MB

As mentioned in the above table, Metaspace has an unlimited default maximum size. On the contrary, PermGen has a default maximum size of 64 MB on 32-bit JVM and 82 MB on the 64-bit version.

Prior to Java 7, interned Strings used to be kept on the PermGen. That caused some serious problems with the infamous: java.lang.OutOfMemoryError: PermGen space

Whenever there is a need to resize PermGen/Metaspace, JVM will do it as it does with the standard heap. Resizing those spaces requires a full GC, which is always an expensive operation. It can usually be observed during a startup when a lot of classes are being loaded. Especially if the application has dependencies on many external libraries. If there are a lot of full GCs during the startup, it’s usually because of that. If that case, increasing the initial size can boost the startup performance.

Commands to set initial and maximum PermGen

-XX:PermSize=N -  sets the initial (and minimum size) of the Permanent Generation space.
-XX:MaxPermSize=N  -  sets the maximum size of the Permanent Generation space.

Commands to set initial and maximum Metaspace

-XX:MetaspaceSize=N  - sets the initial (and minimum size) of the Metaspace.
-XX:MaxMetaspaceSize=N  - sets the maximum size of the Metaspace.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...