Questions tagged [garbage-collection]

Garbage collection (GC) is a form of automatic memory management which attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program.

Garbage collection was invented by John McCarthy around 1959 to solve problems in .

Garbage collection is often portrayed as the opposite of manual memory management, which requires the programmer to specify which objects to deallocate and return to the memory system. However, many systems use a combination of the two approaches, and other techniques such as stack allocation and region inference can carve off parts of the problem. There is an ambiguity of terms, as theory often uses the terms manual garbage collection and automatic garbage collection rather than manual memory management and garbage collection, and does not restrict garbage collection to memory management, rather considering that any logical or physical resource may be garbage collected.

Garbage collection does not traditionally manage limited resources other than memory that typical programs use, such as network sockets, database handles, user interaction windows, and file and device descriptors. Methods used to manage such resources, particularly destructors, may suffice as well to manage memory, leaving no need for GC. Some GC systems allow such other resources to be associated with a region of memory that, when collected, causes the other resource to be reclaimed; this is called finalization. Finalization may introduce complications limiting its usability, such as intolerable latency between disuse and reclaim of especially limited resources, or a lack of control over which thread performs the work of reclaiming.

Real-time garbage collection

While garbage collection is generally nondeterministic, it is possible to use it in hard real-time systems. A real-time garbage collector (while being a daemon thread) should guarantee that even in the worst case it will dedicate a certain number of computational resources to mutator threads. Constraints imposed on a real-time garbage collector are usually either work based or time based. A time based constraint would look like: within each time window of duration T, mutator threads should be allowed to run at least for Tm time. For work based analysis, MMU (minimal mutator utilization) is usually used as a real time constraint for the garbage collection algorithm.

References

General

.NET

Java

Smalltalk

  • Squeak: Open Personal Computing and Multimedia by Mark J. Guzdial & Kimberly M. Rose, covering the Squeak Smalltalk garbage collector and other Smalltalk design issues. Squeak is almost exclusively written in Smalltalk and all of the source—including the garbage collector—is freely available and easy to study using Smalltalk browsers.

Theoretical/Academic

Professor Kathryn McKinley maintains a list of papers for her Memory Management course at the University of Texas online. (Note that the links to these papers that are provided below are from freely available versions; almost all of which are hosted by a university and retrieved using Google Scholar.) If you want to gain a complete theoretical understanding of garbage collection, you should read these papers in order—they are arranged in progressive difficulty of subject matter (not chronologically):

12065 questions
74
votes
6 answers

Forcing garbage collection to run in R with the gc() command

Periodically I program sloppily. Ok, I program sloppily all the time, but sometimes that catches up with me in the form of out of memory errors. I start exercising a little discipline in deleting objects with the rm() command and things get better.…
JD Long
  • 59,675
  • 58
  • 202
  • 294
73
votes
4 answers

How to redirect verbose garbage collection output to a file?

How do I redirect verbose garbage collection output to a file? Sun’s website shows an example for Unix but it doesn't work for Windows.
djangofan
  • 28,471
  • 61
  • 196
  • 289
72
votes
4 answers

Garbage collector and circular reference

Consider these two classes: public class A { B b; public A(B b) { this.b = b; } } public class B { A a; public B() { this.a = new A(this); } } If I have classes designed like above, would the objects of such classes be…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
72
votes
9 answers

Garbage collection Libraries in C++

What free and commercial garbage collection libraries are available for C++, and what are the pros and cons of each? I am interested in hard-won lessons from actual use in the field, not marketing or promotional blurb. There is no need to elaborate…
Andrew Bettison
  • 721
  • 1
  • 5
  • 3
72
votes
2 answers

How to get javascript object references or reference count?

How to get reference count for an object Is it possible to determine if a javascript object has multiple references to it? Or if it has references besides the one I'm accessing it with? Or even just to get the reference count itself? Can I find…
Tauren
  • 26,795
  • 42
  • 131
  • 167
72
votes
4 answers

Why does the JVM full GC need to stop-the-world?

I think it is because the JVM needs to move objects, is that correct?
cedrics
  • 761
  • 1
  • 6
  • 7
71
votes
8 answers

How to implement a garbage collector?

Could anyone point me to a good source on how to implement garbage collection? I am making a lisp-like interpreted language. It currently uses reference counting, but of course that fails at freeing circularly dependent objects. I've been reading of…
salvador p
  • 2,905
  • 5
  • 26
  • 26
71
votes
5 answers

What are the roots?

What are the roots in garbage collection? I have read the definition of root as "any reference that you program can access to" and definition of live is that an object that is being used, which can be a local variable, static variable. I m little…
DarthVader
  • 52,984
  • 76
  • 209
  • 300
71
votes
2 answers

Does garbage collector call Dispose()?

I thought the GC would call Dispose eventually if your program did not but that you should call Dispose() in your program just to make the cleanup deterministic. However, from my little test program, I don't see Dispose getting called at…
noctonura
  • 12,763
  • 10
  • 52
  • 85
70
votes
10 answers

To GC or Not To GC

I've recently seen two really nice and educating languages talks: This first one by Herb Sutter, presents all the nice and cool features of C++0x, why C++'s future seems brighter than ever, and how M$ is said to be a good guy in this game. The talk…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
70
votes
12 answers

Since .NET has a garbage collector why do we need finalizers/destructors/dispose-pattern?

If I understand correctly the .net runtime will always clean up after me. So if I create new objects and I stop referencing them in my code, the runtime will clean up those objects and free the memory they occupied. Since this is the case why then…
J A
  • 703
  • 1
  • 6
  • 5
69
votes
6 answers

Does GC release back memory to OS?

When the garbage collector runs and releases memory does this memory go back to the OS or is it being kept as part of the process. I was under the strong impression that the memory is never actually released back to OS but kept as part of the memory…
Jim
  • 18,826
  • 34
  • 135
  • 254
69
votes
3 answers

Young , Tenured and Perm generation

I'm confused with Heap,Young,Tenured and Perm generation. Could anyone please explain?
Isabel Jinson
  • 8,541
  • 16
  • 59
  • 75
68
votes
3 answers

Why does gc() not free memory?

I run simulations on a Windows 64bit-computer with 64 GB RAM. Memory use reaches 55% and after a finished simulation run I remove all objects in the working space by rm(list=ls()), followed by a double gc(). I supposed that this would free enough…
user7417
  • 1,531
  • 3
  • 15
  • 20
68
votes
15 answers

Should "Dispose" only be used for types containing unmanaged resources?

I was having a discussion with a colleague recently about the value of Dispose and types that implement IDisposable. I think there is value in implementing IDisposable for types that should clean up as soon as possible, even if there are no…
Steve Dunn
  • 21,044
  • 11
  • 62
  • 87