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):

11348 questions
131
votes
4 answers

How to skip "Loose Object" popup when running 'git gui'

When I run 'git gui' I get a popup that says This repository currently has approximately 1500 loose objects. It then suggests compressing the database. I've done this before, and it reduces the loose objects to about 250, but that doesn't…
Michael Donohue
  • 11,606
  • 5
  • 29
  • 44
123
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
  • 48,511
  • 12
  • 121
  • 172
122
votes
10 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
  • 2,960
  • 5
  • 24
  • 41
121
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
121
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.
  • 16,865
  • 9
  • 59
  • 78
119
votes
15 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,183
  • 8
  • 32
  • 44
115
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
  • 22,528
  • 15
  • 64
  • 84
113
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,133
  • 2
  • 8
  • 5
113
votes
11 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
  • 16,782
  • 8
  • 69
  • 86
109
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
  • 983
  • 2
  • 7
  • 7
108
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
  • 112,774
  • 126
  • 328
  • 461
107
votes
5 answers

Why Large Object Heap and why do we care?

I have read about Generations and Large object heap. But I still fail to understand what is the significance (or benefit) of having Large object heap? What could have went wrong (in terms of performance or memory) if CLR would have just relied on…
Manish Basantani
  • 15,105
  • 22
  • 65
  • 96
107
votes
16 answers

What is the garbage collector in Java?

I am new to Java and confused about the garbage collector in Java. What does it actually do and when does it comes into action. Please describe some of the properties of the garbage collector in Java.
Subhransu Mishra
  • 2,855
  • 10
  • 37
  • 44
105
votes
20 answers

What's so wrong about using GC.Collect()?

Although I do understand the serious implications of playing with this function (or at least that's what I think), I fail to see why it's becoming one of these things that respectable programmers wouldn't ever use, even those who don't even know…
Trap
  • 11,199
  • 15
  • 53
  • 64
101
votes
3 answers

How does a garbage collector avoid an infinite loop here?

Consider the following C# program, I submitted it on codegolf as an answer to create a loop without looping: class P{ static int x=0; ~P(){ System.Console.WriteLine(++x); new P(); } static void Main(){ new…
Michael B
  • 7,282
  • 3
  • 25
  • 54