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):

11348 questions
1743
votes
20 answers

Proper use of the IDisposable interface

I know from reading the Microsoft documentation that the "primary" use of the IDisposable interface is to clean up unmanaged resources. To me, "unmanaged" means things like database connections, sockets, window handles, etc. But, I've seen code…
cwick
  • 24,336
  • 12
  • 35
  • 40
899
votes
22 answers

How can I read a large text file line by line using Java?

I need to read a large text file of around 5-6 GB line by line using Java. How can I do this quickly?
manoj singh
  • 9,047
  • 3
  • 13
  • 3
871
votes
20 answers

Error java.lang.OutOfMemoryError: GC overhead limit exceeded

I get this error message as I execute my JUnit tests: java.lang.OutOfMemoryError: GC overhead limit exceeded I know what an OutOfMemoryError is, but what does GC overhead limit mean? How can I solve this?
Mnementh
  • 47,129
  • 42
  • 140
  • 198
623
votes
22 answers

Is there a destructor for Java?

Is there a destructor for Java? I don't seem to be able to find any documentation on this. If there isn't, how can I achieve the same effect? To make my question more specific, I am writing an application that deals with data and the specification…
wai
  • 8,305
  • 4
  • 21
  • 19
445
votes
9 answers

Where Is Machine.Config?

I want to apply a change so That I can use Server GC settings for my C# 3.5 app - I can do that by editing the machine.config file. The only problem is I do not know where that is. How can I find the path of this file in a repeatable way across a…
Jack Kada
  • 22,354
  • 29
  • 75
  • 103
369
votes
11 answers

Deleting Objects in JavaScript

I'm a bit confused with JavaScript's delete operator. Take the following piece of code: var obj = { helloText: "Hello World!" }; var foo = obj; delete obj; After this piece of code has been executed, obj is null, but foo still refers to an…
347
votes
17 answers

When is the finalize() method called in Java?

I need to know when the finalize() method is called in the JVM. I created a test class which writes into a file when the finalize() method is called by overriding it. It is not executed. Can anybody tell me the reason why it is not executing?
Rajesh Kumar J
  • 4,495
  • 6
  • 22
  • 25
341
votes
13 answers

Why is it bad practice to call System.gc()?

After answering a question about how to force-free objects in Java (the guy was clearing a 1.5GB HashMap) with System.gc(), I was told it's bad practice to call System.gc() manually, but the comments were not entirely convincing. In addition, no one…
zneak
  • 124,558
  • 39
  • 238
  • 307
331
votes
5 answers

Java heap terminology: young, old and permanent generations?

I'm trying to understand What the concepts of young, old and permanent generations are in the Java heap terminology, and more specifically the interactions between the three generations. My questions are: What is the young generation? What is the…
knorv
  • 45,461
  • 71
  • 205
  • 289
327
votes
11 answers

Do you need to dispose of objects and set them to null?

Do you need to dispose of objects and set them to null, or will the garbage collector clean them up when they go out of scope?
CJ7
  • 20,640
  • 59
  • 173
  • 305
315
votes
5 answers

When should I use GC.SuppressFinalize()?

In .NET, under which circumstances should I use GC.SuppressFinalize()? What advantage(s) does using this method give me?
Sam Saffron
  • 121,058
  • 74
  • 309
  • 495
301
votes
9 answers

What is JavaScript garbage collection?

What is JavaScript garbage collection? What's important for a web programmer to understand about JavaScript garbage collection, in order to write better code?
user290
281
votes
16 answers

Why doesn't C++ have a garbage collector?

I'm not asking this question because of the merits of garbage collection first of all. My main reason for asking this is that I do know that Bjarne Stroustrup has said that C++ will have a garbage collector at some point in time. With that said,…
Jason Baker
  • 171,942
  • 122
  • 354
  • 501
241
votes
23 answers

How to force garbage collection in Java?

Is it possible to force garbage collection in Java, even if it is tricky to do? I know about System.gc(); and Runtime.gc(); but they only suggest to do GC. How can I force GC?
user179172
207
votes
6 answers

How does the new automatic reference counting mechanism work?

Can someone briefly explain to me how ARC works? I know it's different from Garbage Collection, but I was just wondering exactly how it worked. Also, if ARC does what GC does without hindering performance, then why does Java use GC? Why doesn't it…
1
2 3
99 100