JVM garbage collection summary (7): Tuning Methods

2010-08-26  来源:本站原创  分类:Java  人气:189 

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

JVM garbage collection summary (7): Tuning Methods

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)

JVM garbage collection summary (7): Tuning Methods

View reactor type, the object information see: quantity, type, etc.

JVM garbage collection summary (7): Tuning Methods

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 monitor

JVM garbage collection summary (7): Tuning Methods

Thread Information Monitoring: The system number of threads.

Thread Status Monitoring: all threads are in what state

JVM garbage collection summary (7): Tuning Methods

Dump thread for more information: See the internal operation of the thread

Deadlock check

Hot Spots

JVM garbage collection summary (7): Tuning Methods

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


JVM garbage collection summary (7): Tuning Methods

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.

Stack Overflow

Exception: java.lang.StackOverflowError

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.

  • JVM garbage collection summary (7): Tuning Methods 2010-08-26

    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, n

  • JVM garbage collection summary (6): Perspectives on the GC characteristics of Java 2010-08-26

    The full text: http://topic.csdn.net/u/20091211/12/c79a9293-fdd5-4f81-b18f-a4a29c8d9be2.html Study: yang677888 (from CSDN) 1, using the System.gc () can be used regardless of what kind of JVM garbage collection algorithm, can request Java's garbage c

  • JVM garbage collection summary (8): Reflection and Reference 2010-08-26

    The Paradox of garbage collection The so-called "winner Xiao He Xiao He lost." Java's garbage collection have brought many benefits for the development is made easier. However, in some high-performance, high concurrency situation, garbage collec

  • JVM garbage collection summary (2): Basic Algorithms 2010-08-23

    1, reference counting collector (Reference Counting) Reference counting garbage collection is the early strategy. In this approach, each heap object has a reference count. An object is created, and points to the object reference is assigned to a vari

  • Summary of JVM garbage collection (collection) 2010-12-25

    JVM garbage collection Summary (1): some concepts Summary of JVM garbage collection (2): Basic Algorithms Summary of JVM garbage collection (3): Press on behalf of the garbage collector JVM garbage collection Summary (4): a new generation of garbage

  • [Change] JVM garbage collection points on behalf of the basic concepts of strategy 2010-05-05

    Article summarizes the JVM garbage collection policy points on behalf of why and how sub-generation, as well as garbage collection triggers. Why sub-Generation Sub-generation garbage collection strategy is based on this fact: the life cycle of differ

  • JVM garbage collection mechanism and GC Adjustment 2010-03-29

    JVM garbage collection mechanism and GC Adjustment 1, GC Summary: Knowledge of why the JVM heap, said first JVM heap? JVM is a Java object heap space for activities, procedures, objects in the class from which the allocation of space, its stores are

  • HotSpot JVM garbage collection principle 2010-06-05

    Transfer from: http://blog.chinaunix.net/u/15296/showart_83507.html Objective: This article describes the Sun's HotSpot Java virtual machine garbage collection works. Java lovers for more in the design, development and deployment more convenience and

  • Java's garbage collection summary and to learn Java garbage collection mechanism for experience 2008-06-09

    Memory is a scarce resource, even if memory, a money one! If the improper use of programming, then the memory will run out of big. First, understand Java's automatic garbage collection garbage collection is one of the major characteristics of Java la

  • Sub-generation of JVM garbage collection algorithm and OOM 2010-12-14

    Java memory types are divided into: Heap Permanent Stack One Heap -Xms <size> set initial Java heap size -Xmx <size> set maximum Java heap size Heap divided into: a. generation (young generation): Cenozoic has been divided into two: Eden Space

  • jvm garbage collection related to some of the links 2010-05-10

    Recently begun to seriously study the basis of his own lack of, from the most basic start. Made a ruthless to their own oath, can not let this go on their own, although I start low and secondary origin, the basis is not good, but the effort is still

  • JVM garbage collection 2011-04-28

    Must be close ResultSet and prepareStatement Otherwise Java's garbage collection will not be recovered. Cause memory leak http://blog.sina.com.cn/s/blog_48bc7aa9010006vs.html

  • JVM memory model, as well as garbage collection strategy for analysis 2010-03-29

    First of all I wish you all a happy Spring Festival, a few months before the study a bit JVM memory model, sorting learning experiences, sharing them with everyone to discuss progress in learning. A JVM memory model 1.1 Java Stack Java stack is assoc

  • Bad memory, memo. (JVM memory model and the garbage collection policy analysis) 2010-06-03

    JVM memory model and strategy analysis for a JVM garbage collection memory model 1.1 Java stack Java stack is associated with each thread, JVM is created each time a thread will be allocated a stack space for threads. It is mainly used to store threa

  • Flex garbage collection and performance optimization of a number of summary 2010-08-28

    Some knowledge of Flex garbage collection summary: 1, deleted all references to objects in the external must be removed as a clean garbage collection system can be disposed of. 2, the child within the parent object is an external reference other obje

  • Some knowledge of Flex summary of garbage collection 2011-05-27

    Some knowledge of Flex garbage collection summary: 1, deleted all references to objects in the external must be removed as a clean garbage collection system in order to be disposed of. 2, the parent child object is outside the internal references to

  • java garbage collection algorithm (change) 2010-04-29

    1. The core of garbage collection algorithm Java language to create a garbage collection mechanism to track the object being used and the discovery and recovery are no longer used (reference) object. This mechanism can prevent dynamic memory allocati

  • On Java, the garbage collection mechanism 2010-07-01

    1. Garbage collection is a dynamic storage management technology, which automatically releases the object reference is no longer procedure, according to a particular garbage collection algorithm to auto-recovery function of resources. When an object

  • JAVA garbage collection and memory leaks 2010-07-20

    Since: http://apps.hi.baidu.com/share/detail/9064906 1. The core of garbage collection algorithm Java language to create a garbage collection mechanism to track the object being used and the discovery and recovery are no longer used (reference) objec

  • A comprehensive analysis of Java's garbage collection mechanism 2010-01-29

    Introduction Java run-time heap is a data area, the class instance (object) from which the allocation of space. Java Virtual Machine (JVM) heap storage of the applications that are running all the objects created, these objects through the new, newar