"declaration"
Welcome reprint, but please keep the original source of the article →_→
Life One number: http://www.cnblogs.com/smyhvae/
Article Source: http://www.cnblogs.com/smyhvae/p/4736162.html
Contact information: [email protected]
The main content of this article:
- Trace Trace Parameters
- Allocation parameters of the heap
- Allocation parameters for stacks
0. Print GC logs in the background of the IDE:
Since learning the JVM, reading the GC log is a fundamental skill in dealing with the memory problems of Java virtual machines, it is only a set of rules that are artificially determined, and not much technical content.
That being the case, it is essential to print GC logs in the IDE's console. I'll tell you how to print it now.
(1) If you are using Eclipse, print the GC log as follows:
Add -xx:+printgcdetails to the arrow at the top of the line. Then, after running the program, the GC log can be printed out:
(2) If you are using IntelliJ idea, print the GC log as follows:
Add -xx:+printgcdetails to the arrow at the top of the line. Then, after running the program, the GC log can be printed out:
Of course, the light has -xx:+printgcdetails This sentence parameter certainly is not enough, below we detailed introduce more parameter configuration.
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]
[GC 4790k->374k (15872K), 0.0001563 secs]
[GC 4790k->374k (15872K), 0.0001682 secs]
Above the log means that, before the GC, using about 4M of memory, GC, after the use of 374K 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 CG 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]
The above log 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).
Question: assigned to the new generation has 15M, but only 13824K available, why this difference? After we learned the GC algorithm in the later article, we understand.
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]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type 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 New byte [1 * 1024x768 * 1024x768]; 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 New byte [Ten * 1024x768]; 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)
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:
Question 1:-xmx (maximum heap space) and –XMS (minimum heap space) should maintain a relationship, so that the system can perform as well as possible.
Question 2: If you want to do a Java desktop product, you need to bind the JRE, but the JRE is very large, how do you do the JRE of the thin body?
2,-xmn,-xx:newratio,-xx:survivorratio:
Set Cenozoic size
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 of 1/10 of the younger generation
Now run the following code:
Public class javatest { publicstaticvoid main (string[] args) { byte null; for (int i = 0; i < ten; i++) New byte [1 * 1024x768]; }}
We look at the difference between GC logs by setting different JVM parameters.
(1) When the parameters are set as follows: (Set the Cenozoic to 1M, very small)
-xmx20m-xms20m-xmn1m-xx:+printgcdetails
Operating effect:
Summarize:
No GC triggered
Because the new generation of memory is relatively small, so all allocated in the old age.
(2) When the parameter is set as follows: (Set the Cenozoic to 15M, large enough)
-xmx20m-xms20m-xmn15m-xx:+printgcdetails
Operating effect:
Show:
No GC triggered
All allocated in Eden (shown in blue box)
Not used in the old age (shown in the red box)
(3) When the parameters are set as follows: (Set the Cenozoic to 7M, moderate)
-xmx20m-xms20m–xmn7m-xx:+printgcdetails
Operating effect:
Summarize:
2 New Generation GC
S0 S1 is too small to warrant the old age
(4) When the parameters are set as follows: (Set the Cenozoic to 7M, small and moderate, and increase the surviving generation size)
-xmx20m-xms20m-xmn7m-xx:survivorratio=2-xx:+printgcdetails
Operating effect:
Summarize:
Has performed at least 3 new generation GC
S0 S1 Increase
(5) When the parameter is set to the following:
-xmx20m-xms20m-xx:newratio=1-xx:survivorratio=2-xx:+printgcdetails
Operating effect:
(6) When the parameter is set to the following: as compared to the above (5), the appropriate reduction of the surviving generation size, so that the number of GC can be reduced
-xmx20m-xms20m-xx:newratio=1-xx:survivorratio=3-xx:+printgcdetails
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.
Exporting an Oom path
For example, we set the following parameters:
-xmx20m-xms5m-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=d:/a.dump
Above 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:
New Vector (); for (int i = 0; i <; i++) V.add (newbyte[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.
Note: For the use of VISUALVM, you can refer to the following blog:
Performance analysis and Tuning with VisualVM: http://www.ibm.com/developerworks/cn/java/j-lo-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:
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:
// P represents the PID of the current process
The above parameter 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 be generated in D:/a.txt when the program is Oom The dump of the thread .
5. Summary of the allocation parameters of the heap:
- Adjust the size of the Cenozoic and the surviving generations according to the actual matter
- Official recommendation 3/8 of the Cenozoic Jian
- Surviving generations accounted for 1/10 of the Cenozoic
- when you're in oom, remember to dump out the heap to make sure you can troubleshoot the site.
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++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 classTeststackdeep {Private Static intCount = 0;
Public Static voidRecursion (LongALongBLongc) {LongE = 1, f = 2, G = 3, H = 4, I = 5, k = 6, q = 7, x = 8, y = 9, z = 10; Count++; Recursion (A, B, c); }
Public Static voidMain (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.
Summarize:
We describe some of the most basic parameters of the JVM in this article, as well as a number of parameters, such as GC parameters, that will be covered in a subsequent series of articles. We'll cover the GC algorithm in the next article.
Java Virtual Machine Detailed----Common JVM configuration parameters