Garbage collection

If a Web application runs in a garbage collection environment such as JVM, garbage collection can have a huge impact on system performance. Understanding garbage collection mechanism helps program optimization and parameter tuning, as well as writing memory safe code.

Take JVM as an example, its memory can be mainly divided intoHeap (heap)andStack (stack)

  • The stack is used to store thread context information, such as method parameters, local variables, etc.

  • Heap is the memory space for storing objects, and the creation and release of objects and garbage collection are carried out here.

Observing the object lifecycle, we find that most objects have extremely short lifecycles, and that the garbage generated by these objects should be collected faster to free memory, which isJVMGeneration garbage collection,The basic principle is shown in the following figure.

In the JVM generation garbage collection mechanism, the heap space available for application can be divided intoYoung generation (Young Generation)andElderly generation (Old Generation),And divide the younger generation intoEdenareaFromareaandToarea

  • 1.New objects are always created in the Eden area.

  • 2.When the Eden area is full, it triggers once.Young GC(Garbage Collection,Garbage collection),Copy the objects that are also used to the From area so that the entire Eden area is unused space for continuing to create objects.

  • 3.When the Eden section runs out again, start the Young GC again to copy the objects that are still in use in the Eden section and the From section to the To section.

  • 4.The next Young GC is to copy the objects that are still used in the Eden and To areas to the From area.

  • 5.Therefore, after several times of Young GC, some objects will be duplicated in From and To areas, if more than one.thresholdBefore being released, the object is copied to Old Generation.

  • 6.If Old Generation space is used up, it will trigger.Full GC,The so-called full volume recovery.

Full recovery will have a greater impact on system performance, so we should set the size of Young Generation and Old Generation reasonably according to the system business characteristics and object life cycle, and minimize Full GC. In fact, some Web applications are in the whole.Full GC can never be run during operation.

Leave a Reply

Your email address will not be published. Required fields are marked *