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
49
votes
4 answers

Explanation of Azul's "pauseless" garbage collector

I've just read this: http://www.artima.com/lejava/articles/azul_pauseless_gc.html Although I've some experience with compilers, I've done nothing related with garbage collection; is a big black box to me. I've struggled to understand the issues…
kikito
  • 51,734
  • 32
  • 149
  • 189
49
votes
1 answer

Allocations in new TLAB vs allocations outside TLAB

The Java Mission Control tool in the JDK provides statistics about object allocation in new TLAB and allocations outside TLAB. (It's under Memory/Allocations). What is the significance of these statistics, what is good for the performance of an…
WannaKnow
  • 1,155
  • 2
  • 11
  • 18
49
votes
16 answers

Garbage Collection in C++ -- why?

I keep hearing people complaining that C++ doesn't have garbage collection. I also hear that the C++ Standards Committee is looking at adding it to the language. I'm afraid I just don't see the point to it... using RAII with smart pointers…
Head Geek
  • 38,128
  • 22
  • 77
  • 87
48
votes
6 answers

Why don't purely functional languages use reference counting?

In purely functional languages, data is immutable. With reference counting, creating a reference cycle requires changing already created data. It seems like purely functional languages could use reference counting without worrying about the…
48
votes
1 answer

Circular references in Javascript / Garbage collector

Can somebody explain in detail how Javascript engines deal with circular references ? Is there a big difference between browsers or even node.js ? What I'm talking about is an explicit back-/next reference within objects. For instance: var objA = { …
jAndy
  • 231,737
  • 57
  • 305
  • 359
48
votes
2 answers

How to gain control of a 5GB heap in Haskell?

Currently I'm experimenting with a little Haskell web-server written in Snap that loads and makes available to the client a lot of data. And I have a very, very hard time gaining control over the server process. At random moments the process uses a…
48
votes
5 answers

Any way to workaround WPF's calling of GC.Collect(2) aside from reflection?

I recently had to check in this monstrosity into production code to manipulate private fields in a WPF class: (tl;dr how do I avoid having to do this?) private static class MemoryPressurePatcher { private static Timer gcResetTimer; private…
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
48
votes
3 answers

How does the Garbage-First Garbage Collector work?

Can someone explain how the G1 Garbage Collector works please? I haven't been able to find any comprehensive, easy-to-understand descriptions anywhere yet. Thanks
dogbane
  • 266,786
  • 75
  • 396
  • 414
48
votes
2 answers

What are GC roots for classes?

In Java, there are special objects called Garbage Collection Roots (GC roots). They serve as a root objects for Garbage Collection marking mechanism (see picture). This article describes four types of GC roots: local variables active…
Kao
  • 7,225
  • 9
  • 41
  • 65
48
votes
3 answers

Setting -XX:+DisableExplicitGC in production: what could go wrong?

we just had a meeting to address some performance issues in a web application that is used to calculate insurance rates. The calculations are implemented in a C/C++-module, that is used in other software packages as well. To make it available as a…
Axel
  • 13,939
  • 5
  • 50
  • 79
48
votes
3 answers

Cost of using weak references in Java

Has anyone researched the runtime costs involved in creating and garbage collecting Java WeakReference objects? Are there any performance issues (e.g. contention) for multi-threaded applications? EDIT: Obviously the actual answer(s) will be JVM…
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
47
votes
2 answers

Do static members ever get garbage collected?

Do static member variables ever get garbage collected? For example, let's use the following class. public class HasStatic { private static List shared = new List(); } And supposed that it's used like…
C. Ross
  • 31,137
  • 42
  • 147
  • 238
47
votes
10 answers

HttpWebRequest times out on second call

Why does the following code Timeout the second (and subsequent) time it is run? The code hangs at: using (Stream objStream = request.GetResponse().GetResponseStream()) and then causes a WebException saying that the request has timed out. I have…
Darbio
  • 11,286
  • 12
  • 60
  • 100
47
votes
2 answers

jQuery memory leak patterns and causes

What are some of the standard issues or coding patterns in jQuery which lead to memory leaks? I have seen a number of questions related to the ajax() call or jsonp or DOM removal on StackOverflow. Most of the jQuery memory leak questions are…
Thimmayya
  • 2,064
  • 2
  • 18
  • 20
47
votes
1 answer

What is a TLAB (Thread Local Allocation Buffer)?

I couldn't find a comprehensive source that would explain the concept in a clear manner. My understanding is that a thread is given some chunk of memory in the eden where it allocates new objects. A competing thread will end up having a somewhat…
user1745356
  • 4,462
  • 7
  • 42
  • 70