1

"Why did the JVM replace the permanent generation with metaspace?"

This is a question that a classmate who has worked for 6 years encountered on the first side of byte. Unfortunately, he did not answer it.

Hi, my name is Mic, a Java programmer who has been working for 14 years.

How do we answer this question? What is the interviewer looking for?

Interview analysis

We all know that in Java8 and later versions, the structure of the JVM runtime data area is slowly being adjusted and optimized.

But in fact, these changes have no impact on business development partners.

So I can say that 99% of people can't answer this question.

However, the interview of the big Internet companies is to screen the 1% of outstanding talents, so through this question,

  • It can not only test the job seeker's understanding of the principle of JVM
  • It can also examine the solidity of the basic skills of job seekers
  • It can also realize the screening of senior talents

In Java7, the JVM runtime data area is like this.

In the Hotspot virtual machine, the method area is implemented in the permanent generation, which mainly stores the runtime constant pool, Klass class meta information, etc.

The permanent generation belongs to a piece of storage space in the JVM runtime memory. We can set the size of the permanent generation through -XX:PermSize.

When there is not enough memory, garbage collection is triggered.

image-20220803161151906

In JDK1.8, the JVM runtime data area is like this.

In the Hotspot virtual machine, the permanent generation is cancelled, and the data storage of the method area is realized by the meta space.

Metaspace does not belong to JVM memory, but directly uses local memory, so there is no need to consider GC issues.

By default, the metaspace can use unlimited local memory, but we can also use JVM parameters to limit the memory usage.

image-20220803161642753

Why use metaspace to replace permanent generation, there must be a reason behind it, but if job seekers can answer it.

It is necessary to have a certain understanding of the underlying principles of the JVM.

Let's take a look at the expert's answer.

Expert:

There are three reasons:

  • In version 1.7, the permanent generation memory has an upper limit. Although we can set it through parameters, it is difficult to determine the total number and size of classes loaded by the JVM.

    So it is easy to have OOM problems.

    However, the meta space is stored in the local memory, and the upper memory limit is relatively large, which can avoid this problem.

  • The objects of the permanent generation are garbage collected through FullGC, that is, garbage collection is implemented at the same time as the old generation.

    After replacing with metaspace, Full GC is simplified. Class data can be released concurrently without pausing, while also improving GC performance

  • Oracle wants to merge the code of Hotspot and JRockit, and JRockit has no permanent generation.

The above is my understanding of the problem.

Summarize

Well, today's sharing ends here.

If you like my work, remember to like, favorite, and follow! ! !

Need gold, nine silver and ten Java interview questions to scan below

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Reprint please indicate the source Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。