What are the fundamental differences between garba

2019-03-14 07:41发布

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?

3条回答
We Are One
2楼-- · 2019-03-14 08:02

He's backwards on destructors. You need to not use destructors in C# unless vital. And if you do use them, you should call SuppressFinalize() if you know the object is in a state where the destructor code is no longer needed (most often because the same clean-up happened in a call to IDisposable.Dispose(). If an object has a destructor and SuppressFinalize has not been called, it will live longer (so that it can have that destructor called).

The Garbage Collector most certainly can be relied upon. It can't be relied upon to call a destructor, or to do so within a certain amount of time, but that's not a matter of it not being reliable, it's a matter of it being reliable in collecting garbage which is its job!

I don't know much about the Java Garbage Collector, and I have no doubt that he's right in saying they can't be thought of like each other when you're getting down to the finer details, though I would hope for the sake of Java coders that it can be thought of like the .NET one most of the time - which is to not think of it at all, generally you don't have to.

查看更多
Explosion°爆炸
3楼-- · 2019-03-14 08:04

I'm afraid your coworker is incorrect, but don't take my word for it. Lets have a link fest!

Here are some good articles on GC: http://msdn.microsoft.com/en-us/magazine/bb985010.aspx http://msdn.microsoft.com/en-us/magazine/bb985011.aspx

Also, Maoni's WebLog has some great stuff (will bring you up to date as well, since the articles above are quite old): http://blogs.msdn.com/b/maoni/

Also, just this week, Raymond Chen is doing a series on GC: http://blogs.msdn.com/b/oldnewthing/archive/2010/08/13/10049634.aspx

Here's a good discussion on using Dispose and Finalization: http://www.bluebytesoftware.com/blog/2005/04/08/DGUpdateDisposeFinalizationAndResourceManagement.aspx

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-14 08:10

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.

查看更多
登录 后发表回答