JVM memory management ------ GC algorithm final solution (five minutes for you to fully understand the mark/clear algorithm)

Source: Internet
Author: User

Tag/clear Algorithm. If you have figured out this algorithm, then the last two will be a piece of cake.

Because if we want to recycle garbage during the running of a program (program that is the JAVA program we run on JVM), we must make the GC thread cooperate with the thread in the program, in order to smoothly recycle the garbage without affecting the program running..

The method is to stop the whole program (also known as stop the world) when the available memory in the heap is exhausted, and then perform two tasks, the first item is the mark, and the second item is the clear.

MARK: The process of marking is actually to traverse all GC Roots, and then mark all GC Roots reachable objects as surviving objects.

Clear: clears all objects in the heap and all unmarked objects.

That is, when the available memory is exhausted during the program running, the GC thread will be triggered and the program will be suspended, and then the surviving objects will be marked again, finally, all unmarked objects in the heap are cleared, and the program resumes running..

Their flag spaces are all 0(That is, if it is not marked, 0 is marked by default, and 1 is marked .)Effective memory space depletionThe JVM stops the application running, starts the GC thread, and starts the marking operation. According to the root search algorithm, after marking, the object status is as follows.

Its disadvantage is its low efficiency (recursion and full-heap object traversal), and the application needs to be stopped during GC, which leads to poor user experience., Especially for interactive applications. Imagine if you are playing a website, the website will be suspended for five minutes in an hour. Are you still playing?

The idle Memory cleared in this way is not consecutive.This is not hard to understand. Our dead objects are immediately appearing in every corner of the memory. After clearing them, the memory layout will naturally be messy. To cope with this, JVM has to maintain a memory idle list, which is another overhead. In addition, it is difficult to find continuous memory space when allocating array objects.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.