Why Java and Python garbage collection methods are

2019-01-30 00:53发布

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?

9条回答
We Are One
2楼-- · 2019-01-30 01:12

Late in the game, but I think one significant rationale for RC in python is its simplicity. See this email by Alex Martelli, for example.

(I could not find a link outside google cache, the email date from 13th october 2005 on python list).

查看更多
倾城 Initia
3楼-- · 2019-01-30 01:19

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.

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

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.

查看更多
SAY GOODBYE
4楼-- · 2019-01-30 01:20

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.

查看更多
登录 后发表回答