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
206
votes
23 answers

When is it acceptable to call GC.Collect?

The general advice is that you should not call GC.Collect from your code, but what are the exceptions to this rule? I can only think of a few very specific cases where it may make sense to force a garbage collection. One example that springs to mind…
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
205
votes
4 answers

Do event handlers stop garbage collection from occurring?

If I have the following code: MyClass pClass = new MyClass(); pClass.MyEvent += MyFunction; pClass = null; Will pClass be garbage collected? Or will it hang around still firing its events whenever they occur? Will I need to do the following in…
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
198
votes
2 answers

Understanding garbage collection in .NET

Consider the below code: public class Class1 { public static int c; ~Class1() { c++; } } public class Class2 { public static void Main() { { var c1=new Class1(); //c1=null; // If this…
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97
195
votes
11 answers

How to handle :java.util.concurrent.TimeoutException: android.os.BinderProxy.finalize() timed out after 10 seconds errors?

We're seeing a number of TimeoutExceptions in GcWatcher.finalize, BinderProxy.finalize, and PlainSocketImpl.finalize. 90+% of them happen on Android 4.3. We're getting reports of this from Crittercism from users out in the field. The error is a…
emmby
  • 99,783
  • 65
  • 191
  • 249
186
votes
9 answers

Stack, Static, and Heap in C++

I've searched, but I've not understood very well these three concepts. When do I have to use dynamic allocation (in the heap) and what's its real advantage? What are the problems of static and stack? Could I write an entire application without…
Hai
  • 4,764
  • 8
  • 29
  • 26
179
votes
9 answers

How does Java Garbage Collection work with Circular References?

From my understanding, garbage collection in Java cleans up some objects if nothing else is 'pointing' to that object. My question is, what happens if we have something like this: class Node { public object value; public Node next; …
AlexeyMK
  • 6,245
  • 9
  • 36
  • 41
177
votes
7 answers

Is explicitly closing files important?

In Python, if you either open a file without calling close(), or close the file but not using try-finally or the "with" statement, is this a problem? Or does it suffice as a coding practice to rely on the Python garbage-collection to close all…
user553702
  • 2,819
  • 5
  • 23
  • 27
174
votes
13 answers

How to free memory in Java?

Is there a way to free memory in Java, similar to C's free() function? Or is setting the object to null and relying on GC the only option?
Felix
  • 88,392
  • 43
  • 149
  • 167
172
votes
8 answers

Implementing IDisposable correctly

In my classes I implement IDisposable as follows: public class User : IDisposable { public int id { get; protected set; } public string name { get; protected set; } public string pass { get; protected set; } public User(int UserID) …
Ortund
  • 8,095
  • 18
  • 71
  • 139
170
votes
6 answers

How JavaScript closures are garbage collected

I've logged the following Chrome bug, which has led to many serious and non-obvious memory leaks in my code: (These results use Chrome Dev Tools' memory profiler, which runs the GC, and then takes a heap snapshot of everything not garbaged…
161
votes
3 answers

Java GC (Allocation Failure)

Why always "GC (Allocation Failure)"? Java HotSpot(TM) 64-Bit Server VM (25.25-b02) for linux-amd64 JRE (1.8.0_25-b17), CommandLine flags: -XX:CMSInitiatingOccupancyFraction=60 -XX:GCLogFileSize=10485760 -XX:+HeapDumpOnOutOfMemoryError…
user3644708
  • 2,466
  • 2
  • 16
  • 32
147
votes
4 answers

What does Rust have instead of a garbage collector?

I understand Rust doesn't have a garbage collector and am wondering how memory is freed up when a binding goes out of scope. So in this example, I understand that Rust reclaims the memory allocated to a when it goes out of scope. { let a =…
rix
  • 10,104
  • 14
  • 65
  • 92
146
votes
5 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,776
  • 5
  • 31
  • 44
141
votes
7 answers

How do you prevent IDisposable from spreading to all your classes?

Start with these simple classes... Let's say I have a simple set of classes like this: class Bus { Driver busDriver = new Driver(); } class Driver { Shoe[] shoes = { new Shoe(), new Shoe() }; } class Shoe { Shoelace lace = new…
GrahamS
  • 9,980
  • 9
  • 49
  • 63
139
votes
5 answers

Reducing garbage-collection pause time in a Haskell program

We are developing a program which receives and forwards "messages", while keeping a temporary history of those messages, so that it can tell you the message history if requested. Messages are identified numerically, are typically around 1 kilobyte…
jameshfisher
  • 34,029
  • 31
  • 121
  • 167