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

How to force deletion of a python object?

I am curious about the details of __del__ in python, when and why it should be used and what it shouldn't be used for. I've learned the hard way that it is not really like what one would naively expected from a destructor, in that it is not the…
wim
  • 338,267
  • 99
  • 616
  • 750
67
votes
6 answers

Very High Memory Usage in .NET 4.0

I have a C# Windows Service that I recently moved from .NET 3.5 to .NET 4.0. No other code changes were made. When running on 3.5, memory utilzation for a given work load was roughly 1.5 GB of memory and throughput was 20 X per second. (The X…
RMD
  • 3,421
  • 7
  • 39
  • 85
67
votes
6 answers

Java garbage collector - When does it collect?

What is it that determines when the garbage collector actually collects? Does it happen after a certain time or after a certain amount of memory have been used up? Or are there other factors?
nalo
  • 998
  • 2
  • 8
  • 12
66
votes
5 answers

.Net vs Java Garbage Collector

Does anyone know the major differences between the Java and .Net garbage collectors? A web search has not revealed much, and it was a question that came up in a test.
Mike
  • 661
  • 1
  • 5
  • 3
66
votes
6 answers

How can I avoid garbage collection delays in Java games? (Best Practices)

I'm performance tuning interactive games in Java for the Android platform. Once in a while there is a hiccup in drawing and interaction for garbage collection. Usually it's less than one tenth of a second, but sometimes it can be as large as 200ms…
Brian
  • 8,454
  • 5
  • 27
  • 30
66
votes
9 answers

Why Java and Python garbage collection methods are different?

Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed. But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time. Why does Java…
popopome
  • 12,250
  • 15
  • 44
  • 36
65
votes
10 answers

Why no Reference Counting + Garbage Collection in C#?

I come from a C++ background and I've been working with C# for about a year. Like many others I'm flummoxed as to why deterministic resource management is not built-in to the language. Instead of deterministic destructors we have the dispose…
Skrymsli
  • 5,173
  • 7
  • 34
  • 36
65
votes
4 answers

How many objects will be eligible for garbage collection after executing "m1=null; m2=null;"?

I am confused to understand after executing m1 = null; m2 = null;. How many objects will be eligible for garbage collection? public class MyTest { MyTest m; void show() { System.out.println("Hello this is show method."); } …
user5347751
65
votes
3 answers

What is the use of MetaSpace in Java 8?

I know they have replaced PermGen with MetaSpace in Java 8. But I have few questions: Is MetaSpace by default is GC collected? Even the PermGen is GC collected by adding the args like -XX:+CMSClassUnloadingEnabled, then what makes MetaSpace better…
batman
  • 4,728
  • 8
  • 39
  • 45
64
votes
3 answers

What do GC_FOR_MALLOC, GC_EXPLICIT, and other GC_* mean in Android Logcat?

If you see the Android logs, you may see a lot of those things. What do they mean, knowing those may help us doing better memory allocations. Example: 28470 dalvikvm D GC_FOR_MALLOC freed 665 objects / 239992 bytes in 71ms 28470 …
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
62
votes
9 answers

Is the stack garbage collected in Java?

The heap memory is garbage collected in Java. Is the stack garbage collected as well? How is stack memory reclaimed?
giri
  • 26,773
  • 63
  • 143
  • 176
62
votes
5 answers

Is there garbage collection in PHP?

I know that in PHP you don't have to free memory. Is it reached by garbage collector?
Sergey
  • 47,222
  • 25
  • 87
  • 129
61
votes
4 answers

What is the difference between Objective-C automatic reference counting and garbage collection?

With the new automatic reference counting (ARC) introduced in Xcode 4.2, we no longer need to manually manage retain / release in Objective-C. This seems similar to garbage collection, as done in Objective-C on the Mac, and in other languages. How…
Gauging
  • 705
  • 1
  • 6
  • 8
61
votes
4 answers

Does SqlCommand.Dispose close the connection?

Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand("GetSomething", new SqlConnection(Config.ConnectionString)) { cmd.Connection.Open(); // set up parameters and CommandType to StoredProcedure etc. etc. …
Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
61
votes
8 answers

Force freeing memory in PHP

In a PHP program, I sequentially read a bunch of files (with file_get_contents), gzdecode them, json_decode the result, analyze the contents, throw most of it away, and store about 1% in an array. Unfortunately, with each iteration (I traverse over…
DBa
  • 1,263
  • 3
  • 11
  • 16