How many objects are eligible for Garbage collection in Java?

How is garbage collection in CLR (.NET) different from garbage collection in the JVM (Java)?

  • How does the Common Language Runtime's approach to garbage collection differ from the approach taken by the Java Virtual Machine, and what are the performance trade offs of each approach?

  • Answer:

    There are a number of garbage collectors available on each. First, in general, both the CLR and the JVM are virtual machines.  So both avoid problems common to doing GC in close-to-the-metal fully compiled languages, such as the stack-crawling problem (which requires heuristics for determining the roots) and the atomicity problem (which affects having the collector in a separate thread). Garbage collectors are on a kind of continuum. At one end, there is the full compaction garbage collector.  This must stop the process (or all the processes), find all of the roots of the structures, mark them all, and sweep unmarked blocks into free storage.  These are simple to implement, blindingly fast, and require no stack.  However, the entire heap must be collected at once, and despite the speed of the collector, in a large system it may take too long to finish.  It may be triggered at any time by a failed memory request and may happen during a time-critical section of the code.  These can be useful, but only with some understanding of the program and control over the collector.  I have had good success with a highly optimized garbage collector running every time through the event loop.  However, as the point of most general garbage collectors is to prevent the developer from having to worry about things, this is not a good general solution. At the other end, there are fully dynamic generational collectors, such as the Train algorithm, which guarantees a maximum run time per iteration.  The expense is that they are hard to write, are very slow, and tend to fail rather dramatically if the settings don't match the allocation properties of the program. In the middle are garbage collectors with some of the properties of each.  They are faster than fully dynamic generational collectors but have some awareness of generations.  The are often designed to run in separate threads to avoid bogging down the speed, at the expense of much greater complexity (solvable most easily if certain ideas are built into the run-time system). The last time I checked, the CLR had three standard garbage collectors, all of the middle-ground variety.  I don't think anybody changed them, though, because nowadays people talk about the CLR garbage collector, which is being constantly tweaked.  You can get pretty good performance, but it may sometimes require tweaking. The Hotspot JVM uses the Train algorithm, which basically Just Worksâ„¢ for a large class of programs, without being very noticeable, at the expense of a performance overall, and catastrophic failure or slowdowns in some extreme cases.

Eric Pepke at Quora Visit the source

Was this solution helpful to you?

Other answers

The only major difference that I can think of is that the JVM relies upon a Lisp-like uniform data representation whereas the CLR supports value types. This allows .NET programmers to unbox arbitrarily and, thanks to reified generics, even in the context of generic data structures and algorithms. Consequently, there is typically a lot less stress on the CLR GC. Conversely, the extra stress on the JVM GC has meant that it has been much more heavily optimized. Despite this optimization, the JVM can still be much slower than the CLR when a more appropriate data representation pays off (see http://fsharpnews.blogspot.co.uk/2010/05/java-vs-f.html).

Jon Harrop

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.