<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>

[{'answer_id': 21937, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 3, 'selected': False, 'text': '<p>I think the article "<a href="http://www.ibm.com/developerworks/java/library/j-jtp10283/" rel="noreferrer">Java theory and practice: A brief history of garbage collection</a>" from IBM should help explain some of the questions you have.</p>\n'}, {'answer_id': 21964, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 7, 'selected': True, 'text': "<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>\n\n<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>\n\n<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>\n"}, {'answer_id': 22219, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 4, 'selected': False, 'text': '<p>Darren Thomas gives a good answer.  However, one big difference between the Java and Python approaches is that with reference counting in the common case (no circular references) objects are cleaned up immediately rather than at some indeterminate later date.</p>\n\n<p>For example, I can write sloppy, non-portable code in CPython such as</p>\n\n<pre><code>def parse_some_attrs(fname):\n    return open(fname).read().split("~~~")[2:4]\n</code></pre>\n\n<p>and the file descriptor for that file I opened will be cleaned up immediately because as soon as the reference to the open file goes away, the file is garbage collected and the file descriptor is freed.  Of course, if I run Jython or IronPython or possibly PyPy, then the garbage collector won\'t necessarily run until much later; possibly I\'ll run out of file descriptors first and my program will crash.</p>\n\n<p>So you SHOULD be writing code that looks like</p>\n\n<pre><code>def parse_some_attrs(fname):\n    with open(fname) as f:\n        return f.read().split("~~~")[2:4]\n</code></pre>\n\n<p>but sometimes people like to rely on reference counting to always free up their resources because it can sometimes make your code a little shorter.</p>\n\n<p>I\'d say that the best garbage collector is the one with the best performance, which currently seems to be the Java-style generational garbage collectors that can run in a separate thread and has all these crazy optimizations, etc.  The differences to how you write your code should be negligible and ideally non-existent.</p>\n'}, {'answer_id': 23703, 'author': 'ckpwong', 'author_id': 2551, 'author_profile': 'https://Stackoverflow.com/users/2551', 'pm_score': 2, 'selected': False, 'text': '<p>The latest Sun Java VM actually have multiple GC algorithms which you can tweak.  The Java VM specifications intentionally omitted specifying actual GC behaviour to allow different (and multiple) GC algorithms for different VMs.</p>\n\n<p>For example, for all the people who dislike the "stop-the-world" approach of the default Sun Java VM GC behaviour, there are VM such as <a href="http://www-01.ibm.com/software/webservers/realtime/" rel="nofollow noreferrer">IBM\'s WebSphere Real Time</a> which allows real-time application to run on Java.</p>\n\n<p>Since the Java VM spec is publicly available, there is (theoretically) nothing stopping anyone from implementing a Java VM that uses CPython\'s GC algorithm.</p>\n'}, {'answer_id': 46780, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 74327, 'author': 'mfx', 'author_id': 8015, 'author_profile': 'https://Stackoverflow.com/users/8015', 'pm_score': 3, 'selected': False, 'text': '<p>Garbage collection is faster (more time efficient) than reference counting, if you have enough memory. For example, a copying gc traverses the "live" objects and copies them to a new space, and can reclaim all the "dead" objects in one step by marking a whole memory region. This is very efficient, <em>if</em> you have enough memory. Generational collections use the knowledge that "most objects die young"; often only a few percent of objects have to be copied.</p>\n\n<p>[This is also the reason why gc can be faster than malloc/free]</p>\n\n<p>Reference counting is much more space efficient than garbage collection, since it reclaims memory the very moment it gets unreachable. This is nice when you want to attach finalizers to objects (e.g. to close a file once the File object gets unreachable). A reference counting system can work even when only a few percent of the memory is free. But the management cost of having to increment and decrement counters upon each pointer assignment cost a lot of time, and some kind of garbage collection is still needed to reclaim cycles.</p>\n\n<p>So the trade-off is clear: if you have to work in a memory-constrained environment, or if you need precise finalizers, use reference counting. If you have enough memory and need the speed, use garbage collection.</p>\n'}, {'answer_id': 196487, 'author': 'Luke Quinane', 'author_id': 18437, 'author_profile': 'https://Stackoverflow.com/users/18437', 'pm_score': 5, 'selected': False, 'text': '<p>Actually reference counting and the strategies used by the Sun JVM are all different types of garbage collection algorithms.</p>\n\n<p>There are two broad approaches for tracking down dead objects: tracing and reference counting. In tracing the GC starts from the "roots" - things like stack references, and traces all reachable (live) objects. Anything that can\'t be reached is considered dead. In reference counting each time a reference is modified the object\'s involved have their count updated. Any object whose reference count gets set to zero is considered dead.</p>\n\n<p>With basically all GC implementations there are trade offs but tracing is usually good for high through put (i.e. fast) operation but has longer pause times (larger gaps where the UI or program may freeze up). Reference counting can operate in smaller chunks but will be slower overall. It may mean less freezes but poorer performance overall.</p>\n\n<p>Additionally a reference counting GC requires a cycle detector to clean up any objects in a cycle that won\'t be caught by their reference count alone. Perl 5 didn\'t have a cycle detector in its GC implementation and could leak memory that was cyclic.</p>\n\n<p>Research has also been done to get the best of both worlds (low pause times, high throughput):\n<a href="http://cs.anu.edu.au/~Steve.Blackburn/pubs/papers/urc-oopsla-2003.pdf" rel="noreferrer">http://cs.anu.edu.au/~Steve.Blackburn/pubs/papers/urc-oopsla-2003.pdf</a></p>\n'}, {'answer_id': 1604520, 'author': 'David Cournapeau', 'author_id': 11465, 'author_profile': 'https://Stackoverflow.com/users/11465', 'pm_score': 0, 'selected': False, 'text': '<p>Late in the game, but I think one significant rationale for RC in python is its simplicity. See this <a href="http://mail.python.org/pipermail/python-list/2005-October/921938.html" rel="nofollow noreferrer">email by Alex Martelli</a>, for example.</p>\n\n<p>(I could not find a link outside google cache, the email date from 13th october 2005 on python list).</p>\n'}, {'answer_id': 7826363, 'author': 'Alejandro VD', 'author_id': 780279, 'author_profile': 'https://Stackoverflow.com/users/780279', 'pm_score': 3, 'selected': False, 'text': '<p>One big disadvantage of Java\'s tracing GC is that from time to time it will  "stop the world" and freeze the application for a relatively long time to do a full GC. If the heap is big and the the object tree complex, it will freeze for a few seconds. Also each full GC visits the whole object tree over and over again, something that is probably quite inefficient. Another drawback of the way Java does GC is that you have to tell the jvm what heap size you want (if the default is not good enough); the JVM derives from that value several thresholds that will trigger the GC process when there is too much garbage stacking up in the heap.</p>\n\n<p>I presume that this is actually the main cause of the jerky feeling of Android (based on Java), even on the most expensive cellphones, in comparison with the smoothness of iOS (based on ObjectiveC, and using RC). </p>\n\n<p>I\'d love to see a jvm option to enable RC memory management, and maybe keeping GC only to run as a last resort when there is no more memory left. </p>\n'}]
['https://Stackoverflow.com/questions/21934', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1556/']