Interview by manufacturers: How do you talk about online JVM is optimized?

JVM (Java Virtual Machine) is simply to run Java code interpreter, Screws as a programmer at JVM actually almost understand it, do not know the details of the internal JVM can still write quality code! But a scene to build rockets, aircraft (interview) you do not understand the JVM, the interviewer will be severely abuse, this issue JVM listed common interview questions:

  1. Said a JVM memory model look like?
  2. When objects can be recovered?
  3. What, what advantages and disadvantages of each common garbage collector algorithm?
  4. When an object enters years old?
  5. What is the guarantee space allocation strategy?
  6. How to optimize the reduction of Full GC?

Faced with this big wave JVM face questions, you really live it Hold?

JVM memory model look like?

JVM memory model can be roughly divided into thread-private area and a shared area , the private area of the thread by the virtual machine stack, native method stacks, program counter, and by the shared heap region, the metadata space (area method) composition.

Then someone asked JVM memory model you will recall at the top of the chart, but knowing the way the JVM memory model that does not work, but also know what they are doing.

VM stack / native method stacks

When you come across StackOverflowExceptionwhen this exception, there is no why is there such an exception under thinking? The answer lies in the stack virtual machine, JVM method will be generated for each stack frame and the stack frame is pressed into the virtual machine stack.

For chestnut: Suppose JVM parameter -Xssset 1m, if a method which creates an array of 128kb, and that this method can only be in the same thread recursive 4 times, then recursively the fifth time will be reported StackOverflowExceptionabnormalities because the virtual machine the stack size is only 1m, each recursive methods need to allocate space in the virtual machine 128kb stack is, it is the fifth time to display on the lack of space.

Program Counter

The program counter is a row number designator recorded bytecode executed by the current thread. Multithreading is the JVM by the CPU round-robin (i.e., alternately switching the thread and allocated processor execution time) algorithm is implemented. In other words, because a thread might run out of time slice is suspended in the implementation process, while another thread time slice to get started.

Simply put, the main function of the program counter is to record the line number indicator byte code executed by the current thread .

Method region (metadata area)

The method of the storage of metadata information classes, static variables, constants and other data.

Heap (heap)

We usually use newto create keyword objects will enter the heap, GC heap area is the focus of care, heap will be divided into: the new generation, old time, but the new generation will be further divided into Eden area and Survivor areas:

The new generation of the Eden area and Survivor district, according to the JVM collection algorithm, but now most of them are used generational recovery algorithm, so the time to introduce the new generation will be directly heap summarized as Survivor and Eden District area.

summary

JVM memory model Summary:

  • JVM memory model is divided into thread-private area and shared area
  • VM stack / native method stacks store thread is responsible for the implementation of the method stack frame
  • Recording position of the program counter for a thread of execution instruction
  • Method region (metadata area) metadata information storage class static variables, constants and other data
  • Heap (heap) use newto create keyword objects will enter the heap, the heap is divided into the old and the new generation's

When objects can be recovered?

JVM recovery determination target in two ways: the reference count , the GC Roots , simple reference count, for each object JVM maintains a reference count , the reference count of zero is assumed that the object A is no task described object references the object A, the object A that It can be recycled, but the reference count has a drawback that can not solve the problem of circular references.

GC Roots through a series of named GC Roots object as a starting point, to start the search downward from the nodes, called a reference path searched chain, when an object has no references to GC Roots chain joined, it is proved that the object unavailable.

In Java, an object can be used as the GC Roots of the following categories:

  • VM stack object references;
  • The method of the object in a static class attribute references;
  • Object method constant reference region;
  • Object referenced in the JNI native method stacks (i.e., the general said Native method);

summary

When an object is in general by GC Roots search less, indicating that the object can be recovered, but when the recovery depends on GC mood!

What, what advantages and disadvantages of each common garbage collector algorithm?

Clear labeling

This algorithm is divided into two points: marking, clear two stages, the mark phase is set to begin scanning from the root (GC Root), reaches each object will be marked as a viable state of the object, the cleanup phase after the scanning is completed there will be no marked objects to clear out.

With a diagram illustrates:

This algorithm has a flaw is generated memory fragmentation, the B is removed as shown above will leave a memory area, if the latter need to allocate a large object will result in no contiguous memory available.

Tags to organize

Tags to organize would be no 内存碎片problem, but also from the root collection (GC Root) to begin scanning to mark and clear the unwanted objects, purges after completion it will organize memory.

Such a memory is contiguous, but another problem arises in that: each had the moving object, and therefore costly.

Replication algorithm

JVM will push into the copy algorithm bisector, if the stack is set 1g, that when using the stack of copy algorithm will be divided into two regions with a respective 512m. When allocating memory for objects always use one of them to distribute, after full allocation, GC will be marked, and then the object is moved to another surviving a blank area, and then remove any objects did not survive, so repeat the process, there will always be a blank area is not reasonable to use.

Two regions are used interchangeably, the biggest question is will lead to a waste of space, now heap memory usage is only 50%.

summary

JVM collection algorithm Summary:

  • Clear labeling speed, but it will produce memory fragmentation;
  • Tags to organize to solve the mark to clear memory fragmentation problems, but each had to move objects, so the cost is high;
  • Copy the algorithm does not need to move the object memory fragmentation, but lead to waste of space;

When an object enters years old?

Out of the newly created object will remain in the beginning of the new generation, but with the JVM running, long some surviving objects will move slowly to the old era.

According target age

JVM will increase the target age (age) of the counter, each object "get through" a GC, the age will +1, to be the object reaches the set threshold (default is 15 years old) will be shifted to move to the old era, may by -XX:MaxTenuringThresholdadjust this threshold.

Once Minor GC, the target age will be +1, reached the threshold of the object is moved to the old era, the other surviving objects will remain in the new generation.

Dynamic Age estimation

In addition there is a policy based on target age also makes the object into the old era, without waiting 15 years old to enter the GC after his roughly rule is, if you put the current object Survivor, the total size of the object is greater than this number of memory Survivor 50%, then the object is greater than the age group of objects, you can directly enter old age.

A on FIG, B, D, E four objects, if Survivor 2is 100m, if the A + B + Dmemory size exceeds 50m, D is now aged 10, it will be moved to the old E's. Indeed this calculation logic is this: Age Age 1 + n 2 + target age plurality of sum exceeds Survivor50% of the area, it will age more than n objects into year old.

Large objects directly into the old year

If you set -XX:PretenureSizeThresholdthis parameter, if the object you want to create is greater than the value of this parameter, such as allocating a large array of bytes, this time directly to the large object into the old era, the new generation will not pass.

Doing so can avoid large objects in the new generation, repeatedly escaped the GC, had to carry them to copy to copy to go, and finally into the old era, such a large copy of the object back and forth, is very time-consuming.

What is the guarantee space allocation strategy?

JVM before it occurs Minor GC, virtual chance to check whether old's maximum available contiguous space larger than the new generation of the total space of all objects, and if so, then the Minor GC is safe if less than, the virtual opportunity to view HandlePromotionFailure set the value of the item whether to allow the guarantee to fail. = To true if HandlePromotionFailure , it will continue to check whether old's maximum available contiguous space larger than the previous promotion to the average size of the object of old age , if more than the attempt to conduct a Minor GC, but this time Minor GC is still risky; if less than or HandlePromotionFailure = to false , then to conduct a Full GC.

How to optimize the reduction of Full GC?

The summary of some of the problems down the front, and then applied to the line, how it should be optimized to reduce the JVM Full GC it? 8G in a standard 4-core machine as an example, the system is first reserved 4G, 4G other is assigned as follows:

  • Heap memory: 3g
  • New Generation: 1.5g
  • New Generation Eden Area: 1228m
  • New Generation Survivor area: 153m
  • Method area: 256m
  • VM stack: 1m / thread

Set the parameters as follows:

-Xms3072m
-Xmx3072m
-Xmn1536m
-Xss=1m
-XX:PermSize=256m
-XX:MaxPermSize=256m
-XX:HandlePromotionFailure
-XX:SurvivorRatio=8
复制代码

The system estimates the amount of memory occupied per second

Prior to optimize the JVM, first to estimate the amount of memory occupied by the system per second, if a Nikkatsu million shopping system, the daily order volume of around 20w, according to eight hours a day count, probably that service orders per second there will be 500 requests, and how much memory each request occupies under a rough estimate, calculate how much memory you want to spend per second.

Suppose 500 requests per second, each request requires 100k space allocation, it needs to allocate approximately one second memory 50m.

Under calculate how long trigger a Minor GC

According to the previous estimate of 1 second memory needs to be allocated about 50m , then space Eden area is 1228m on average every 25 seconds that will perform a Minor GC.

Survivor whether sufficient area under examination

According to the above model, every 25 seconds is necessary to perform a Minor GC, GC during the execution of all objects and can not be recycled out of the new generation, and that every time that GC 50m target will not be recovered during the remainder of the implementation of about 100m per second Survivor will enter the area, but do not forget there are JVM dynamic mechanism to determine the age , so set down Survivor space obviously a bit small, so the new generation set 2048m, in order to avoid triggering dynamic judgment Age :

-Xms3072m
-Xmx3072m
-Xmn2048m
...
复制代码

Large objects directly into the old year

Large objects are generally long-term survival and use of objects, generally speaking directly into the setting object 1M years old, so avoid large object has been replicated in the new generation of back and forth, so add PretenureSizeThreshold = 1m parameters.

...
-XX:PretenureSizeThreshold=1m
...
复制代码

Set a reasonable target age threshold

Minor GC after default automatically promoted to escape after 15 years old and garbage collection, according to our assessment of 25 seconds to trigger a Minor GC, if in accordance with MaxTenuringThreshold the default value of the parameter, escaped GC 15, it should be a matter of six minutes after , and with the current business scenario where you can down a little bit, so that those objects should enter old age, as soon as possible into the old era, the Cenozoic avoid copying costs and waste of space, resulting in insufficient space for the new generation of Survivor, triggering Full GC.

...
-XX:MaxTenuringThreshold=6
...
复制代码

Guess you like

Origin juejin.im/post/5dc9f9675188250c31674b71