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
91
votes
3 answers

What is the difference between gc() and rm()

I am periodically cleaning the memory in R using a call to rm(list=ls()). Do I need to call the garbage collector gc() after that? What is the difference between these 2 functions? Does gc() call rm() for certain variables?
RockScience
  • 17,932
  • 26
  • 89
  • 125
88
votes
15 answers

Does assigning objects to null in Java impact garbage collection?

Does assigning an unused object reference to null in Java improve the garbage collection process in any measurable way? My experience with Java (and C#) has taught me that is often counter intuitive to try and outsmart the virtual machine or JIT…
James McMahon
  • 48,506
  • 64
  • 207
  • 283
86
votes
4 answers

When will a string be garbage collected in java

In Java, when an object has got no live reference, it is eligible for garbage collection. Now in case of a string, this is not the case because the string will go into the string pool and JVM will keep the object alive for re-use. So that means a…
Victor
  • 16,609
  • 71
  • 229
  • 409
85
votes
2 answers

Prevent .NET Garbage collection for short period of time

I have a high performance application that is handling a very large amount of data. It is receiving, analysing and discarding enormous amounts of information over very short periods of time. This causes a fair amount of object churn that I am…
drobertson
  • 1,548
  • 3
  • 14
  • 25
85
votes
7 answers

Java GC: why two survivor regions?

For Sun/Oracle's JVM, I've read that the GC algo divides new generation into one Eden region and two survivor regions. What I'm wondering about is, why two survivor regions and not just one? The algo can keep ping-ponging between Eden and just one…
shrini1000
  • 7,038
  • 12
  • 59
  • 99
84
votes
2 answers

ASP MVC: When is IController Dispose() called?

I'm going through a big refactoring / speed tweaking of one of my larger MVC apps. It has been deployed to production for a few months now, and I was starting to get timeouts waiting for connections in the connection pool. I have tracked the issue…
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
83
votes
4 answers

Why does a System.Timers.Timer survive GC but not System.Threading.Timer?

It appears that System.Timers.Timer instances are kept alive by some mechanism, but System.Threading.Timer instances are not. Sample program, with a periodic System.Threading.Timer and auto-reset System.Timers.Timer: class Program { static void…
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
82
votes
8 answers

Java 7 (JDK 7) garbage collection and documentation on G1

Java 7 has been out for a while now, but I cannot find any good resources on the configuration of the garbage collectors, specifically the new G1 collector. My questions: Is G1 the default collector in Java 7 and if not how do I activate G1? What…
Florakel
  • 1,131
  • 1
  • 10
  • 8
82
votes
2 answers

Why does exist WeakHashMap, but absent WeakSet?

From J. Bloch A ... source of memory leaks is listeners ... The best way to ensure that callbacks are garbage collected promptly is to store only weak references to them, for instance, by storing them only as keys in a …
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
81
votes
7 answers

Tracking down a memory leak / garbage-collection issue in Java

This is a problem I have been trying to track down for a couple months now. I have a java app running that processes xml feeds and stores the result in a database. There have been intermittent resource problems that are very difficult to track…
liam
  • 3,830
  • 6
  • 32
  • 31
80
votes
12 answers

RAII vs. Garbage Collector

I recently watched a great talk by Herb Sutter about "Leak Free C++..." at CppCon 2016 where he talked about using smart pointers to implement RAII (Resource acquisition is initialization) - Concepts and how they solve most of the memory leaks…
Jiddoo
  • 1,091
  • 2
  • 9
  • 14
78
votes
15 answers

How to monitor Java memory usage?

We have a j2ee application running on Jboss and we want to monitor its memory usage. Currently we use the following code System.gc(); Runtime rt = Runtime.getRuntime(); long usedMB = (rt.totalMemory() - rt.freeMemory()) / 1024 / 1024; …
Oleg Pavliv
  • 20,462
  • 7
  • 59
  • 75
77
votes
14 answers

Java very large heap sizes

Does anyone have experience with using very large heaps, 12 GB or higher in Java? Does the GC make the program unusable? What GC params do you use? Which JVM, Sun or BEA would be better suited for this? Which platform, Linux or Windows, performs…
pdeva
  • 43,605
  • 46
  • 133
  • 171
76
votes
9 answers

Can I trigger JavaScript's garbage collection?

I want to trigger JavaScript garbage collection. Is it possible? Why would I want to, or not want to, do this?
Abhinav
  • 1,041
  • 1
  • 9
  • 11
74
votes
3 answers

When should I use delete vs setting elements to null in JavaScript?

Possible Duplicate: Deleting Objects in JavaScript I have a JS object having a large number of properties. If I want to force the browser to garbage collect this object, do I need to set each of these properties as null or do I need to use the…
jeffreyveon
  • 13,400
  • 18
  • 79
  • 129