3

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?

Ravi
  • 30,829
  • 42
  • 119
  • 173
ForInfinity
  • 166
  • 1
  • 12

1 Answers1

4

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

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • Instead of `java.lang.OutOfMemoryError: Metaspace`, won't it be `java.lang.StackOverFlowError`? – ForInfinity Jan 20 '19 at 14:16
  • @ForInfinity Technically, both are different `StackOverFlowError` doesn't means `OutOfMemory` instead you have exceeded the stack size. – Ravi Jan 20 '19 at 14:17
  • Additionally, one major point remains unanswered, is GC allowed in other areas of the **Native Area**, other than the Metaspace? – ForInfinity Jan 20 '19 at 14:19
  • @ForInfinity the (not uncommon) confusion stems from using the term “garbage collector” for two entirely different things. Does the garbage collector traverse objects in the Metaspace? *No*. Does the garbage collector’s activity allow reclaiming memory inside the Metaspace? *Yes*. See also [this answer](https://stackoverflow.com/a/26484392/2711488) – Holger Jan 21 '19 at 10:18