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
135
votes
1 answer

Python garbage collector documentation

I'm looking for documents that describes in details how python garbage collection works. I'm interested what is done in which step. What objects are in these 3 collections? What kinds of objects are deleted in each step? What algorithm is used for…
Ante
  • 5,350
  • 6
  • 23
  • 46
133
votes
12 answers

How do you Force Garbage Collection from the Shell?

So I am looking at a heap with jmap on a remote box and I want to force garbage collection on it. How do you do this without popping into jvisualvm or jconsole and friends? I know you shouldn't be in the practice of forcing garbage collection -- you…
eyberg
  • 3,160
  • 5
  • 27
  • 43
131
votes
8 answers

Does Haskell require a garbage collector?

I'm curious as to why Haskell implementations use a GC. I can't think of a case where GC would be necessary in a pure language. Is it just an optimization to reduce copying, or is it actually necessary? I'm looking for example code that would leak…
Pubby
  • 51,882
  • 13
  • 139
  • 180
131
votes
16 answers

When does System.gc() do something?

I know that garbage collection is automated in Java. But I understood that if you call System.gc() in your code that the JVM may or may not decide to perform garbage collection at that point. How does this work precisely? On what basis/parameters…
harry
125
votes
2 answers

Is it necessary to explicitly remove event handlers in C#

I have a class that offers up a few events. That class is declared globally but not instanced upon that global declaration--it's instanced on an as-needed basis in the methods that need it. Each time that class is needed in a method, it is…
rp.
  • 17,483
  • 12
  • 63
  • 79
125
votes
17 answers

Best Practice for Forcing Garbage Collection in C#

In my experience it seems that most people will tell you that it is unwise to force a garbage collection but in some cases where you are working with large objects that don't always get collected in the 0 generation but where memory is an issue, is…
Echostorm
  • 9,678
  • 8
  • 36
  • 50
123
votes
5 answers

Why Large Object Heap and why do we care?

I have read about Generations and the Large Object Heap, but I still fail to understand what the significance (or benefit) is of having the Large Object Heap? What could have gone wrong (in terms of performance or memory) if the CLR would have just…
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
120
votes
10 answers

Understanding the meaning of the term and the concept - RAII (Resource Acquisition is Initialization)

Could you C++ developers please give us a good description of what RAII is, why it is important, and whether or not it might have any relevance to other languages? I do know a little bit. I believe it stands for "Resource Acquisition is…
Charlie Flowers
  • 17,338
  • 10
  • 71
  • 88
118
votes
5 answers

What kind of Garbage Collection does Go use?

Go is a garbage collected language: http://golang.org/doc/go_faq.html#garbage_collection Here it says that it's a mark-and-sweep garbage collector, but it doesn't delve into details, and a replacement is in the works... yet, this paragraph seems not…
user1003432
  • 1,183
  • 2
  • 8
  • 5
117
votes
4 answers

Default garbage collector for Java 8

What is the default garbage collector for Java 8? When I check the JMX Beans, they reveal it to be the parallel collector for the new generation and the old serial collector for the old generation.
Code Junkie
  • 1,429
  • 2
  • 10
  • 9
116
votes
6 answers

Does setting Java objects to null do anything anymore?

I was browsing some old books and found a copy of "Practical Java" by Peter Hagger. In the performance section, there is a recommendation to set object references to null when no longer needed. In Java, does setting object references to null…
sal
  • 23,373
  • 15
  • 66
  • 85
116
votes
4 answers

Best practices for reducing Garbage Collector activity in Javascript

I have a fairly complex Javascript app, which has a main loop that is called 60 times per second. There seems to be a lot of garbage collection going on (based on the 'sawtooth' output from the Memory timeline in the Chrome dev tools) - and this…
UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
114
votes
6 answers

When are Java temporary files deleted?

Suppose I create a temporary file in Java with the method File tmp = File.createTempFile(prefix, suffix); If I do not explicity call the delete() method, when will the file be deleted? As an intuition, it might be when the JVM terminates, or…
Alphaaa
  • 4,206
  • 8
  • 34
  • 43
112
votes
11 answers

Garbage collector in Android

I have seen many Android answers that suggest calling the garbage collector in some situations. Is it a good practice to request the garbage collector in Android before doing a memory-hungry operation? If not, should I only call it if I get an…
hpique
  • 119,096
  • 131
  • 338
  • 476
112
votes
13 answers

Best practice for creating millions of small temporary objects

What are the "best practices" for creating (and releasing) millions of small objects? I am writing a chess program in Java and the search algorithm generates a single "Move" object for each possible move, and a nominal search can easily generate…
Humble Programmer
  • 1,013
  • 2
  • 8
  • 7