The garbage collector and tomcat tuning

Garbage collection

1、概述:垃圾回收机制,Java中的对象不再有“作用域”的概念,只有对象的引用才有“作用域”。垃圾回收可以有效的防止内存泄露,有效的使用空闲的内存。

Memory leak

内存泄漏的定义:对象已经没有被应用程序使用,但是垃圾回收器没办法移除它们,因为还在被引用着。该内存空间使用完毕之后未回收,在不涉及复杂数据结构的一般情况下,Java 的内存泄露表现为一个内存对象的生命周期超出了程序需要它的时间长度,我们有时也将其称为“对象游离

How to prevent memory leaks

    1、特别注意一些像HashMap、ArrayList的集合对象,它们经常会引发内存泄漏。当它们被声明为static时,它们的生命周期就会和应用程序一样长。
    2、特别注意事件监听和回调函数。当一个监听器在使用的时候被注册,但不再使用之后却未被反注册。
3、如果一个类自己管理内存,那开发人员就得小心内存泄漏问题了。” 通常一些成员变量引用其他对象,初始化的时候需要置空。

finalize action

Java技术使用finalize()方法在垃圾收集器将对象从内存中清除出去前,做必要的清理工作。这个方法是由垃圾收集器在确定这个对象没有被引用时对这个对象调用的。它是在Object类中定义的,因此所有的类都继承了它。子类覆盖finalize()方法以整理系统资源或者执行其他清理工作。finalize()方法是在垃圾收集器删除对象之前对这个对象调用的。

Garbage collection algorithm

  • Reference counting
    Overview: adding a reference to the object counter, whenever a reference to its place, the counter value is incremented by 1; when referring to the failure, the counter value is decreased by 1; any time counter are not the object is 0 is used, the garbage collector reclaims the memory used by the object.
  • Replication algorithm
    S0 and S1 by the available memory capacity is divided into two pieces of equal size, wherein one uses only when used over this memory, it will also copy live objects up to the other memory, and the used memory space once cleared away. Such that each time one of which do not consider the complexities of memory for memory debris during recovery, memory allocation, simply move the top of the stack pointer in order to allocate memory, simple, efficient operation. Copy the disadvantage of the algorithm is obvious that memory can be reduced to half the original use.
  • Sweep algorithm marker
    flag - Clear (Mark-Sweep) algorithm name suggests, mainly two actions, one marker, the other is clear.
    Tag is based on a specific algorithm (such as: reference counting algorithm, reachability analysis algorithm) indicated which objects in memory can be recovered, which will continue to use the object.
    Mark indicates the recovery, to close it off directly; flag indicates that the object can use, leave it standing still.
  • Mark - compression algorithm
    marked compression method on the basis of clear labeling is optimized, the survival of compressed objects into memory at one end, and then clean up the garbage. (java-Laos's use of the mark is the compression method)
  • Generational collection algorithm
    based on survival period objects in different memory, the memory is divided into a few pieces, java virtual machine memory is generally divided into the old generation and the new generation, the general allocation of memory space in the new generation when a new object is created when the new generation of recycling garbage collector after a few still live objects are moved to the old generation memory, but also directly created in the older generation when a large object can not find enough contiguous memory in the new generation. For years the new generation and the old, the new generation of high frequency of recovery, but each time is very short recovery time consuming, while lower frequencies old's recovery, but will be a relatively long time-consuming, so it should be to minimize the GC old age.

    The garbage collector

    Java garbage collector is a Java Virtual Machine (JVM) three important modules (the other two are interpreters and multithreading) one, providing automatic allocation of memory (Memory Allocation) for the application, automatic recovery (Garbage Collect) function, these two operations have taken place in the Java heap (a section of memory fast). A certain point in time, if an object has more than one reference (Rreference) pointing to it, then the object is to live (Live), or death (Dead), considered junk, can be recycled garbage collector. Garbage collection operation consumes CPU, threads, time and other resources, it is readily understood that the operation is not a real-time garbage collection occurs (release immediately the object of death), when the memory is consumed or reach a certain indicators (Threshold, total memory using memory than columns, such as 0.75), trigger a garbage collection operation. There are exceptions death of an object, the object type java.lang.Thread even without a reference, as long as the thread is still running, it will not be recovered.

    Serial collector (Serial Collector)

    Single-threaded implementation of a recovery operation, suspend all application threads of execution, the default collector in client mode during the recovery, by -XX: + UseSerialGC command-line option to specify mandatory.

    Parallel collector (ParNew collector)

    Parallel recovery in a serial collector on the basis of improvements have been made, he can use multiple threads for garbage collection, for computer strong computing power, can effectively shorten the sharp interannual time required for garbage collection.
    ParNew recovery is a work in the new generation of the garbage collector, he simply serial collector multithreaded his quick recovery strategies and algorithms and the same serial collector. Use XX: + UseParNewGC new generation ParNew collector.

    Parallel return collector (ParallelGC)

    ParallelOldGC years old collector is also a multi-threaded collector, and the new generation of ParallelGC collector as also an off toward the throughput collector, he used the label compression algorithm implementation.
    -XX: + UseParallelOldGC set
    -XX: + ParallelCThread can also set the amount of threads to teach garbage collection.

    Test configuration tuning Tomcat

  • Jmeter stress testing tool
  • Tuning summarizes
    the initial value and the maximum heap memory heap memory is larger, the higher the throughput.
    Parallel collector is preferably used, because the speed is higher than the parallel phone serial certain speed.
    The proportion of the new generation ratio set heap memory's old and preferably 1: 2 or 1: 3.
    GC reducing the recovery of old age.

Guess you like

Origin www.cnblogs.com/Libbo/p/11470555.html