Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Metaspace and Native Area memory gets managed in Java?

From what I have understood, the Native Area inside the JVM is completely off-limits for the Garbage Collector. Inside the Native Area, in which Metaspace is located. In the aforementioned Metaspace, we have areas such as Constant Pool, Field and Method Data.

Since the Native Area is managed by the C++ memory management (or at least that's how I understand it), how come that Metaspace, that is not of a fixed size, instead grows dynamically, depending on how much memory is needed, will not run out of the memory? Is Garbage Collector allowed in the Metaspace, but not the rest of Native Area? Is C++ managing the memory there dynamically?

like image 709
ForInfinity Avatar asked May 29 '26 20:05

ForInfinity


1 Answers

how come that Metaspace, that is not of a fixed size

You can limit the size by setting value for MaxMetaSpaceSize on command line.

...will not run out of the memory?

Yes, it actually does run out of memory. When it exceeds usage of available memory and will get java.lang.OutOfMemoryError: Metaspace exception.

Is Garbage Collector allowed in the Metaspace

Yes. The GC will collect both the Java Heap and the Metaspace, but not the native heap. It is managed by whoever native code owns it.

Reference :

3.2 Understand the OutOfMemoryError Exception

About G1 Garbage Collector, Permanent Generation and Metaspace

like image 56
Ravi Avatar answered May 31 '26 09:05

Ravi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!