Why Java and Python garbage collection methods are different?
<p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p>

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

<p>Why does Java choose this strategy and what is the benefit from this?</p>

<p>Is this better than the Python approach?</p>

<p>There are drawbacks of using reference counting. One of the most mentioned is circular references: Suppose A references B, B references C and C references B. If A were to drop its reference to B, both B and C will still have a reference count of 1 and won't be deleted with traditional reference counting. CPython (reference counting is not part of python itself, but part of the C implementation thereof) catches circular references with a separate garbage collection routine that it runs periodically...</p>

<p>Another drawback: Reference counting can make execution slower. Each time an object is referenced and dereferenced, the interpreter/VM must check to see if the count has gone down to 0 (and then deallocate if it did). Garbage Collection does not need to do this.</p>

<p>Also, Garbage Collection can be done in a separate thread (though it can be a bit tricky). On machines with lots of RAM and for processes that use memory only slowly, you might not want to be doing GC at all! Reference counting would be a bit of a drawback there in terms of performance...</p>
