Smart Java compiler for garbage collection

why the java compiler is no smarter. Suppose this is so, then it can recognize an unreachable object in compiletime and the code itself will clear the garbage. I think this will help to avoid the concept of garbage collection in java (you need to add the DELETE keyword to delete the object). Why is this impossible? ..

0
source share
4 answers

There are many problems that computers may not always give the answer “yes” or “no.” In fact, there are more problems that cannot be solved by computers than problems that can be.

Look at an unsolvable problem and a list of unsolvable problems . There you will find a stopping problem : Stopping problem , which says that the computer cannot even tell if the program is stopped. If you could build a compiler as you described, then this will not be a problem, so you cannot (this can be proved).

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

+1
source

In general, during compilation it is impossible to find out at what point in the program exactly the object stops referencing (so that the compiler can insert the “delete” instruction at that moment).

Java 6 14, Java ( , ), .

, , , "" - ; , . , , Java , , "", ( , ) - , .

+3

. , " " - , . . , . , .

, :

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

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

, , . JVM , [ num, ] .

+2

The name " garbage collection" means that objects that the program no longer needs are " garbage" and can be thrown away.
Garbage collection is the process by which objects without references are deleted. This is not a compilation process.

Garbage collection is a RunTime process. This only happens when objects are created on the heap.

Good tutorial on How garbage collection works in Java?

0
source

Source: https://habr.com/ru/post/1616931/


All Articles