JVM Common parameter Configuration

Source: Internet
Author: User
Tags xms visualvm

  • Trace Trace Parameters
    • -VERBOSE:GC  -XX:+PRINTGC  Print GC Brief information
    • -xx:+printgcdetails  Print GC details
    • -xx:+printgctimestamps  Print CG time stamp occurs
    • -xloggc:log/gc.log  Specify the location of the GC log for file output
    • xx:+traceclassloading  loading of monitoring classes
    • -xx:+printclasshistogram  Press Ctrl+break to print information about the class
  • Allocation parameters of the heap
    • -xmx–xms specifying the maximum heap and minimum heap
    • -xmn Setting the Cenozoic size
    • the ratio of-xx:newratio Cenozoic (eden+2*s) and older generation (not including the permanent region).  For example: 4, the new generation: the old age of =1:4, that is, the Cenozoic 1/5 the whole heap
    • -xx:survivorratio (surviving generations) set the ratio of two survivor area to Eden For example: 8, representing two survivor:eden=2:8, that is, a survivor for the younger generation of 1/10
    • -xx:+heapdumponoutofmemoryerror   Oom When exporting heaps to a file according to this file, we can see what happens when the system dumps.
    • -xx:+heapdumppath Exporting an Oom path
    • -xx:onoutofmemoryerror executes a script when it is oom.  You can send mail or even restart the program when you are oom. 
    • -xx:permsize-xx:maxpermsize sets the initial space and maximum space for the permanent zone. In other words, when the JVM starts, the permanent zone occupies the permsize size space at the beginning, and if the space is not enough, it can continue to expand, but cannot exceed maxpermsize, otherwise it will be oom.
  • Allocation parameters for stacks
    •  -xss128k sets the size of the stack space. Usually only hundreds of K  determines the depth of the function call each thread has a separate stack space local variable, the parameter is allocated on the stack

Print GC logs in the background of the IDE:

(1) If you are using Eclipse, print the GC log as follows:

(2) If you are using IntelliJ idea, print the GC log as follows:

One, trace trace parameters:

1. to print a GC's brief information:

-verbose:gc-xx:+printgc

Explanation: You can print brief information about a GC. Like what:

[GC 4790k->374k (15872K), 0.0001606 secs]

[GC 4790k->374k (15872K), 0.0001474 secs]

This means that, before the GC, using about 4M of memory, after the GC, with 374K of memory, a total of nearly 4M recovered. The memory size is about 16M altogether.

2. Print GC Details:

-xx:+printgcdetails

Explanation: Print GC details.

-xx:+printgctimestamps

Explanation: Prints the timestamp of the GC occurrence.

Understand the meaning of the GC log:

For example, the following log:

[Gc[defnew:4416k->0k (4928K), 0.0001897 secs] 4790k->374k (15872K), 0.0002232 secs] [times:user=0.00 sys=0.00, real=0.00 secs]

That means: This is a new generation of GC. Inside the square brackets "4416k->0k (4928K)" means: "The memory area before the GC has used capacity->GC the memory area has used capacity (total capacity of this memory area)". The "4790k->374k (15872K)" Outside the square brackets means "the Java heap has used capacity (total Java heap capacity) since the Java Heap has used capacity->GC before GC." Looking back, "0.0001897 secs" indicates the time taken by the GC in the memory area, in seconds.

Another example is the following GC log:

, let's take a look at the meaning of "[0x27e80000, 0x28d80000, 0x28d80000)" marked with a red box, which represents the position of the Cenozoic in memory: The first parameter is the starting position to apply to, the second parameter is the end position of the application, The third parameter represents the maximum number of locations that can be requested.

The example in the new generation applies to the 15M control, and this 15M is equal to: (Eden Space's 12288K) + (from Space's 1536K) + (to Space's 1536K).

3. Specify the location of GC log:

-xloggc:log/gc.log

Explanation: Specifies the location of the GC log for file output . Help developers analyze issues.   

-xx:+printheapatgc

Explanation: The heap information is printed every time before GC and after GC.

For example:

, the Red box section is exactly a GC, the Red Box section is preceded by the GC log, and the Red Box section is followed by a post-GC log.

-xx:+traceclassloading

Explanation: The load of the monitoring class.

For example:

[Loaded java.lang.Object from shared objects file]

[Loaded java.io.Serializable from shared objects file]

[Loaded java.lang.Comparable from shared objects file]

-xx:+printclasshistogram

Explanation: After you press Ctrl+break, print the information for the class.

For example:

Second, the allocation parameters of the heap:

1,-xmx–xms: Specify the maximum heap and minimum heap

For example , when the parameter is set to the following:

-xmx20m-xms5m

Then we run the following code in the program:

System.out.println ("xmx=" + runtime.getruntime (). MaxMemory ()/1024.0/1024 + "M");     Maximum space of the system
System.out.println ("Free mem=" + runtime.getruntime (). Freememory ()/1024.0/1024 + "M"); Free space for the system
System.out.println ("Total mem=" + runtime.getruntime (). TotalMemory ()/1024.0/1024 + "M"); Total space currently available

Operating effect:

Keep the parameters intact and run the following code in the program: (Allocate 1M space to the array)

Byte[] B = new BYTE[1 * 1024 * 1024]; System.out.println ("Allocated 1M space to the array");
System.out.println ("xmx=" + runtime.getruntime (). MaxMemory ()/1024.0/1024 + "M"); Maximum space of the system
System.out.println ("Free mem=" + runtime.getruntime (). Freememory ()/1024.0/1024 + "M"); Free space for the system
System.out.println ("Total mem=" + runtime.getruntime (). TotalMemory ()/1024.0/1024 + "M");

Operating effect:

Note: Java will maintain the value of total mem to the minimum heap whenever possible.

Keep the parameters intact and run the following code in the program: (Allocate 10M space to the array)

Byte[] B = new BYTE[10 * 1024 * 1024]; System.out.println ("Allocated 10M space to the array");
System.out.println ("xmx=" + runtime.getruntime (). MaxMemory ()/1024.0/1024 + "M"); Maximum space of the system
System.out.println ("Free mem=" + runtime.getruntime (). Freememory ()/1024.0/1024 + "M"); Free space for the system
System.out.println ("Total mem=" + runtime.getruntime (). TotalMemory ()/1024.0/1024 + "M"); Total space currently available

Operating effect:

As shown in the red box: At this point, total mem is 7M when it is unable to meet the requirements, and then the total mem rose to 16.5M.

Keep the parameters intact and run the following code in the program: (Perform a GC recycle)

Byte[] B = new BYTE[10 * 1024 * 1024];
System.GC ();
System.out.println ("xmx=" + runtime.getruntime (). MaxMemory ()/1024.0/1024 + "M"); Maximum space of the system
System.out.println ("Free mem=" + runtime.getruntime (). Freememory ()/1024.0/1024 + "M"); Free space for the system
System.out.println ("Total mem=" + runtime.getruntime (). TotalMemory ()/1024.0/1024 + "M"); Total space currently available

Operating effect:

2,-xmn,-xx:newratio,-xx:survivorratio:

    • -xmn

Set Cenozoic size

    • -xx:newratio

The ratio of Cenozoic (eden+2*s) and older generation (not including the permanent region)

For example: 4, the new generation: the old age =1:4, that is, the new generation accounted for the entire heap of 1/5

    • -xx:survivorratio (Surviving generations)

Set the ratio of two survivor area to Eden

For example: 8, representing two survivor:eden=2:8, that is, a survivor for the younger generation of 1/10

3,-xx:+heapdumponoutofmemoryerror,-xx:+heapdumppath

    • -xx:+heapdumponoutofmemoryerror

When you export a heap to a file in Oom

According to this file, we can see what happens when the system dumps.

    • -xx:+heapdumppath

Exporting an Oom path

For example, we set the following parameters:

-xmx20m-xms5m-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=d:/a.dump

This means that the heap memory is now allocated up to 20M of space. If an oom exception occurs, export the dump information to the D:/a.dump file.

We then execute the following code:

Vector v = new vector (), for (int i = 0; i < i++) V.add (New byte[1 * 1024 * 1024]);

In the above code, there is a need to take advantage of 25M of space, and it is obvious that an oom exception occurs. Now we run the program, the console prints as follows:

Now let's go to the D drive and look at the dump file:

Display, in general, the size of this file is consistent with the size of the maximum heap.

We can open this dump file with VISUALVM. Or the Java VISUALVM tool that comes with Java is OK:

Is the dump out of the file, the file can be seen, a total of 19 bytes have been assigned.

4,-xx:onoutofmemoryerror:

    • -xx:onoutofmemoryerror

Executes a script while in Oom.

You can send mail or even restart the program when you are oom.

For example, we set the following parameters:

-xx:onoutofmemoryerror=d:/tools/jdk1.7_40/bin/printstack.bat%p//p represents the PID of the current process

This means that the Printstack.bat script is executed, and this script does the following: D:/tools/jdk1.7_40/bin/jstack-f%1 > d:/a.txt, which will generate a thread in D:/a.txt when the program is Oom 's dump.

6, the Permanent Zone allocation parameters:

    • -xx:permsize-xx:maxpermsize

Sets the initial space and maximum space for the permanent zone. In other words, when the JVM starts, the permanent zone occupies the permsize size space at the beginning, and if the space is not enough, it can continue to expand, but cannot exceed maxpermsize, otherwise it will be oom.

They say how many types of a system can fit

code example:

We know that when using libraries such as cglib, there may be a lot of classes, these classes, which are likely to explode in the permanent zone causing oom. So, we run the following code:

for (int i=0;i<100000;i++) {Cglibbean bean = new Cglibbean ("Geym.jvm.ch3.perm.bean" +i,new HashMap ());}

The above code will continuously generate new classes in the permanent area. As a result, the operating effect is as follows:

Summarize:

If the heap space is not exhausted it also throws an oom, which can be caused by a permanent zone .

Heap space actually consumes very little, but a permanent overflow is thrown as an oom.

third, the allocation parameters of the stack:

1. XSS:

Sets the size of the stack space. Usually only hundreds of k

Determines the depth of a function call

Each thread has a separate stack space

Local variables, parameters allocated on the stack

Note: The stack space is the private area of each thread. The main content of the stack is the stack frame, and the stack frame is a local variable table, the contents of the local variable table are: local variables, parameters.

Let's take a look at the following code: (Recursive call without exit)

public class Teststackdeep {    private static int count = 0;
public static void recursion (long A, long B, long c) { long E = 1, f = 2, G = 3, H = 4, I = 5, k = 6, q = 7, x = 8, y = 9, z = ten; count++; Recursion (A, B, c); }
public static void Main (String args[]) { try { recursion (0L, 0L, 0L); } catch (Throwable e) { System.out. println ("deep of calling =" + count); E.printstacktrace ();}}}

The above code is a recursive call without an exit, and there will definitely be an oom.

If the stack size is set to 128k:

-xss128k

The effect is as follows: (method was called 294 times)

If the stack size is set to 256k: (method is called 748 times)

This means that the number of function calls is too deep, like this recursive invocation is a typical example.


















JVM Common parameter Configuration

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.