java Java垃圾收集背后的理论和算法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4141237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:59:05  来源:igfitidea点击:

Theory and algorithm behind Java garbage collection

javagarbage-collection

提问by daydreamer

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

我在很多地方读过,但没有找到可以学习的地方:

What is java garbage collection all about?

java垃圾收集是什么?

How is it implemented?

它是如何实施的?

When and how is it called ?

何时以及如何称呼?

What algorithms if follows in order to reclaim memory ??

如果遵循什么算法来回收内存?

In short everything about it :)

总之关于它的一切:)

FIXED!!!

固定的!!!

A very good article : http://www.artima.com/insidejvm/ed2/gcP.html

一篇非常好的文章:http: //www.artima.com/insidejvm/ed2/gcP.html

采纳答案by TofuBeer

The very short version of answers are:

非常简短的答案是:

What is java garbage collection all about?

java垃圾收集是什么?

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.

GC 是一种内存管理机制,系统(在本例中为 JVM)负责自动回收不再使用的内存。

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.

这也取决于实施。在 Java 中唯一的保证是在抛出 OutOfMemoryError 之前系统将尝试回收内存。我希望大多数 GC 实现在向底层操作系统请求更多内存之前也会尝试进行收集。通常会有一个后台线程来处理运行收集器。

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.

有几种可能的。看看其他人发布的文章作为起点。

回答by Stephen C

... In short everything about it :)

...简而言之,关于它的一切:)

If you have access to a good library, checkout this excellent and comprehensive book on garbage collection:

如果你有一个好的图书馆,请查看这本关于垃圾收集的优秀而全面的书:

Richard Jones and Rafael Lins, Garbage Collection: Algorithms for Automatic Dynamic Memory Management, Wiley and Sons (1996), ISBN 0-471-94148-4

Richard Jones 和 Rafael Lins,垃圾收集:自动动态内存管理算法,Wiley and Sons (1996),ISBN 0-471-94148-4

Furthermore, this book it is still in print, and listed on at least one well known online bookstore. Shop around. It is available new for a wide range of prices, and for as low as $25 US second hand.

此外,这本书仍在印刷中,并且至少在一家知名的在线书店上架。货比三家。它是全新的,价格范围很广,二手价格低至 25 美元。

回答by Dan Grossman

The Wikipedia entry for garbage collection covers all your questions:

垃圾收集的维基百科条目涵盖了您的所有问题:

http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)

http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)

回答by vanza

Sun^H^H^HOracle has extensivedocumentation on the subject.

Sun^H^H^HOracle 有关于这个主题的大量文档。

回答by Albertkaruna

The garbage collection technique uses MARK and SWEEP algorithm.

垃圾收集技术使用 MARK 和 SWEEP 算法。

For more details Mark-and-Sweep Garbage Collection

有关详细信息,标记和清除垃圾收集

回答by Karthik Prasad

Better article for how GC works in Java at JavaRevistedAnd Algorithem you can get at wikias mentioned by Dan

关于 GC 如何在 Java 中工作的更好的文章JavaRevistedAnd Algorithem 你可以在wiki 上获得,正如 Dan 所提到的