Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of MetaSpace in Java 8?

I know they have replaced PermGen with MetaSpace in Java 8. But I have few questions:

  1. Is MetaSpace by default is GC collected?
  2. Even the PermGen is GC collected by adding the args like -XX:+CMSClassUnloadingEnabled, then what makes MetaSpace better than PermGen?
  3. MetaSpace is based on native memory, so it keeps the java objects on the disks rather than on VM?
  4. Even MetaSpace can run out of memory? If so again I will get OutOfMemoryException.
  5. By default the MetaSpace can grow on increase in memory?

Thanks in advance

like image 759
batman Avatar asked Jun 06 '14 04:06

batman


People also ask

What is the advantage of Metaspace?

Advantages of MetaSpace Take advantage of Java Specification property: Classes and associated metadata lifetimes match class loaders. Per loader storage area: Metaspace. Linear allocation only. No individual reclamation (except for Redefine Classes and class loading failure)

What are PermGen and Metaspace in Java 8?

In Java 8, PermGen method area replaced with MetaSpace. They have moved permGem to the separate memory in the native OS and that is called MetaSpace. It can by default auto increases its size. In MetaSpace, classes can load and unload during the lifespan of the JVM.

What is Metaspace area Java?

What is Metaspace? Metaspace is a native (as in: off-heap) memory manager in the hotspot. It is used to manage memory for class metadata. Class metadata are allocated when classes are loaded.

What happens when Metaspace is full?

The MaxMetaspaceSize JVM option sets an upper limit on the commited size of the Metaspace, and if not configured large enough can cause java. lang. OutOfMemoryError: Metaspace. Also, as mentioned before, all of the space configured for the Compressed class space is reserved upfront, and can not grow later at runtime.


2 Answers

Is MetaSpace by default is GC collected?

Yes, GC will run on metaspace when its getting full, it would also dynamically increase (given its allowed to) the memory allocated for metadata.

Even the PermGen is GC collected by adding the args like -XX:+CMSClassUnloadingEnabled, then what makes MetaSpace better than PermGen?

The improvement is with the dynamic expansion of the metaspace which is something permgen wasn't able to do.

MetaSpace is based on native memory, so it keeps the java objects on the disks rather than on VM?

Based on the description of metaspace, it only uses the native memory (no paging).

Based on the research by Pierre - Hugues Charbonneau (link here), it's clear that the introduction of metaspace doesn't necessarily solve the OOM issue, it's a bandaid to the problem at best, it attempts to dynamically resize the metaspace memory to accomodate the growing number of classes which get loaded with a possible side effect of growing uncontrollably (so long as the native memory permits it).

We can achieve the famed OOM error by setting the MaxMetaspaceSize argument to JVM and running the sample program provided.

many thanks to Pierre - Hugues Charbonneau.

like image 128
Anantha Sharma Avatar answered Sep 27 '22 19:09

Anantha Sharma


In response:

  1. By default the Metaspace memory is collected if it reaches the MaxMetaspaceSize. This parameter is initially unlimited. The limit is the memory in your machine. But the memory is automatically freeded when a class and class loader is no longer needed. You only need to tune this parameter if you suspect that the ClassLoader has a memory leak.

  2. The MetaSpece use native memory and the organization in memory with pointers makes that the GC are faster than older PermGen memory.

  3. No, it means that the JVM use the memory like common C program and don't use the virtual memory space for java objects. That seems that the memory is only limited by the machine. Take care that the memory of the machine can be swapped to disk if needed.

  4. If you set the parameter MaxMetaspaceSize you can get OutOfMemory and, if you don't set this parameter you can get if the process allocate all the machine memory (including swap space).

like image 32
Fernando Rincon Avatar answered Sep 27 '22 21:09

Fernando Rincon