| RE: [aus-dotnet] A Comparison of .NET and Java GC |
- From: David Kean
- Subject: RE: [aus-dotnet] A Comparison of .NET and Java GC
- Date: Wed, 07 Jul 2004 11:45:14 +1000
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
The author Paul Wilson is totally wrong about setting objects to null. He implies that setting objects to null will cause them to be garbage collected early. However this wrong.
In a non-debug application the following code will NOT cause the garbage collector to collect 'myObject' any earlier.
public void MyMethod()
{
MyObject myObject = new MyObject();
myObject.MyMethod();
myObject = null;
MyOtherObject otherObject = new MyObjectObject();
otherObject.AReallyLongMethod();
}
In a debug application the above code COULD effect when 'myObject' is collected, but only because myObject isn't allowed to be collected until the end of the method to help with debugging.
I think he is mistaking setting variables to null and setting fields to null.
The following code could affect when the garbage collector collects '_MyObject' after Dispose is called.
public class MyClass : IDisposable
{
private MyObject _MyObject = new _MyObject;
public MyClass()
{
}
public void Dispose()
{
_MyObject == null;
}
}
-----Original Message-----
From: Nick Lothian [mailto:nl@xxxxxxxxxxxxxxxx]
Sent: Wednesday, 7 July 2004 11:05 AM
To: 'dotnet@xxxxxxxxxxx'
Subject: RE: [aus-dotnet] A Comparison of .NET and Java GC
>
> ".NET GC is Excellent - Better Than Java" -
> http://weblogs.asp.net/pwilson/archive/2004/02/20/77448.aspx
>
> Ok, first 4 probably not exactly what you're after, but the
> last one is an
> interesting read. First 4 may give those asking about the GC
> a bit more info
> on it.
I don't know much about the .NET GC, but I do know the Java GC enough to know that this guy is confusing garbage collection and resource management. I guess that's a common enough confusion, but it isn't good to see it put around as a fact.
He says:
"As for the Dispose method, I learned that this pattern is commonly used and accepted as necessary in Java, but it was never officially made part of Java itself, which is one place .NET is actually better since they learned from Java's experience."
To a certain extend this is true - is you have a resource in Java that you need to clean up after user you need to use try {...} finally {} blocks. IDisposable is a nice piece of syntactic sugar that makes that a bit cleaner. A nice summary of this is at http://www.neward.net/ted/weblog/index.jsp?date=20040123
However, that has NOTHING to do with garbage collection - in fact it is the opposite. IDisposable or try {..} finally {} let you clean up resources that garbage collection do not manage (typical examples are things like managing pooled connections to something where you want to keep the connection objects, but close the network connection).
Back to Garbage Collection:
A good overview of the .NET GC is <http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx> and <http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/default.aspx>
There is probably more documentation available on Java GC, simply because it has been around longer and gone through more revisions. Start with http://java.sun.com/docs/hotspot/gc/index.html and http://java.sun.com/docs/hotspot/gc1.4.2/faq.html
I wrote a blog entry on tuning the Java GC with the Java Jvmstat tool, which includes some more links: <http://www.mackmo.com/nick/blog/java/?permalink=jvmstat.txt>. Note that the Java 1.5 VM (or Java 5 VM as it is now know) does more goal orientated self tuning of its GC algorithm (they call it garbage collector ergonomics). I haven't looked at this in a great deal of detail, but I'd consider it a good thing since GC tuning is currently something of a black art. See http://java.sun.com/j2se/1.5.0/docs/guide/vm/gc-ergonomics.html
If you are really interested in garbage collection I highly recommend running jvmstat against a Java program. It is quite amazing to see the generational GC working - you watch as objects get copied from the Eden space to the survivor pools, and then on to the old and permanent generations. You can see the difference the various GC algorithms make, too.
Nick
Nick
_________________
You are a part of the Australian "dotnet" mailing list.
To unsubscribe send "unsubscribe dotnet" or to re-subscribe send "subscribe dotnet Your Name" in the body of the email to: imailsrv@xxxxxxxxxxx List managed by: http://www.stanski.com
(Click here for more information on the aus-dotnet mailling list)
- Prev by Date: RE: [aus-dotnet] A Comparison of .NET and Java GC
- Next by Date: [aus-dotnet] Thoughts on the User Interface Process Application Block
- Previous by thread: Re: [aus-dotnet] A Comparison of .NET and Java GC
- Next by thread: RE: [aus-dotnet] A Comparison of .NET and Java GC
- Index(es):
