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.






  • 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.


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
20 answers

Proper use of the IDisposable interface

I know from reading Microsoft documentation that the "primary" use of the IDisposable interface is to clean up unmanaged resources. To me, "unmanaged" means things like database connections, sockets, window handles, etc. But, I've seen code where…
  • 26,132
  • 12
  • 37
  • 40
22 answers

Error java.lang.OutOfMemoryError: GC overhead limit exceeded

I get this error message as I execute my JUnit tests: java.lang.OutOfMemoryError: GC overhead limit exceeded I know what an OutOfMemoryError is, but what does GC overhead limit mean? How can I solve this?
  • 50,487
  • 48
  • 148
  • 202
24 answers

Is there a destructor for Java?

Is there a destructor for Java? I don't seem to be able to find any documentation on this. If there isn't, how can I achieve the same effect? To make my question more specific, I am writing an application that deals with data and the specification…
  • 8,923
  • 4
  • 24
  • 19
9 answers

Where Is Machine.Config?

I want to apply a change so That I can use Server GC settings for my C# 3.5 app - I can do that by editing the machine.config file. The only problem is I do not know where that is. How can I find the path of this file in a repeatable way across a…
Jack Kada
  • 24,474
  • 29
  • 82
  • 106
14 answers

Deleting Objects in JavaScript

I'm a bit confused with JavaScript's delete operator. Take the following piece of code: var obj = { helloText: "Hello World!" }; var foo = obj; delete obj; After this piece of code has been executed, obj is null, but foo still refers to an…
5 answers

When should I use GC.SuppressFinalize()?

In .NET, under which circumstances should I use GC.SuppressFinalize()? What advantage(s) does using this method give me?
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
18 answers

When is the finalize() method called in Java?

I need to know when the finalize() method is called in the JVM. I created a test class which writes into a file when the finalize() method is called by overriding it. It is not executed. Can anybody tell me the reason why it is not executing?
Rajesh Kumar J
  • 4,745
  • 6
  • 26
  • 25
13 answers

Why is it bad practice to call System.gc()?

After answering a question about how to force-free objects in Java (the guy was clearing a 1.5GB HashMap) with System.gc(), I was told it's bad practice to call System.gc() manually, but the comments were not entirely convincing. In addition, no one…
  • 134,922
  • 42
  • 253
  • 328
3 answers

Java heap terminology: young, old and permanent generations?

I'm trying to understand What the concepts of young, old and permanent generations are in the Java heap terminology, and more specifically the interactions between the three generations. My questions are: What is the young generation? What is the…
  • 49,059
  • 74
  • 210
  • 294
12 answers

Do you need to dispose of objects and set them to null?

Do you need to dispose of objects and set them to null, or will the garbage collector clean them up when they go out of scope?
  • 22,579
  • 65
  • 193
  • 321
0 answers

Managing the lifetimes of garbage-collected objects

I am making a simplistic mark-and-compact garbage collector. Without going too much into details, the API it exposes is like this: /// Describes the internal structure of a managed object. pub struct Tag { ... } /// An unmanaged pointer to a…
  • 19,076
  • 18
  • 85
  • 153
10 answers

What is JavaScript garbage collection?

What is JavaScript garbage collection? What's important for a web programmer to understand about JavaScript garbage collection, in order to write better code?
16 answers

Why doesn't C++ have a garbage collector?

I'm not asking this question because of the merits of garbage collection first of all. My main reason for asking this is that I do know that Bjarne Stroustrup has said that C++ will have a garbage collector at some point in time. With that said,…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
25 answers

How to force garbage collection in Java?

Is it possible to force garbage collection in Java, even if it is tricky to do? I know about System.gc(); and Runtime.gc(); but they only suggest to do GC. How can I force GC?
6 answers

How does the new automatic reference counting mechanism work?

Can someone briefly explain to me how ARC works? I know it's different from Garbage Collection, but I was just wondering exactly how it worked. Also, if ARC does what GC does without hindering performance, then why does Java use GC? Why doesn't it…
2 3
99 100