Golden nine silver ten must ask Java interview questions

After working for six years in an interview, I was asked why the JVM replaced the permanent generation with metaspace? 

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

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

Hi everyone, I'm Mic, a Java programmer with 14 years of experience.

How do we answer this question? What exactly 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 the small partners of business development.

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

However, the interviews of major Internet companies are to screen the 1% of outstanding talents, so passing this question,

  • It can not only examine the job seekers' understanding of the principles 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 looks like this.

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

The permanent generation belongs to a 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 will be triggered.

 

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

In the Hotspot virtual machine, the permanent generation is canceled, and the data storage in the method area is realized by the metaspace.

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

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

 

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

There must be a certain understanding of the underlying principles of the JVM.

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

Master: #

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 metaspace is stored in the local memory, and the upper limit of the memory is relatively large, which can well avoid this problem.

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

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

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

The above is my understanding of this issue.

Guess you like

Origin blog.csdn.net/l688899886/article/details/126753469