ChatGPT解决这个技术问题 Extra ChatGPT

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:

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


H
Hearen

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.


Yes, GC will run on metaspace when its getting full, it would also dynamically increase (given its allowed to) the memory allocated for metadata. Is it normal that GC will run out of heap?
the idea of having GC is to prevent that.. but it can get close to being full, then GC starts up.. but this can be prevented with intelligent memory config and sensible coding.. also having coupious amount of RAM can prolong the inevitable :)
The improvement is with the dynamic expansion of the metaspace which is something permgen wasn't able to do. are you saying that permgen cant be expanded & metaspace can? both of them can be limited by supplying arguments. So why is metaspace expandable?
@ernesto If you do NOT specify an upper limit to metaspace, it will expand as much as necessary (and possible).
From what i'm seeing myself, it doesn't seem to be GCd unless it hits the limit, making it necessary to set up a sensible limit for long-running processes.
F
Fernando Rincon

In response:

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. The MetaSpece use native memory and the organization in memory with pointers makes that the GC are faster than older PermGen memory. 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. 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).


if it reaches MetaspaceSize not the MaxMetaspaceSize
Metaspace definitely does not go to swap (testing it on Java 8u60 on Linux 64bit). On a leak it can push everything else to swap, crawling a machine to a halt.
@JigarJoshi And I've just tested that by default Metaspace is collected if it reaches the MaxMetaspaceSize and not MetaspaceSize.
@kubanczyk, you're wrong. The Java metaspace can be swapped. Obviously, if it is extensively used, then the memory of other applications will be swapped. "direct memory" can be misunderstood has its meaning depend on the context. Here it means "not java virtual memory, but os memory". But it is "os vitual memory" that is allocated in RAM and SWAP. With Linux, only privileged user can allocate memory directly in RAM (see kmalloc). JAVA is a userland process (I think it makes use of mmap).
@mcoolive In practice my Linux server was brought down several times by some Metaspace (i.e. class/loader) leak. In Linux, userland programs can pin the virtual memory so it always stays in RAM. My 16 gigabytes of classes were not extensively used and were allocated by non-root user, but not a byte ever got swapped out. The vital parts of OS were swapped out under pressure and this led to outage.
T
Tiny

Is MetaSpace by default is GC collected? Garbage collection of the dead classes and classloaders is triggered once the class metadata usage reaches the “MaxMetaspaceSize” which by default is "unlimited" so a proper monitoring is required to limit the delay or frequency of such GC. Even the PermGen is GC collected by adding the args like -XX:+CMSClassUnloadingEnabled, then what makes MetaSpace better than PermGen? The main goal is removing permgen was so that users do not have to think about correctly sizing it. MetaSpace is based on native memory, so it keeps the java objects on the disks rather than on VM? Disk is not native memory but storage device. Native memory, in this context is the area, is the memory for the process left over from the Java heap Even MetaSpace can run out of memory? Yes, it is limited by the amount of memory in your machine.


Haha point 2 made me laugh. One of my production servers crashed twice today because Metaspace used all the RAM and pushed everything else to swap. Oh didn't that made me think about how to size Metaspace :D