开发者

Smart Java compiler for garbage collection [closed]

开发者 https://www.devze.com 2023-03-11 15:12 出处:网络
开发者_JS百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. F
开发者_JS百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

why java compiler is not more smart. Suppose it is, then it can find out the unreachable object at compiletime and code itself clean the garbage. I`m thinking that this will helps to avoid garbage collection concept in java(need to add DELETE keyword for delete the object). Why it is not possible?..


In general, it is not possible to know at compile time at what point in a program exactly an object becomes unreferenced (so that the compiler could insert a "delete" statement at that point).

Since Java 6 update 14, Java has escape analysis (as an experimental feature; it might become a standard feature in later versions) that partly solves this problem.

What happens with escape analysis is that the compiler checks if objects "escape" from some local scope; for example, local variables inside a method. If the compiler discovers that an object doesn't escape, Java will allocate the object on the stack instead of on the heap, which means that it will be discarded "for free" when the method returns (at that moment, the stack frame of the method ends and the object is discarded) - so the garbage collector doesn't have to do anything to clean up the object.


Garbage collection CANNOT be done at compile time. This is because "compile time" is the time when your source is converted to byte code. There is no existence of the running program in memory. And since there is no running program in memory, there are no objects allocated. Hence no garbage collection.

For example if you have the following code:

int num = new Scanner(System.in).readInt();

for(int i = 0; i < num; i++)
{
    Object o = new Object();
}

Now at compile time how will the compiler know how many objects will be created and how many should be garbage collected. It is only at run time when you run the program will the JVM know how many objects were created [based on the value num, inputted by user] and how many should be garbage collected.


There is a lot of problems computers can't always give a yes or no answer to. Actually, there is more problems which can't be solved by computers than problems which can.

Look at Undecidable problem and List of undecidable problems. There you will find the halting problem: Halting problem which says that a computer can't even say whether or not a program stops. If you could construct a compiler as you describe, then this wouldn't be a problem - so you can't (it can be proved).

Furthermore, there is a theorem which says you NEVER can make an optimal compiler ;-) they can always be improved :-)


The name "garbage collection" implies that objects no longer needed by the program are "garbage" and can be thrown away.
Garbage collection is a process by which unreferenced objects are removed. It is not a compile time process.

Garbage collection is RunTime process. It happens only when objects are created on heap.

A good tutorial on How Garbage collection works in Java ?

0

精彩评论

暂无评论...
验证码 换一张
取 消