Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the fundamental differences between garbage collection in C# and Java?

I had some very wrong sounding advice recently from a "senior" developer/coworker regarding the C# garbage collector such as...

  • "You need to use destructors everywhere in C# because the garbage collector cannot be relied upon."

  • "The C# garbage collector cannot be thought of like the Java garbage collector".

This sounds extremely fishy to me, as far as I know the differences between the C# and Java garbage collectors are as follows...

  • The C# is a Generational garbage collector, Java is concurrent mark sweep in 1.6 with G1 being the new default (generational) garbage collector featuring Java 7 and has been optional since ~1.6.21. As far as I know
  • C# as a language has the ability to manually dispose of objects that implement IDisposable. Java must always use garbage collection, although some frameworks like SWT require you manually call methods to release memory in the underlying native code.

I realize that Java and C# are just the languages and the garbage collectors are a component of the runtime, however for this case I am specifically speaking about the Sun/Oracle JVM and the Microsoft .NET Runtime.

Does anybody have feedback?

like image 368
benstpierre Avatar asked Aug 13 '10 15:08

benstpierre


People also ask

What are different garbage collection methods?

There are two ways to do it : Using System. gc() method: System class contain static method gc() for requesting JVM to run Garbage Collector. Using Runtime.

What is garbage collection in C language?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.

What is garbage collection and how does it work in C#?

The garbage collector (GC) manages the allocation and release of memory. The garbage collector serves as an automatic memory manager. You do not need to know how to allocate and release memory or manage the lifetime of the objects that use that memory.

What is the difference between major and minor garbage collection?

Major GC cleans up the old generation. The task of Major GC is as same as the minor GC, but the only difference is minor GC reclaims the memory of the young generation whereas major GC reclaims the memory of the old generation. It is also said that many major GCs are triggered by minor GCs.


1 Answers

The advice you've been given is, broadly speaking, a load of hooey.

Both C# and Java have GCs that attempt to optimise the fast recovery of lots of small objects. They're designed to solve the same problem, they do it in slightly different ways but as a user the technical differences in your approach to using them is minimal, even non-existent for the majority of users.

IDisposable is nothing to do with the GC as such. It's a standard way of naming methods that would otherwise be called close, destroy, dispose, etc., and often are called that in Java. There is a proposal for Java 7 to add something very similar to the using keyword that would call a similar close method.

"Destructors" in C# refers to finalizers - this was done deliberately to confuse C++ programmers. :) The CLR spec itself calls them finalizers, exactly as the JVM does.

There are many ways in which Java and C#/CLR differ (user value types, properties, generics and the whole family of related features known as Linq), but the GC is one area where you can develop a substantial amount of software before you need to worry much about the difference between them.

like image 160
Daniel Earwicker Avatar answered Sep 18 '22 18:09

Daniel Earwicker