Exploring Java Virtual machines-memory management and garbage collection

Source: Internet
Author: User
Tags domain name server xms

See: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt106

1. Data area of Java Virtual runtime

2. Common Memory Area adjustment parameters

-XMS: The initial heap size, default is the physical memory of 1/64 (<1GB), the default (minheapfreeratio parameter can be adjusted) when the free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-xmx

-XMX: Maximum heap size, default (maxheapfreeratio parameter can be adjusted) when free heap memory is greater than 70%, the JVM will reduce the heap until the minimum limit of-XMS

-XMN: The size of the new generation of memory space, note: The size here is (eden+ 2 survivor space). is different from the new Gen shown in Jmap-heap. Entire Heap size = Cenozoic size + Laosheng generation size + permanent generation size.
If the size of the heap is not changed, the Laosheng will be reduced after the new generation is enlarged. This value has a large impact on system performance, and Sun's official recommendation is 3/8 for the entire heap.

-xx:survivorratio: The capacity ratio of the Eden region to the survivor region in the Cenozoic, with a default value of 8. The ratio of two survivor to one Eden area was 2:8, and one survivor area accounted for 1/10 of the entire young generation.

-XSS: The stack size of each thread. After JDK5.0, each thread has a stack size of 1M, before each thread has a stack size of 256K. Appropriate adjustments should be made to the size of the memory required by the application's thread. In the same physical memory, reducing this value can generate more threads. However, the operating system of the number of threads within a process is still limited, can not be generated indefinitely, the empirical value of 3000~5000 around. General small application, if the stack is not very deep, should be 128k enough, large applications recommend the use of 256k. This option has a large impact on performance and requires rigorous testing. Similar to the Threadstacksize option interpretation, the official documentation does not seem to explain the phrase in the Forum: "-XSS is translated in a VM flag named threadstacksize" This value is generally set.

-xx:permsize: Sets the permanent generation (Perm Gen) initial value. The default value is 1/64 of physical memory.

-xx:maxpermsize: Sets the maximum number of persistent generations. 1/4 of physical memory.

3. Memory allocation method

1) on-heap allocation 2) on-stack allocation 3) out-of-heap allocation (directbytebuffer or directly using unsafe.allocatememory, but not recommended in this way)

4. Monitoring methods

1) When the system program runs, the Jstat–gcutil can be used to view the changes of each memory area in the heap and the working status of GC.
2) can add-xx:+printgcdetails–xloggc:<file> output to log file to view GC status at startup;
3) Jmap–heap can be used to view the size of each memory space;

5) The method of dating can be aggregated by GC

One, the new generation available GC

1) Serial GC (Serial Copying): The default GC mode in client mode can also be enforced by-XX:+USESERIALGC, by default Eden, S0, S1 are controlled by-xx:survivorratio, default is 8, Meaning
For the EDEN:S0 ratio, it can be viewed by jmap–heap [PID] after startup.

By default, only allocated on Tlab or Eden, only two cases are allocated on the Laosheng generation:
1. The amount of memory that needs to be allocated exceeds the size of Eden space;
2. If Pretenuresizethreshold is configured, the object size is greater than this value.

By default, when minor GC is triggered:
Previously minor GC promoted to old average size < remaining space for Laosheng generation < Eden+from survivor usage space. When Handlepromotionfailure is true, only the minor GC is triggered, and if false, the full GC is triggered.

By default, the new Generation object is promoted to the Laosheng generation rule:

1, experienced multiple minor GC still surviving objects, can be controlled by the following parameters: The Maxtenuringthreshold value, the default is 15.
2, to space put down, directly into the Laosheng generation;

2) Parallel GC (PARNEW): The CMS GC is used by default, or it can be specified by-XX:+USEPARNEWGC, while garbage collection is multi-threaded.

3) Parallel Recovery GC (Parallel scavenge): The default GC mode in server mode can also be specified by-XX:+USEPARALLELGC, and the size of Eden, S0, S1 can be controlled by-xx:survivorratio, But by default
In-xx:initialsurivivorratio, this value defaults to 8, which represents the Cenozoic size: s0, which should be paid special attention to.

By default, when allocations on Tlab and Eden fail, determine whether the amount of memory that needs to be allocated is >= half the size of Eden space, and is allocated directly on the Laosheng generation;

Garbage collection rules by default:

1, before recycling PS GC will first detect each PS GC, the promotion to Laosheng generation average size is greater than the remaining space Laosheng generation, such as greater than the direct trigger full GC;
2, after the recovery, will also follow the above rules for testing.

By default the new Generation object is promoted to the Laosheng generation rule:
1, experienced multiple minor GC is still alive objects, can be controlled by the following parameters: Alwaystenure, default false, that is, as long as minor GC survival, the promotion to Laosheng generation; Nevertenure, default false, means never promoted to Laosheng generation The above two are not set under the situation attaining, such as Useadaptivesizepolicy, start with initialtenuringthreshold value as the threshold for survival, after each PS GC will be dynamically adjusted, If the useadaptivesizepolicy is not used, the maxtenuringthreshold shall prevail.
2, to space put down, directly into the Laosheng generation.

After recycling, such as the Useadaptivesizepolicy,ps GC dynamically adjusts the size of Eden, to, and tenuringthreshold based on the operational state. You can set-xx:-useadaptivesizepolicy if you do not want to adjust dynamically. If you want to track each change, you can increase it on the start parameter: Printadaptivesizepolicy.

Second, Laosheng generation available GC

1. Serial GC (Serial Copying): The default GC mode under Client mode, can be specified by-XX:+USESERIALGC.

Summary OF trigger mechanism:
1) Old Gen space is insufficient;
2) Perm Gen space shortage;
3) Pessimistic strategy when minor GC;
4) allocating memory on Eden after minor GC still fails;
5) When the heap dump is executed;
6) External call System.GC, can be forbidden by-XX:+DISABLEEXPLICITGC.

2, Parallel recovery GC (Parallel scavenge): The default GC mode in server mode, can be specified by-XX:+USEPARALLELGC; The number of threads in parallel is the CPU core<=8? CPU core:3+ (CPU Core*5)/8 or by-XX:PARALLELGCTHREADS=X to force the designation. If SCAVENGEBEFOREFULLGC is true (the default value), the minor GC is executed first.

3, parallel compacting: can be specified by-XX:+USEPARALLELOLDGC force.

4, concurrent CMS: can be-XX:+USECONCMARKSWEEPGC to enforce the designation. The number of concurrent threads defaults to: ( The number of parallel GC threads +3)/4 can also be specified by Parallelcmsthreads.

Trigger mechanism:
1, when the use of laosheng generation space to reach a certain rate of triggering;

The default is 65% in Hotspot v 1.6, which can be viewed by printcmsinitiationstatistics (this parameter is not available in V 1.5) to see how much of this value , which can be forced by cmsinitiatingoccupancyfraction, the default value is not assigned to this value, is calculated according to the following formula: ((100-minheapfreeratio) + (double) ( Cmstriggerratio * minheapfreeratio)/100.0)/100.0; where minheapfreeratio default value: Cmstriggerratio default: 80.

2, when Perm Gen uses the CMS collects and the space uses to a certain ratio to trigger;

Perm Gen uses a CMS collection to set:-xx:+cmsclassunloadingenabled Hotspot V 1.6 defaults to 65%; can be specified by Cmsinitiatingpermoccupancyfraction, Similarly, it is calculated according to the following formula: ((100-minheapfreeratio) + (double) (cmstriggerpermratio* minheapfreeratio)/100.0)/100.0; where minheapfreeratio default value: Cmstriggerpermratio default: 80.

3, hotspot based on the cost calculation of the need to implement a CMS GC, can be-xx:+usecmsinitiatingoccupancyonly to remove the dynamic execution of the policy.
4, the external call System.GC, and set up the explicitgcinvokesconcurrent; It is important to note that in Hotspot 6, in this case, a bug can occur if the application uses NIO simultaneously.

6. GC Combination

1) Default GC combination

2) Optional GC combination

7. GC Monitoring

1) jstat–gcutil [PID] [intervel] [count]
2)-VERBOSE:GC//can assist to output some detailed GC information;-xx:+printgcdetails//output GC details;-xx:+printgcapplicationstoppedtime//Output GC causes application pause time
-xx:+printgcdatestamps//GC time information;-XX:+PRINTHEAPATGC//the size of each area in the heap before and after the GC,-xloggc:[file]//Output GC information to a separate file, with the recommendation to add, This consumes little, and has a great help in checking problems and tuning. GC logs can be analyzed using Gclogviewer or Gchisto after the log is taken down.
3) The graphical case can be directly analyzed with JVISUALVM.

4) View the memory consumption status

(1) long-term consumption, can be directly dump, and then mat (Memory Analysis tool) view can

(2) Short-term consumption, graphical interface, you can use JVISUALVM Memory Profiler or Jprofiler.

8, the System tuning method

Step: 1, assess the status quo 2, set Goal 3, try to tune 4, measure tuning 5, fine adjustment

Set Goals:

1) Reduce the frequency of full GC execution?
2) Reduce the time spent on full GC?
3) Reduce application downtime caused by full GC?
4) Reduce minor GC execution frequency?
5) Reduce minor GC consumption time?
For example, the GC tuning target for a system: reduce the frequency of full GC execution while minimizing the frequency of minor GC execution, time spent, and the time it takes for the GC to halt the application.

Measurement tuning:

1. Measuring Tools
1) Print GC log information:-XX:+PRINTGCDETAILS–XX:+PRINTGCAPPLICATIONSTOPPEDTIME-XLOGGC: {file name}-xx:+printgctimestamps
2) Jmap: (Because the default value of each version of the JVM may change, it is recommended to use Jmap first to observe the current memory size of each generation, GC mode)?
3) Health monitoring tools: Jstat, JVISUALVM, SAR, Gclogviewer

2. Information to be collected
1) minor GC execution frequency; How often does the full GC run, and how much time is spent per GC?
2) What is the peak period?
3) What is the effect of minor GC recovery? What is the consumption status of survivor, and how many objects will go into Laosheng generation each time?
4) What is the effect of full GC recovery? (Simple memory leak judging method)
5) system load, CPU consumption, QPS or TPS, response time

QPS per second query rate: is the measure of how much traffic is handled by a particular query server over a specified time period. On the Internet, machine performance as a domain name server is often measured by the query rate per second. Corresponds to the fetches/sec, that is, the number of response requests per second, which is the maximum throughput capacity.
TPS (Transaction per Second): The number of transactions or transactions that the system can process per second.

Try tuning:

Note the timed GC trigger mechanism for Java RMI, which can be prevented by:-XX:+DISABLEEXPLICITGC or by-dsun.rmi.dgc.server.gcinterval=3600000 to control the time of the trigger.

1) Reduce full GC execution frequency – usually bottlenecks
Laosheng generation itself occupies the memory space has been high, so as long as a little bit to put the object to Laosheng generation, the full GC;
The usual reason: the system caches too many things;
Example: PreparedStatement cache is too large when using the Oracle 10g drive;
Find out: Now execute dump and then perform mat analysis;

(1) After Minor GC always has the object unceasingly enters the Laosheng generation, causes the Laosheng generation unceasingly the full
The usual reason: survivor is too small.
System performance: The system response is too slow, the request volume is too large, each request allocated too much memory, the allocated object is too large ...
Find out: Analyze two times between minor GC exactly where the memory is allocated;
Using Jstat to observe the consumption status of Survivor,-XX:PRINTHEAPATGC, and output the detailed information before and after GC;
System optimization is not the content of GC optimization for slow system response.

(2) The memory consumption of Laosheng generation has been high
Tuning method: ① expands the size of the Laosheng generation (reduces the size of the Cenozoic or the size of the heap);
Reduce the impact of new attention on the minor GC and possibly cause full GC or severity;
Big heap Note the longer the full GC, the CPU is tough, is the OS a bit?
② Program Optimization (remove some unnecessary caches)

(3) After Minor GC, there are always objects constantly entering the Laosheng generation.
Prerequisites: Most of these objects entering the Laosheng generation will be recycled at full GC
Tuning method:
① reduces the frequency of minor GC execution;
② allows objects to be recycled as much as possible in minor GC: Increase Eden area, increase survivor, increase tenuringthreshold, and note that these may cause minor GC to perform frequently;
③ switch to CMS GC: Laosheng generation is not fully recovered, thus reducing the likelihood of full GC triggering;
④ Program Optimization: Improves response speed, reduces memory per request allocation,

(4) Reduce the execution time of a single full GC
The usual reason: Laosheng is too big ...
Tuning method: 1) Is it a parallel GC? 2) Upgrade CPU 3) reduce heap or laosheng generation

(5) Reduce minor GC execution frequency
Common Cause: More memory per request, large request volume
Usual approach: 1) Expand the heap, expand the new generation, expand Eden. Note: Reduce the amount of memory allocated per request, and increase the number of machines across the scale to share requests.

(6) Reduce minor GC execution time
The usual reason: the Cenozoic is too big to respond too slowly, resulting in more objects surviving each minor GC
Usual method: 1) Reduce the point of the new generation bar; 2) Increase the number of CPUs, upgrade the configuration of the CPU, and speed up the response of the system

Minor adjustments:

The first thing you need to know is:

① will the system go down when the response speed drops or the request volume rises?

② parameter adjusted how often does the system perform a minor GC, how often does the full GC run, and how long is the peak?

Amount that needs to be calculated:

① How much memory will be allocated on average per request? What is the average response time of the system? What is the request volume, how often does the minor GC, the full GC?

② the existing parameters, how often should be minor GC, full GC, compare the real situation, make certain adjustments;

Kill skill: Increase response speed and reduce the memory allocated per request?

9, System Tuning Example

Phenomenon: 1, the system response speed is about 100MS;2, when the system QPS growth to 40 o'clock, the machine executes every 5 seconds minor GC, every 3 minutes to execute a full GC, and soon has been full GC, 4, each full GC after the old generation will probably consume 400M, A little too much.

Solution: Solve the problem of too many full GC times

(1) Reduce the response time or the number of requests, this need to refactor, more trouble;-This is the ultimate method, often can solve the problem smoothly, because most of the problems are caused by the program itself.

(2) Reduce the consumption of laosheng memory, more reliable;--you can analyze the dump file (Jmap dump) and use the mat to find the cause of memory consumption, so as to discover the cause of the Laosheng memory consumption in the program.

(3) Reduce the memory consumption of each request, seemingly more reliable;--this is a mirage, there is no good way.

(4) Reduce the time that the GC caused the application to pause--you can use the CMS GS garbage collector. The parameters are set as follows:

-xms1536m-xmx1536m-xmn700m-xx:survivorratio=7-xx:+useconcmarksweepgc-xx:+usecmscompactatfullcollection

-xx:cmsmaxabortableprecleantime=1000-xx:+cmsclassunloadingenabled-xx:+usecmsinitiatingoccupancyonly-xx:+ Disableexplicitgc

(5) Reduce the minor GC promotion to old object each time. Optional method: 1) Adjust the new generation. 2) Adjust the large survivor. 3) Adjust the large tenuringthreshold.

Survivor: The current use of PS Gc,survivor space will be dynamically adjusted. Due to the small adjustment amplitude, resulting in the regular transfer of objects to the Laosheng generation, so the survivor zone is forbidden to adjust the dynamic,-xx:-useadaptivesizepolicy, and calculate the size of survivor space needed, so continue to observe, and do fine-tuning .... Finally, the full GC is postponed to 2 hours and 1 times.

10, the realization principle of garbage collection

Implementation of Memory Reclamation: 1) Reference count: not suitable for reference relationships of complex objects, especially for loop-dependent scenarios. 2) Graph tracing: A reference-relational scenario suitable for complex objects, which is used by the hotspot. Common algorithms: Copying, Mark-sweep, Mark-compact.

Hotspot starts by scanning a referenced object from root set and special handling of objects of type reference.
The following is a list of root sets: 1) the currently executing thread; 2) global/static variables; 3) the JVM handles;4) JNI "Java Native Interface" Handles;

In addition: Minor GC only scans the Cenozoic, when the Laosheng object refers to the new generation of objects, it will be treated as follows: When assigning a reference to an object, it passes through a write barrier process, in order to check if there is a case of laosheng generation referencing the new generation object. If any, it is recorded in the remember set. And when minor GC, the new generation object that the remember set points to is also the root set.

Cenozoic Serial GC (Serial Copying):

New Generation serial GC (Serial Copying) Full memory allocation strategy:

1) The allocation is first attempted on the Tlab (local thread allocation buffer);
2) Check whether it is necessary to allocate on the Cenozoic, if the size required to allocate is less than pretenuresizethreshold, the allocation is made on the Eden area, the allocation is successful, and the allocation failure continues;
3) Check whether you need to try to allocate on the Laosheng generation, and if necessary, traverse all generations and check if they can be allocated on that generation, if possible, and if you do not need to try to allocate on the Laosheng generation, continue;
4) Execute the new generation GC or full GC according to the policy decision, do not clear soft Ref when executing full GC;
5) If the size needs to be allocated larger than Pretenuresizethreshold, try to allocate on the Laosheng generation, otherwise try to allocate on the Cenozoic;
6) Try to enlarge the heap and allocate it;
7) Execute the full GC and clear all soft Ref, and proceed to the assignment as per step 5.

New Generation serial GC (Serial Copying) full memory recovery strategy
1) Check whether to is empty, not NULL to return false;
2) Check that the remaining space of the Laosheng is greater than the current eden+from size, such as greater than the value of true, such as less than and handlepromotionfailure to true, then check whether the remaining space is greater than the average size of each minor GC progression to Laosheng generation, If it is greater than return true, such as less than return false.
3) If the result above is false, the full GC is executed, and if the result above is true, perform the following steps;
4) Scan the reference relationship, copy the live object to the to space, if the object has survived more than Tenuring_threshold in the minor GC, or the allocation fails, then replicate to the Laosheng generation, as the copy still fails, Depending on the handlepromotionfailure, if you do not need to process, directly throw oom, and exit the VM, if you need to process, keep these new generation objects do not move;

New Generation Available Gc-ps

Full memory allocation policy
1) First allocated on the Tlab, allocation failure is directly allocated on Eden;
2) When allocation fails on Eden, check if the size to be allocated is >= half of Eden space, and if so, assign it directly in the Laosheng generation;
3) If the allocation still fails, and the GC has exceeded the frequency, then the oom is thrown;
4) The mode of failure to enter the basic allocation strategy;
5) perform PS GC, allocate on Eden;
6) Perform non-maximum compression of full GC, allocated on Eden;
7) Distribution on the old generation;
8) Perform the maximum compression full GC, allocated on Eden;
9) Distribution on the old generation;
10) If you fail, go back to 2.

In the most tragic case, the allocation triggers multiple PS GC and multiple full GC until oom.

Full Memory reclamation Policy
1) If the GC executes more than the time, the direct end;
2) Call Invoke_nopolicy first
2.1 First check is not to try scavenge;
2.1.1 To space must be empty and return false if not empty;
2.1.2 Gets the average size of all minor GC advance to old, and compares the size of the current eden+from used to take a smaller value, such as the Laosheng generation of the remaining space is less than this value, then returns FALSE, if greater than true;
2.2 If you do not need to try scavenge, then return false, otherwise continue;
2.3 Multi-thread scan live object, and base on the copying algorithm recovery, recycling the corresponding promotion object to the old generation;
2.4 As Useadaptivesizepolicy, then recalculate the values of to space and tenuringthreshold and adjust.
3) If Invoke_nopolicy returns False, or before all minor GC is promoted to the average size of the Laosheng generation > The remaining space of the old generation, then proceed to the following steps, otherwise end;
4) If USEPARALLELOLDGC, execute psparallelcompact, if not USEPARALLELOLDGC, then execute psmarksweep.

Laosheng generation parallel CMS GC:

Advantages and Disadvantages

1) Most of the time and the application concurrent, so it will only cause a very short pause time;
2) floating garbage, no way, so the memory space to a little bit larger;
3) memory fragments,-xx:+usecmscompactatfullcollection to solve;
4) Scramble for the CPU, this GC way;
5) Multiple remark, so the total GC time will be longer than the parallel;
6) Memory allocation, free list mode, so performance is slightly poor, the minor GC will have a little impact;
7) and application concurrency, it is possible to allocate and recycle at the same time, generate competition, introduce a lock, JVM allocation takes precedence.

11. Explanation of Tlab

The object data within the heap is shared by each thread, so when a new object is created inside the heap, a lock operation is required. Lock operations are time-consuming, so the JVM allocates a "private plots"--tlab on the heap for each line (full name is thread Local Allocation Buffer), which is located in the new generation of heap memory, the Eden zone. Each thread, when creating a new object, first tries to allocate it in its own tlab, and if it succeeds, it fails and then goes to the shared Eden area to apply for space. The thread's own Tlab zone. Failure to create an object generally has two reasons: one is the object is too large, and the other is not enough space in the Tlab area. Typically, the default Tlab area is 1% of the Eden region, and of course it can be manually adjusted, and the corresponding JVM parameter is-xx:tlabwastetargetpercent.

Exploring Java Virtual machines-memory management and garbage collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.