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
112
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
  • 12,050
  • 15
  • 55
  • 67
111
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
  • 3,035
  • 11
  • 40
  • 47
102
votes
8 answers

Is there a practical use for weak references?

Possible Duplicate: Weak references - how useful are they? Since weak references can be claimed by the garbage collector at any time, is there any practical reason for using them?
user496949
  • 83,087
  • 147
  • 309
  • 426
102
votes
6 answers

Are static fields open for garbage collection?

Given an hypothetical utility class that is used only in program setup: class MyUtils { private static MyObject myObject = new MyObject(); /*package*/static boolean doStuff(Params... params) { // do stuff with myObject and params... …
Michael Deardeuff
  • 10,386
  • 5
  • 51
  • 74
102
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,512
  • 3
  • 31
  • 57
101
votes
3 answers

How to request the Garbage Collector in node.js to run?

At startup, it seems my node.js app uses around 200MB of memory. If I leave it alone for a while, it shrinks to around 9MB. Is it possible from within the app to: Check how much memory the app is using ? Request the garbage collector to run ? The…
Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
98
votes
3 answers

Java Garbage Collection Log messages

I have configured java to dump garbage collection information into the logs (verbose GC). I am unsure of what the garbage collection entries in the logs mean. A sample of these entries are posted below. I've searched around on Google and have not…
Ethan Heilman
  • 16,347
  • 11
  • 61
  • 88
98
votes
24 answers

What's the best way (most efficient) to turn all the keys of an object to lower case?

I've come up with function keysToLowerCase (obj) { var keys = Object.keys(obj); var n = keys.length; while (n--) { var key = keys[n]; // "cache" it, for less lookups to the array if (key !== key.toLowerCase()) { // might already be in…
97
votes
10 answers

MemoryStream.Close() or MemoryStream.Dispose()

Which one do I call? Is it necessary to call both? Will the other throw an exception if I have already called one of them?
ashwnacharya
  • 14,601
  • 23
  • 89
  • 112
96
votes
4 answers

Android - Activity Constructor vs onCreate

I understand that Android Activities have specific lifecycles and that onCreate should be overridden and used for initialization, but what exactly happens in the constructor? Are there any cases when you could/should override the Activity…
idolize
  • 6,455
  • 4
  • 24
  • 34
93
votes
1 answer

GC overhead limit exceeded

What is the sampling time JVM uses to throw 'java.lang.OutOfMemoryError : GC overhead limit exceeded'? I know you can control 98% and 2% with parameters GCTimeLimit and GCHeapFreeLimit but whats the sampling time?
PRK
  • 959
  • 1
  • 6
  • 3
93
votes
11 answers

How to force JavaScript to deep copy a string?

I have some javascript code which looks like this: var myClass = { ids: {} myFunc: function(huge_string) { var id = huge_string.substr(0,2); ids[id] = true; } } Later the function gets called with some large strings (100 MB+). I…
93
votes
4 answers

Java Thread Garbage collected or not

This question was posted on some site. I didnt find right answers there, so I am posting it here again. public class TestThread { public static void main(String[] s) { // anonymous class extends Thread Thread t = new…
Ashish
  • 2,521
  • 2
  • 21
  • 21
92
votes
15 answers

Does using final for variables in Java improve garbage collection?

Today my colleagues and me have a discussion about the usage of the final keyword in Java to improve the garbage collection. For example, if you write a method like: public Double doCalc(final Double value) { final Double maxWeight = 1000.0; …
Goran Martinic
  • 3,807
  • 4
  • 21
  • 14
92
votes
16 answers

Java G1 garbage collection in production

Since Java 7 is going to use the new G1 garbage collection by default is Java going to be able to handle an order of magnitude larger heap without supposed "devastating" GC pause times? Has anybody actually implemented G1 in production, what were…
benstpierre
  • 32,833
  • 51
  • 177
  • 288