Theory and algorithm behind Java garbage collection

后端 未结 6 1292
悲哀的现实
悲哀的现实 2021-01-31 05:14

I read at many places, but did not find a place where I can learn about :

What is java garbage collection all about?

How is it implemented?

When and

6条回答
  •  清歌不尽
    2021-01-31 05:55

    The very short version of answers are:

    What is java garbage collection all about?

    GC is a mechanism of memory management where the system (the JVM in this case) is responsible for automatically reclaiming memory that is no longer in use.

    How is it implemented?

    There are various ways to implement it. A simple description is that each piece of memory that is allocated is tracked. periodically the system checks the allocated pieces to see if any part of the program (the variables) can still reach the memory. Any memory that cannot be reached is reclaimed.

    When and how is it called ?

    This is also left up to the implementation. The only guarantee you have in Java is that before an OutOfMemoryError is thrown the system will attempt to reclaim memory. I would expect that most GC implementations also try to do a collection before they ask the underlying operating system for more memory. In general there will be a background thread that deals with running the collector.

    What algorithms if follows in order to reclaim memory ??

    There are several possible ones. Look at the articles others have posted as a starting point for that.

提交回复
热议问题