Alex Rivera | Logout

Why Java and Python garbage collection methods are different?

Asked 2008-08-22T07:35:26.703
67

Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.

But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.

Why does Java choose this strategy and what is the benefit from this?

Is this better than the Python approach?

Edit
Report

2 Answers

8

I think the article "Java theory and practice: A brief history of garbage collection" from IBM should help explain some of the questions you have.

answered 2008-08-22T07:40:12.557
5

Reference counting is particularly difficult to do efficiently in a multi-threaded environment. I don't know how you'd even start to do it without getting into hardware assisted transactions or similar (currently) unusual atomic instructions.

Reference counting is easy to implement. JVMs have had a lot of money sunk into competing implementations, so it shouldn't be surprising that they implement very good solutions to very difficult problems. However, it's becoming increasingly easy to target your favourite language at the JVM.

answered 2008-09-05T20:03:19.737

Your Answer