Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Java language specification allows a dummy gc() method. Why?

Tags:

java

I've a hard time understanding the following:

"The Java language specification allows a dummy gc() method."

Why would the standard do this?
Its effectively making a very important feature of java optional.
This would also mean my same program will behave differently on two different JVM implementations !!! Something totally against Java's important feature of portability.

like image 203
gameover Avatar asked Jan 27 '10 13:01

gameover


5 Answers

Its effectively making a very important feature of java optional.

The GC is not made optional by that in Java. What is made optional is an explicit garbage collection, triggered by a call to gc(). And this is completely acceptable, since explicitly triggering a garbage collection is rarely necessary and interferes with the function of a modern garbage collector.

like image 90
Konrad Rudolph Avatar answered Nov 18 '22 19:11

Konrad Rudolph


Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

Calling gc() is not guaranteed to trigger garbage-collection. I.e. you cant force garbage collection. That's why the method can be dummy.

"The best effort" mentioned above might be "waited for implicit garbage collection".

like image 29
Bozho Avatar answered Nov 18 '22 19:11

Bozho


This would also mean my same program will behave differently on two different JVM implementations !!! Something totally against Java's important feature of portability.

Differences of this sort between different JVM implementations is actually a good thing. It allows improvements in the underlying details that don't affect the correctness of a program and usually improve performance. Garbage collection happens to be an area where the JVM spec focuses on allowing implementation room to basically experiment with different approaches to solving the problem. Sun explicitly states that calling gc() will not force a memory sweep, so programmers can not make the claim that they expect a certain behavior out of every JVM.

like image 4
Kelly S. French Avatar answered Nov 18 '22 19:11

Kelly S. French


The core idea - gc() is optional and you can force to collect garbage if you want.

But still you have the "automatic" garbage collection as it should be. gc() is just a matter of making a program more and more effective

like image 2
Chathuranga Chandrasekara Avatar answered Nov 18 '22 19:11

Chathuranga Chandrasekara


It is usually a good idea to let the JVM figure out when it needs to do garbage collection instead of explicitly telling it to do so. The fact that the Java spec allows a dummy method does not change the fact that Java does garbage collection, it just means that it can ignore requests from you to do it explicitly.

Here is a good (slightly old) article about garbage collection in Java, which has a short paragraph on System.gc(): http://www.ibm.com/developerworks/library/j-jtp01274.html

like image 2
Neal Avatar answered Nov 18 '22 17:11

Neal