JVM garbage collection and six kinds OOM

Garbage Collection

Common garbage collection algorithm

Reference count

  • Disadvantages:
    • Each time the object was assigned to maintain the reference counter, and the counter itself has a certain consumption.
    • More difficult to deal with circular references
  • JVM implementations generally do not use such a manner

copy

  • From the perspective of Java heap GC may also be subdivided into: New Generation (Eden area, From Survivor To Survivor region and region) and elderly stay.
  • Advantages: no memory fragmentation (because the overall copy)
  • Disadvantages: relative waste of space, time consuming

MinorGC process (copy -> Empty -> interchange)

  • eden, SurvivorFrom copied to SurvivorTo, ages +1
    • When the Eden area is full when the GC will trigger the first time, the object was still alive copied to SurvivorFrom zone when Eden District trigger GC again scans From Eden district and area between the two areas for garbage collection
    • After this recovery still alive object is copied directly into the To area (if the object has reached the standard age of old age, then assigned to the district's old), while the age of the objects +1
  • Empty edeb, SurvivorFrom
    • Empty Eden and SurvivorFrom the objects, that is, after the exchange replication, who who is to empty
  • SurvivorTo and SurvivorFrom swap
    • SurvivorTo and SurvivorFrom swap, the original SurvivorTo become SurvivorFrom area at the next GC.
    • Part of the object will be copied in the From and To regions to be copied, so the exchange 15 (MaxTenuringThreshold determined by the JVM argument, the default is 15), and finally if you still alive, it is stored in the old era.

Clear labeling

  • Garbage collection algorithms - clearly marked Act (Mark-Sweep): mark and sweep algorithm is divided into two stages, first mark the object to be recovered, then unified recovery of these objects.
  • Advantages: No large area replication objects, saving memory space
  • Cons: memory fragmentation

Tags to organize

  • Mark - Compression (Mark-Compact)
  • principle:
    • Mark (Mark): the mark - as cleared.
    • Compression (Compact): re-scanning, and to the end of the sliding live objects.
  • Advantages: no memory fragmentation, you can use bump
  • Costs need to move objects: Disadvantages

GC Roots

How to determine whether an object can be recovered

  • Reference counting

    • In Java, references and objects are related. If you want to operate the object must be referenced.
    • Thus, a simple way is by reference counting to determine whether an object can be recycled. I.e., add a reference to the object count, whenever a reference fails, the counter value by one.
    • Any time counter value 0 is no longer subject to be utilized, then the object is recyclable objects.
    • Mainstream Java virtual machine inside did not choose this algorithm is that it mutually circular references between objects is difficult to solve the problem.
  • Enumeration root do reachability analysis (root search path)

    • In order to resolve the reference cycle counting method reference problem, Java method using reachability analysis.
    • The so-called "GC roots" or tracing GC "root collection" is a group to be active reference.
    • The basic idea is through a series of objects called "GC Roots" as a starting point, starting from the object is called GC Roots search down, if an object is not connected to the GC Roots any reference to the chain, then this object is not available . I.e. a reference to a given set of starting as the root, traverse the object by reference to FIG relationship, to be traversed (reachability) object is determined to survive, not to be traversed is judged to be dead.

GC Roots can be used as an object

  • Virtual Machine stack (local variables in a stack frame, also called the local variable table) referenced object.
  • The method of the referenced object region class static properties.
  • The method of constant reference object region
  • Object native method stack JNI (Native Method) references.

Four main types of garbage collector

Serial garbage collector (Serial)

  • Serial (new generation), SerialOld (years old)
  • It is designed as a single-threaded environment and uses only one thread garbage collection will suspend all user threads. It is not suitable for server environments.

Parallel garbage collector (Parallel)

  • Parallal (new generation), ParNew (new generation), Parallal (years old)
  • Multiple threads in parallel garbage collection, then the user thread is suspended for scientific computing / processing platform for big data processing and other weak interaction scenarios.

Concurrent garbage collector (CMS)

  • ConcMarkSweep (years old)
  • User threads simultaneously executed and garbage collection thread (not necessarily parallel, may be alternately performed), the user does not need to pause thread, using the response time required by the scene.
  • Recovery step
    • Initial labels: GC Roots mark object can only directly linked to the
    • Concurrent mark: be GC Roots Tracing process
    • The final mark: correction during concurrent mark, because the program is running that led to marked changes in the part of the object
    • Concurrency Recovery: user threads and the alternately performed together

GI garbage collector

  • Previously collected Features

    • Years old and the young generation is independent and contiguous block of memory
    • The young generation collection using a single eden + S0 + s replication algorithm
    • Old's collection to scan the entire area of ​​the old year
    • They are to be performed quickly and GC as little as possible for the design principles.
  • G1 (Garbage-First) collector, the collector is designed for server applications, application in multi-processor and large memory environment, while achieving high throughput, as far as possible to meet the garbage collection pause time requirements . In addition it also has features:

    • Like CMS as the collector can be executed concurrently with application threads.
    • Space Space finishing faster
    • More time is needed to predict the GC pause time
    • We do not want to sacrifice a lot of throughput performance
    • It does not require a larger Java Heap
  • G1 collector design goal is to replace CMS collector, which compared with the CMS, expressed in terms of better:

    • G1 is a garbage collector memory consolidation process, does not produce a lot of memory fragmentation.
    • G1's Stop The World (STW) more controllable, G1 adds prediction mechanism on the dwell time, the user can specify a desired dwell time.
  • The underlying principle

    • G1 stack algorithm is divided into a plurality of regions (Region), it still belongs to the generational collector
    • Region regionalization garbage collector, G1 will be the new generation, the physical space of the old era canceled.
    • Memory Region dicing regionalization, compiled for the entire series of discrete memory areas, to avoid a full memory GC.
      • On the heap, G1 does not require storage object must be physically contiguous to the long logically contiguous
      • Each partition will not be fixed for a generation services, demand can switch between the old and the young generation's.
      • Start by parameters -XX: G1HeapRegionSize = n can be specified partition size (1MB-32MB, and must be a power of 2), by default the entire stack is divided into 2048 partitions. Range in size from 1MB-32MB, provided up to 2048 regions. That it can support maximum memory: 32MB * 2048 = 65536MB = 64G of memory.
    • The core idea is the entire heap memory area is divided into the same size of the sub-region (Region), it will automatically set the size of these sub-regions at startup JVM.
  • Region contains the type of

    • The new generation, a new generation of garbage collection is still using the application threads to suspend all the way, the live objects are copied to or Survivor's old space.
    • Years old, G1 collector by copying objects from one area to another to complete the cleanup. This means that in the normal process, G1 completed stack of compression (or at least parts of the heap compression), so there would be no problem of memory fragmentation CMS.
    • Humongous (huge) area. If an object exceeds the space occupied by more than 50% of the partition size ,, G1 collector will think this is a giant object. These giant objects are assigned by default directly in the old time, but if it is a short-lived massive objects, it will have a negative impact on the garbage collector. To solve this problem, G1 divided a Humongous area, which is used specifically to store huge objects. If a H area fit a giant object, then the G1 will look for consecutive H partition to store. In order to be able to find a continuous H area, and sometimes have to start Full GC.
  • Recovery step

    • Young GC in G1 collector
      • Be collected for the Eden area, it will be triggered after the Eden area is depleted, mainly small regional collections + form a contiguous block of memory to avoid memory fragmentation.

      • Data movement to a new area Eden Survivor areas, part of the data zone promoted to the Old.

      • Data movement to a new area Survivor Survivor areas, part of the data zone promoted to the Old.

      • Finally, Eden District to collect clean, the end of the GC, the user of the application to continue.

  • 4-step process

    • Initial labels: GC Roots mark object can only directly linked to the
    • Concurrent mark: be GC Roots Tracing process
    • The final mark: correction during concurrent mark, because the program is running that led to marked changes in the part of the object
    • Filter Recycling: recycling to maximize the value of time.
  • Common configuration parameters

    • -XX:G1HeapRegionSize=n
      • The size of the area G1 provided. Value is a power of 2, the range of 1MB to 32MB. Goal is to divide the region of about 2048 based on the smallest Java heap size.
    • -XX:MaxGCPauseMillis=n
      • The maximum GC pause time, this is a soft target, JVM will try (but not guarantee) stop less than this time.
    • -XX:InitiatingHeapOccupancyPercent=n
      • The number of occupied heap when he triggered GC, the default is 45
    • -XX:ConcGCThreads=n
      • GC number of concurrent threads used.
    • -XX:G1ReservePercent=n
      • Set aside as a percentage of free memory, in order to reduce the risk of the target space overflow, the default value is 10%
  • Advantages over and CMS

    • G1 does not produce memory fragmentation
    • That it can be precisely controlled to a halt. The collector is the entire heap (the new generation, the old generation) region is divided into a plurality of fixed size, each dwell time to allow the mobile phone according to the largest trash area.

Turn different garbage collector command

  • + XX: UseSerialGC: Serial (new generation) + SerialOld (years old)
  • + XX: UseParNewGC: ParNew (new generation) + SerialOld (years old)
  • + XX: UseParallalGC: Parallal (new generation) + ParallalOld (years old)
  • + XX: UseParallalOldGC: Parallal (new generation) + ParallalOld (years old)
  • + XX: UseConcMarkSweepGC: Parallal (new generation) + CMS (years old) + SerialOld (emergency exits)
  • + XX: UseG1GC: G1 (new and old)

Six kinds OOM

java.lang.StackOverFlowError

  • Solution
    • The virtual stack machine is large:-Xss1024k

java.lang.OutOfMemoryError:Java heap space

  • Solution
    • The larger heap memory:-Xmx2g -Xms2g

java.langOutOfMemoryError:Metaspace

  • Solution
    • The yuan larger memory space:-XX:MetaSpaceSize=256m -XX:MaxMetaSpaceSize=512m

java.lang.OutOfMemoryError:unable to create new native thread

  • The native thread associated with abnormalities corresponding platform
  • High concurrent requests a server, often following exception: java.lang.OutOfMemoryError: unbale to create new native thread
  • Cause:
    • Application creates a thread too, an application process creates multiple threads, the system load exceeds the limit.
    • The server does not allow applications to create so many threads, linux system default number of threads to allow a single process can create is 1024, if the application creates more than this number, will be reported java.lang.OutOfMemoryError: unable to create new native thread
  • Solution:
    • To find ways to reduce the number of threads to create applications to analyze whether the application really need to create so many threads, if not, change the code to minimize the number of threads.
    • For some applications really need to create multiple threads, far more than linux system default limit of 1024 threads can be configured by modifying the linux server, expand linux default limit.

java.lang.OutOfMemoryError:Direct buffer memory

  • Cause:

    • NIO ByteBuffer often used to write programs to read or write data, which is based on the channel (Channel) and a buffer (Buffer) in the I / O mode, it can use Native libraries directly outside the heap memory allocated, then a DirectByteBuffer objects stored in the Java heap inside this memory as a reference for operation. This can significantly improve performance in some scenarios, it avoids the replicate data in the heap and the heap java Native.
    • ByteBuffer.allocate (capability) The first way is to allocate JVM heap memory, it falls within the jurisdiction of GC, since it is relatively slow to be copied.
    • ByteBuffer.allocateDirect (capability) The first way is to allocate local memory OS, not within the jurisdiction of GC, since no memory copy, the speed is relatively fast
    • However, if the continuously allocate memory, heap memory is rarely used, the JVM does not need to perform GC, DirectByteBuffer objects they will not be recovered, this time enough heap memory, but the local memory may have used the light, attempts to allocate local memory on again there will be OutOfMemoryError, that program directly collapsed.
  • Solution

    • The local memory is large:+XX:MaxDirectMemorySize=1g

java.lang.OutOfMemoryError:GC overhead limit exceeded

  • GC recovery will throw OutOfMemoryError long time. Is defined too long, more than 98% of the time used for GC and recovered less than 2% of the heap, GC repeatedly recovered only at less than 2% of the extreme case will be thrown.

  • GC overhead limit hypothesis does not throw an error occurs GC clean up memory will soon fill up again, forcing the GC to perform again, thus forming a vicious cycle, CPU usage always 100%, while the lack of GC without any results.

Published 109 original articles · won praise 47 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43934607/article/details/104392422