Open full code force, code dynamic life, wechat search [Craig Mowgli], pay attention to this word does not drive the code of the old driver

This article has been included on GitHub github.com/BeKingCodin… , my contact information and technical exchange group, welcome Star and perfect

The interview began

A middle-aged man in a plaid shirt who had apparently not washed his hair for a week walked in with a MAC covered in stickers. Looking at the Mediterranean Sea, I thought he must be a senior architect! But have seen a programmer the great partner, certainly with Mowgli as practiced blowing ability, certainly not with empty.

Hi young man, it says on your resume that you are familiar with the principles and tuning of JVM. Tell me what you know about JVM first.

The in the mind cannot help dark scold, this call what problem, too wide, but you can’t say. This question is just like introducing yourself.

The Java Virtual Machine is the foundation for the Java language to be widely distributed, making it possible to install Java anywhere and run it. Running on different platforms does not require recompilation, and the JVM shields platform-specific information, leaving us Java programmers to focus only on the implementation of the object code.

What are the parts of the JVM?

Heap, Stack, program counter Register, Method area/Permanent, Native Method Stack.

I’m sure 99% of you will be able to answer the five basic components of the JVM. If you can’t answer the five basic components of the JVM, please fill in the blanks.

But if you want to further differentiate yourself from the other candidates, here are a few things to add.

After JDK8, the JVM memory space was changed. The main difference is that the method area was removed and the meta-space was added. The meta-space is placed in direct memory outside the JVM memory space. In JDK8, PermSize and MaxPermSize parameters for the method area are invalid.

Good. Then why do we need to do this? What is the structure of a meta-space?

Sometimes the interviewer doesn’t really know how to ask, so we need to guide the interviewer. For example, through the last question, we can lead to their understanding of meta-space, this knowledge point is actually very can reflect the ability.

Before JDK8, there was a method area in the JVM model, which was a very important area that could be shared by threads, just like the heap.

In the method area, the information of each class (including the name of the class, method information, field information), static variables, constants, and the compiled code of the compiler are stored. We also used to call method area it “forever” (Permanent Generation), but most Java programmers should have seen “Java. Lang. OutOfMemoryError: “PremGen space” exception, where “PermGen space” actually refers to the method area.

The -xx :MaxPermSize parameter sets the size of the method area, but how much space to allocate to it is difficult to determine because the size of PermSize depends on many factors, such as the total number of classes loaded by the JVM, the size of the constant pool, and the size of the method.

As more and more dynamic class loads occur, this chunk of memory becomes less and less manageable. OOM:PermGen error OOM:PermGen error OOM:PermGen error OOM:PermGen error OOM:PermGen

The essence of a meta-space, like a permanent generation, is an implementation of the method area in the JVM specification. However, the biggest difference between a meta-space and a permanent generation is that the meta-space is not in the virtual machine, but uses local memory. Therefore, by default, the size of the meta-space is limited only by local memory.

Note: I love to ask this question in an interview, but it’s actually quite understandable. After reading this article, I believe you can talk about it in front of the interviewer.

You mentioned the memory overflow, can you say several common OOM?

Some students see this question, thinking that this is not to dig a hole for themselves, I know OOM is Out Of Memory error, where also know the way?

But we encounter everything do not panic, repeated train of thought problem is not big.

When a serious shortage of the JVM memory, will be thrown. Java lang. An OutOfMemoryError, according to the actual production experience, OOM is a very serious problem, generally in the application log OutOfMemoryError configuration keyword alerts, once found, the processing immediately.

Common errors that cause OOM are as follows:

(1) Java Heap Space

When the heap does not have enough memory space to store the newly created object, will be thrown Java. Lang. OutOfMemoryError: Java heap space error. For example, a request to create a very large array object is made, or there is a memory leak, the reclaimed object is not freed, there is not enough heap space, etc.

(2) GC overhead limit exceeded

When Java process takes more than 98% of the time doing GC, but back to less than 2% of the memory, and repeated five times in a row, the activity will be thrown. Java lang. OutOfMemoryError: GC overhead limit exceeded the error.

(3) Unable to create new native thread

Java threads are user-level threads, which are created and used by operating system kernel-level threads. Each Java thread needs to occupy a certain amount of memory space. This type of error is reported when the JVM requests the underlying operating system OS to create a native thread and there is not enough resource allocation.

At this time it is estimated that the interviewer will show a (wretched) smile, thinking that the young man interviewed today is not bad, master very solid. Now it’s time to ask some tough questions, find out what he’s up to.

Do you have experience with JVM tuning in your projects?

Invisible force, the most deadly. And this question is to give you the best chance to brag *!

Many students will think that the JVM is low-level knowledge, very difficult to master, but this is a required course to become a good Java programmer!

Even if your heart is churning, respond with the appropriate amount of detachment and say, “Yes, I have done some, including JVM optimizations and online troubleshooting before the project goes live.”

The other party including me will be very interested, the heart began to read: finally began to have some meaning.

After all, the previous questions are more theoretical, but when it comes to JVM tuning and troubleshooting, you’ll see the basics, not just the backrest concept.

How do you know how well the JVM is running?

Many mature companies have built visual monitoring platforms such as Zabbix, Ganglia, Open-Falcon, Prometheus, etc., but without this, it is possible to observe memory changes, GC times, and GC times in various areas of the JVM from the command line.

Jstat is a small but powerful tool that intuitively prints each parameter. Specific use we can see the previous article:

Jstat: No More JVM tantrums

What should I do if I find memory overflow?

Memory overflow occurs. We need to determine which OOM is used first and then handle it accordingly. For example, the heap memory overflow may be caused by large objects. In this case, we can use jMAP tool to dump the memory snapshot, and then use Visual tools such as JHAT or Visual VM to analyze it.

There was a real major Bug “Major Accident! Online system frequently freezes, the murderer is Full GC?” , you can learn to analyze and troubleshoot ideas.

When interviewing, it’s okay to talk about it as if it’s your own experience. I believe that by this point the interviewer has secretly given you a thumbs up. And quietly gave you an A+.

The end of the interview

Boy, you can do it. When do you have time to come to work? Why don’t you come tomorrow?

I still need to think about it. Maybe next Monday.

Ok, I wonder if he has many offers on hand. No, I have to ask HR to give him more money and communicate with him on wechat.

You can’t help but give yourself a thumbs up for making it to the end. In technical interviews, whether it’s the JVM or any other question, if you can come up with practical examples, or problems and gains from your own development process, you will impress the interviewer with a logical answer. You think your interviewer might just be fixing bugs that confuse you and them.

The other thing is that when you answer a question, don’t memorize the concept. The interviewer will probably want to hear something different from everyone else.

First of all, our project encountered a performance bottleneck when it was launched, especially when there was a second kill activity. JVM OOM error is usually reported due to the large amount of data, so it is necessary to check and tune it. Check the distribution of observed objects, garbage collection time… To start, I tuned by adjusting memory size, changing garbage collection times… And then combined all of that with the characteristics of our project, and finally I did online… .

If you answer my questions in such a methodical, well-reasoned way and say a lot of things beyond the question, I think you’re more than just a code writer. Your logic is clear, and you have your own understanding and thinking about knowledge. In a nutshell, your offer is a success.


Welcome to my public account “Criag Mowgli”


I’m mowgli, the old driver of code world. Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see you in the next article!