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
60
votes
6 answers

Unloading the Assembly loaded with Assembly.LoadFrom()

I need to check the time amount to run GetTypes() after loading the dll. The code is as follows. Assembly assem = Assembly.LoadFrom(file); sw = Stopwatch.StartNew(); var types1 = assem.GetTypes(); sw.Stop(); double time1 =…
prosseek
  • 182,215
  • 215
  • 566
  • 871
60
votes
7 answers

Difference between system.gc() and runtime.gc()

What is the difference between System.gc() and Runtime.gc()?
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
60
votes
8 answers

Can the JVM GC move objects in the middle of a reference comparison, causing a comparison to fail even when both sides refer to the same object?

It's well known that GCs will sometimes move objects around in memory. And it's to my understanding that as long as all references are updated when the object is moved (before any user code is called), this should be perfectly safe. However, I saw…
Kat
  • 4,645
  • 4
  • 29
  • 81
60
votes
3 answers

What are the Generations in Garbage Collection?

I don't understand what "generations" are in the context of Garbage Collection. Can someone explain in simple terms?
user246252
58
votes
8 answers

Find out the size of a .NET object

I'm trying to find out how much memory my objects take to see how many of them are ending up on the Large Object Heap (which is anything over 85,000 bytes). Is it as simple as adding 4 for an int, 8 for a long, 4 (or 8 if you're on 64 bit) for any…
Matthew Steeples
  • 7,858
  • 4
  • 34
  • 49
58
votes
7 answers

What are pinned objects?

I am trying to find a memory leak using ants memory profiler, and I've encountered in a new term: Pinned objects. Can some one give me a good & simple explanation about what this objects are, How can I pinn/Unpinn objects, and detect who pinned…
sagie
  • 2,998
  • 3
  • 22
  • 31
58
votes
9 answers

What makes Ruby slow?

Ruby is slow at certain things. But what parts of it are the most problematic? How much does the garbage collector affect performance? I know I've had times when running the garbage collector alone took several seconds, especially when working…
Nick Retallack
  • 18,986
  • 17
  • 92
  • 114
56
votes
21 answers

Memory Leak in C#

Is it ever possible in a managed system to leak memory when you make sure that all handles, things that implement IDispose are disposed? Would there be cases where some variables are left out?
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
56
votes
12 answers

Know of any Java garbage collection log analysis tools?

I'm looking for a tool or a script that will take the console log from my web app, parse out the garbage collection information and display it in a meaningful way. I'm starting up on a Sun Java 1.4.2 JVM with the following flags: -verbose:gc…
braveterry
  • 3,724
  • 8
  • 47
  • 59
56
votes
4 answers

Do I need to close a ByteArrayInputStream?

Short question, I saw in some old code where a ByteArrayInputStream was created like: new BufferedReader(new InputStreamReader(new ByteArrayInputStream(somebytes))); And then the BufferedReader is used to read out somebytes line by line. All…
dr jerry
  • 9,768
  • 24
  • 79
  • 122
55
votes
4 answers

"Island of isolation" of Garbage Collection

Could anyone please explain the concept of Island of isolation of Garbage Collection?
Isabel Jinson
  • 8,541
  • 16
  • 59
  • 75
55
votes
6 answers

How to implement garbage collection in C++

I saw some post about implement GC in C and some people said it's impossible to do it because C is weakly typed. I want to know how to implement GC in C++. I want some general idea about how to do it. Thank you very much! This is a Bloomberg…
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86
55
votes
7 answers

Why does the JVM require warmup?

I understand that in the Java virtual machine (JVM), warmup is potentially required as Java loads classes using a lazy loading process and as such you want to ensure that the objects are initialized before you start the main transactions. I am a C++…
Suparna
  • 1,132
  • 1
  • 8
  • 20
55
votes
5 answers

Xamarin iOS memory leaks everywhere

We've been using Xamarin iOS for the last 8 months and developed a non-trivial enterprise app with many screens, features, nested controls. We've done our own MVVM arch, cross platform BLL & DAL as "recommended". We share code between Android and…
Herman Schoenfeld
  • 8,464
  • 4
  • 38
  • 49
55
votes
6 answers

Mixing Erlang and Haskell

If you've bought into the functional programming paradigm, the chances are that you like both Erlang and Haskell. Both have purely functional cores and other goodness such as lightweight threads that make them a good fit for a multicore world. But…