JVM tuning tools
Jconsole, jProfile, VisualVM
Jconsole: jdk own, simple functions, but you can load in the system where a certain use. Garbage collection algorithm on a very detailed tracking. Detailed information here
JProfiler: business software, need to pay. Powerful. Detailed information here
VisualVM: JDK own, powerful, and JProfiler similar. Recommended.
How to tune
Observe memory release conditions set class inspection, the object tree
Above the tuning tools provide powerful, but generally function normally divided into the following categories
Heap information see
To see the heap space allocated (the young generation, old generation, sustained generation of distribution)
Provide real-time garbage collection
Waste Monitoring (recovery time monitoring the situation)
View reactor type, the object information see: quantity, type, etc.
Object reference case view
With the heap area information see the functions, we can successfully address the following general questions:
- The size of the elderly on behalf of the younger generation of classification is reasonable
- Memory leak
- Garbage collection algorithm to set reasonable
Thread Information Monitoring: The system number of threads.
Thread Status Monitoring: all threads are in what state
Dump thread for more information: See the internal operation of the thread
CPU hot spots: the method of checking system which occupied a lot of CPU time
Memory hot spots: check what the maximum number of objects in the system (some time living object and destroy object memory with statistics)
These two things very helpful for system optimization. We can find a hot, targeted search for system bottlenecks and for system optimization, rather than scattershot for all of the code optimization.
Snapshot of the system at some point run into a fixed grid. Zai us tuning of the time, not with the eyes to track All system changes, Yi Lai snapshot function, we can carry out Xitong two Butongyunxing time, Duixiang (or class, Xian Cheng, etc.) of different, Yipiankuaisu Zhaodao problem
For example, I want to check after the garbage collection system and whether there is the recovery of the object is missing then. Well, I can before and after carrying out garbage collection, respectively a heap snapshot of the situation, and then comparing the two snapshots of the object situation.
Memory leak check
Memory leaks are more common problem, but solutions are more generic, where you can focus on that look, and thread, hot issues is the analysis of specific issues.
Memory leaks can generally be understood as system resources (various resources, heap, stack, threads, etc.) used in the wrong circumstances, lead to the resource can not be recycled after use (or no recovery), resulting in new resource allocation request can not be completed , causing system errors.
Memory leak on the system against bigger, because he can cause the system to collapse.
Need to distinguish between what, memory leaks, and system overload is a difference between the two, although it may lead to the final result is the same. Memory leak is caused by running out of resources did not recall the error, and the system is overloaded system did not have enough resources to allocate the (other resources are in use).
Age on behalf of the heap space is filled
Exception: java.lang.OutOfMemoryError: Java heap space
This is the most typical way of a memory leak, it simply means that all heap are garbage collected object can not be filled, the virtual machine no longer in the allocation of new space.
As shown above, this is very typical of the garbage collector memory leak situation map. Peaks are all part of a garbage collection point, the bottom part of all that is left after a garbage collection memory. Connect all the bottom points, we can find a line from the bottom to high, indicating that, over time, the system heap space is continuously filled and eventually occupies the entire heap space. Preliminary view that it can within the system may have a memory leak. (The above map is only an example, in practice, the time needed to collect more data, such as hours or days)
This approach is easier to solve, the general situation is compared before and after garbage collection according to the same time, according to object reference situation (a common set of object references) analysis, the basic point can find a leak.
Persistent generation of already occupied
Exception: java.lang.OutOfMemoryError: PermGen space
Perm space is filled. Unable to allocate storage space for the new class caused the exception. This exception had never happened before, but extensive use of Java reflection of today's disorders are relatively common. Mainly because a large number of dynamically generated class reflection constantly is loaded, leading to Perm area was already occupied.
Even more frightening is that different classLoader even use the same class, but will be loaded on their equivalent of the same thing, if there are N-classLoader he will be loaded N times. Therefore, in some cases, as no solution to this problem basically. Of course, there are a lot classLoader and the situation where a large number of reflection classes are not many.
1.-XX: MaxPermSize = 16m
2. For use JDK. For example JRocket.
Description: This is not to say, is the general recursion no return, or call the resulting cycle
Full thread stacks
Exception: Fatal: Stack size too small
Description: java in a thread space is limited. JDK5.0 later this value is 1M. Data related to this thread will be saved in them. But when the thread is full of space after the above exception will occur.
Solution: increase the thread stack size. -Xss2m. However, this configuration can not solve the fundamental problem, depends on whether the code is part of the part caused by leakage.
System memory to be filled
Exception: java.lang.OutOfMemoryError: unable to create new native thread
This anomaly is due to the operating system does not have sufficient resources to produce the thread created. System to create threads, in addition to the Java heap to allocate memory, the operating system itself also needs to allocate resources to create thread. Therefore, when a large number of threads to a certain extent after the heap and perhaps space, but no resources allocated to the operating system, and the emergence of this anomaly was.
Allocated to the Java virtual machine more memory, less system resources remaining, therefore, when the system memory is fixed, assigned to the Java virtual machine more memory, then the system can produce a total of less thread, the two are inversely proportional relationship. At the same time, you can modify the-Xss to reduce the space allocated to a single thread, you can also increase the system produced a total number of threads.
1. To re-design systems to reduce the number of threads.
2. Thread can not reduce the number of cases, by-Xss reduce the size of a single thread. So as to produce more threads.