Why MetaSpace Size is twice as big as Used MetaSpa

2019-07-06 09:01发布

I wrote a program to simulate MetaSpace OOM. But I found that MetaSpace Size is almost always twice as big as Used MetaSpace. Why?

I run my program with flag -XX:MaxMetaspaceSize=50m, the program throw OOM when Used MetaSpace reached about 25M rather than 50M, Why?

enter image description here

2条回答
三岁会撩人
2楼-- · 2019-07-06 09:20

I think the following two experiment will explain what does the gap between MetaSpace Size and Used MetaSpace mean:

EXP-1: load one class per ClassLoader, I got this: enter image description here

EXP-2: load five classes per ClassLoader, I got this: enter image description here

As Java 8 document says:

Space is requested from the OS and then divided into chunks. A class loader allocates space for metadata from its chunks (a chunk is bound to a specific class loader).

I think it means a chunk can contains usually more than one class, so utilization rate of MetaSpace Size grows as you load more classes per ClassLoader.

You can reappear the results by running code here using command:

java -XX:MaxMetaspaceSize=50m MetadataOOMSimulator 100000000

Attention: make sure that all classes be loaded should not appear in CLASSPATH (usually include current directory), or those classes will be loaded by APPClassLoader rather than customized ClassLoader.

查看更多
倾城 Initia
3楼-- · 2019-07-06 09:34

Good question ;-)

Have a look at this pretty good article.

PermGenSpace vs MetaSpace

查看更多
登录 后发表回答