sequence

Just joined the work please ignore, obscure things, with the increase of wrinkles on the face, dim eyes, hair reduction will slowly know the nature. The version is old and outdated, but now that Java has been updated to 16, G1 is also outdated.

Jvm memory allocation

Stack allocation

Save parameters, local variables, intermediate calculations, and other data. When exiting the method, changing the top pointer destroys the contents of the stack frame. Stack advantage: access speed is relatively fast, only second to the register, stack data can be shared. Disadvantages of stacks: stack size and lifetime are determined at compile time, resulting in lack of flexibility.

Heap allocation

The advantages of heap: dynamic allocation of memory space, lifetime is dynamically allocated at runtime, garbage collection will automatically take away the space area no longer used. Disadvantages of the heap: The runtime has to allocate memory, which takes time to allocate and destroy, so it is inefficient.

JVM heap configuration parameters

-xms Initial heap size -Xmx Maximum heap size You are advised to set -xms = -xmx Total heap size = Young generation size + Old generation size + Persistent generation size

Jvm memory partitioning

The JVM Cenozoic

  • New generation = 1 Eden region + 2 Survivor region
  • -XMN young generation size (1.4 or lator)

-xx :NewSize, -xx :MaxNewSize (set young generation size (for 1.3/1.4)) default size is 3/8 of the entire heap

  • -xx :NewRatio Ratio of young generation (including Eden area + 2 Survivor areas) to old generation (excluding old generation)

If Xms=Xmx and Xmn is set, this parameter does not need to be set

  • -XX.SurvivorRatio

If the size ratio of Eden zone to Survivor zone is set to 8, the ratio of two Survivor zones to one Eden zone is 2:8, and one Survivor zone accounts for 1/10 of the expected size of the whole year

  • Used to hold Java objects just allocated by the JVM

Java old s

  • Old age = whole heap – young generation size – persistent generation size
  • Objects in the young generation that are not collected through garbage collection are copied to the old generation
  • The older generation stores objects that are much older than the younger generation
  • New objects can also be brought directly into the old age
    • Large objects, can through the launch parameters – XX: PretenureSizeThreshold = 1024 (unit of byte, the default 0) to represent more than how many not in Cenozoic era, when in old age distribution directly
    • Large array object, cut out the array without reference to the external object
  • The age size has no configuration parameter

Java last generation

  • Persistent generation = whole heap – young generation size – old age size
  • -XX:PermSize -XX:MaxPermSize

Set the size of the persistent generation. In general, it is recommended to set the values of -xx :PermSize and -xx :MaxPermSize to the same value, because the persistent generation size adjustment will cause the heap village to start FGC

  • Store Class and Method meta information, the size of which depends on the size of the project, the number of classes and methods. Generally, set it to 128, which is sufficient. The setting principle is to reserve 30% space
  • Recycle method of persistent generation
    • Constant, useless class information
    • For garbage collection, you must ensure the following three things:
      • All instances of the class have been reclaimed
      • The loading ClassLoader has been reclaimed
      • The Class object of the Class object is not referenced (that is, there is no place where the Class is referenced by reflection)

I don’t know who said 360 garbage

360 Locating garbage

  • Reference calculation algorithm

Each object has a reference count attribute and the count increases by 1 when a new reference is added, the count decreases by 1 when the reference is fortified, and the count can be recycled when the count is 0. This method is simple and cannot solve the problem of circular reference between objects and accurately calculate the count times.

  • The GC Roots algorithm

The search path is called reference chain. When there is no reference chain between an object and GC Roots, it is proved that the object is unavailable and unreachable. In Java,GC Roots include: Static familiarity with entity reference objects – constant reference objects in method area – Local method stack JNI reference objects

360 Clean up trash

  1. Copying algorithms
  2. Mark-sweep algorithm
  3. Mark-compac Algorithm

Noun explanation

Parallel collector is a collection where multiple GC threads are working in Parallel, but the user threads are suspended, so Serial is Serial, Parallel collector is Parallel, The CMS collector is a concurrent collection of concurrent user threads and GC threads executing at the same time, not necessarily concurrently, but alternately, but generally at the same time, without the need to pause the user thread (in CMS, the user thread does pause, just very briefly, and the GC thread executes on another CPU).

Garbage collector

Serial collector (Serial collector)

A single-threaded collector can only use one CPU or one county to complete garbage collection; While garbage collection is in progress, all other worker threads must be paused until the collection is complete

Disadvantages: Stop – The – World

Advantages: simple, for THE CPU situation, because there is no multi-threaded interaction overhead, but can be more efficient, is a Client mode under the default new generation collector

Next-generation Serial recycler

  1. – XX: UseSerialGC open

    Serial New+ Serial Old collector combination for memory collection

  2. Using a copy algorithm

  3. Exclusive garbage collection

    One thread GC, serial. Other worker threads are paused

Old age Serial collector

  1. – XX: UseSerialGC open

    Serial New+ Serial Old collector combination for memory collection

  2. Use a tag compression algorithm
  3. Serial, exclusive garbage collection

    Due to large memory, the recycling speed is slower than the new generation

ParNew payback period (parallel collector)

The parallel collector is an exclusive collector. During the collection process, all applications will be paused. However, because the parallel collector uses multithreading to collect garbage, it generates a very short serial collection period on the CPU with strong concurrency, but in the system with single CPU or weak concurrency, A parallel collector does not perform better than a serial collector, and it may perform worse than a serial collector due to the pressures of multiple threads

The New generation Parallel Exploiter

  1. Throughput optimized collector

    Focusing on CPU throughput, i.e., the time it took to run user code/total time, for example, if the JVM runs for 100 minutes with 99 minutes of user code running and 1 minute of garbage collection, the throughput is 99%. This is the collector that makes the most efficient use of the CPU for running background computing

  2. Open – XX: + UseParallelGC

    Recycle garbage using the Parallel Scavenge+Serial Old collector, which is also the default in Server mode

  3. -XX:GCTimeRatio

    Set the ratio of user execution time to total time. The default is 99, which is 1 minute for garbage collection

  4. -XX:+MaxGCPauseMillis

    Set the maximum GC pause time

  5. Using a copy algorithm

CMS (Concurrent token Cleanup) collector

The operation process is divided into four stages

  • CMS Initial Mark: Marks objects to which GC Roots can be directly associated
  • CMS Concurrent Mark: The process of GC RootsTracing
  • CMS remark: Corrects the marking of the part of the object that is changed because the user program continues to run during concurrent marking
  • CMS Concurrent sweep

The marking and re-marking phases require stop-the-world, and The longest time of The whole process is The concurrent marking and concurrent clearing process collector, both of which can work with The user thread at The same time

  1. Mark-clear algorithm

    It is also a garbage collector that uses multiple threads for concurrent collection

  2. -XX:ParallelCMSThreads

    Manually set the number of threads in the CMS. By default, the number of threads in the CMS is (ParallelCMSThreads+3)/4

  3. – XX: UseCONCmARKSweepGC open

    The combination of the ParNew+CMS+Serial Old collector is used for memory reclamation, and Serial Old is used as a backup collector for CMS in the event of a “Concurrent Mode Failure”

  4. -XX:CMSInitiatingOccupancyFraction

    Set the CMS collector after the years LaoDai space is how much triggers garbage collection, the default is 68%, only valid at the time of the CMS collector, – XX: CMSInitiatingOccupancyFraction = 70

  5. -XX:+UseCMSCompacAtFullCollection

    Since the CMS collector generates fragmentation, this parameter sets whether a memory defragmentation process is required after the garbage collector. This parameter is valid only for the CMS collector

  6. -XX:+CMSFullGCBeforeCompaction

    Set the CMS garbage collector in several times after collection memory defragmentation process, often used with UseCMSCompactAtFullCollection parameters

  7. -XX:CMSInitiatingPermOccupancyFraction

    Set the percentage of Perm Gen used to start, default is 92%

GC performance Index

  • Throughput The percentage of time an application spends on non-GC
  • GC load, as opposed to throughput, is the percentage of time an application spends on GC
  • Pause time The amount of time The application spends in GC stop-the-world
  • The GC frequency
  • The response rate changes from an object to garbage, the time that the object is collected

An interactive application requires as little pause time as possible, and a non-interactive application requires as little GC load as possible. A real-time system requires as little pause time and GC load as possible. An embedded system requires as little Footprint as possible

Rules for configuring memory capacity

  • Young generation size selection
    • Response time first references are set as large as possible until they approach the system’s minimum response time limit, in which case young generation collections occur with minimal frequency, reducing the number of objects that reach the old age

    • Throughput first applications, as large as possible, can reach the degree of Gbit, because there is no response time requirements, garbage collection can be concurrent, generally with 8 CPU or more applications

    • Avoid setting too small, which may lead to frequent YGC. If the YGC object directly enters the old age, FGC will be triggered when the old age is full

  • Aged generation size selection
    • Response time priority applications: the elderly use the concurrent collector, so its size needs to be set, generally consider the concurrent session rate and session duration and other parameters; If the heap setting is small, it can be reloaded with memory fragmentation, high recycle frequency, and application pausing instead of traditional token cleanup. If the heap is set up large, the collection time can be slow
    • Response time priority, concurrent garbage collection information, persistent generation concurrent collection times, traditional GC information, pay attention to the ratio of young generation to old generation collection time
    • Throughput optimization, generally throughput margin references are a large young generation and a small old generation, because it is possible to reclaim most of the short-term monopolies and reduce the mid-range objects, while the old generation tries to store the long-lived objects.

other

This article should be of no use to you. Give me a chance and I’ll update the latest one next time.

If you find this article helpful, please like it and share it with us. If you are interested, you can also browse the author’s other articles. If you think this article is a waste of your time, please leave your comments in the comments section. If you have any questions, please leave a message. The author is willing to spend time and energy to find answers and discuss them together.