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

how to destroy an object in java?

I encountered this question in an interview with following options: How to destroy an object in java? a. System.gc(); b. Runtime.getRuntime.gc(); c. object.delete(); d. object.finalize(); e. Java performs gc by itself, no need to do it…
nr5
  • 4,228
  • 8
  • 42
  • 82
54
votes
1 answer

Why are there memory allocations when calling a func

I have the following program which construct a local Func from two static methods. But strangely, when I profile the program, it allocated close to a million Func objects. Why invoking Func object is also creating Func instances? public static…
Xiaoguo Ge
  • 2,177
  • 20
  • 26
54
votes
8 answers

C#: should object variables be assigned to null?

In C#, is it necessary to assign an object variable to null if you have finished using it, even when it will go out of scope anyway?
Craig Johnston
  • 543
  • 1
  • 4
  • 5
54
votes
2 answers

Can .NET Task instances go out of scope during run?

If I have the following block of code in a method (using .NET 4 and the Task Parallel Library): var task = new Task(() => DoSomethingLongRunning()); task.Start(); and the method returns, will that task go out of scope and be garbage collected, or…
Hank
  • 8,289
  • 12
  • 47
  • 57
54
votes
8 answers

git gc error: failed to run repack message

I've just run git gc on my local repository to do some regular clean-up. Today for the first time, it failed. Here's what I've done: (Windows 7) C:\Source\TxTranslation>git gc Counting objects: 880, done. Delta compression using up to 8…
ygoe
  • 18,655
  • 23
  • 113
  • 210
53
votes
3 answers

Forcing garbage collection in Google Chrome

We are developing a single-page web app with ZK which constantly communicates with server and updates parts of its screens. Updating can be as frequent as 1s. During these updates, references to large ammounts of JS objects are lost and those…
Paulius K.
  • 813
  • 1
  • 7
  • 13
52
votes
3 answers

Caching reflection data

What's the best way to cache expensive data obtained from reflection? For example most fast serializers cache such information so they don't need to reflect every time they encounter the same type again. They might even generate a dynamic method…
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
52
votes
1 answer

timeit and its default_timer completely disagree

I benchmarked these two functions (they unzip pairs back into source lists, came from here): n = 10**7 a = list(range(n)) b = list(range(n)) pairs = list(zip(a, b)) def f1(a, b, pairs): a[:], b[:] = zip(*pairs) def f2(a, b, pairs): for i,…
superb rain
  • 5,300
  • 2
  • 11
  • 25
52
votes
3 answers

Is there a replacement for the garbage collection JVM args in Java 11?

In Java 11 a number of JVM args relating to GC logging are not supported anymore. What, if anything, can they be replaced with, if we still want to use GC logging? In particular, this relates to the following JVM args:…
martin_wun
  • 1,599
  • 1
  • 15
  • 33
52
votes
12 answers

Memory Allocation/Deallocation Bottleneck?

How much of a bottleneck is memory allocation/deallocation in typical real-world programs? Answers from any type of program where performance typically matters are welcome. Are decent implementations of malloc/free/garbage collection fast enough…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
52
votes
2 answers

Whole team gets 'too many unreachable loose objects' messages

Not long ago, we have made the switch from SVN to Git. A few days ago, I realized that all of our team gets those messages when they push : $ git push Counting objects: 32, done. Delta compression using up to 8 threads. Compressing objects: 100%…
jlengrand
  • 12,152
  • 14
  • 57
  • 87
52
votes
5 answers

How to check heap usage of a running JVM from the command line?

Can I check heap usage of a running JVM from the commandline, I mean the actual usage rather than the max amount allocated with Xmx. I need it to be commandline because I don't have access to a windowing environment, and I want script based on the…
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
51
votes
8 answers

Which loop has better performance? Why?

String s = ""; for(i=0;i<....){ s = some Assignment; } or for(i=0;i<..){ String s = some Assignment; } I don't need to use 's' outside the loop ever again. The first option is perhaps better since a new String is not initialized each…
Vivek Kodira
  • 2,764
  • 4
  • 31
  • 49
50
votes
4 answers

Why are annotations under Android such a performance issue (slow)?

I'm the lead author of ORMLite which uses Java annotations on classes to build database schemas. A big startup performance problem for our package turns out to be the calling of annotation methods under Android 1.6. I see the same behavior up…
Gray
  • 115,027
  • 24
  • 293
  • 354
50
votes
10 answers

Circular References in Java

Given an aggregation of class instances which refer to each other in a complex, circular, fashion: is it possible that the garbage collector may not be able to free these objects? I vaguely recall this being an issue in the JVM in the past, but I…