0

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

3
  • 2
    I have no idea what you are talking about (so I'm marking this as Not a Real Question). Otherwise I'd mark this as "Subjective and Argumentative" or "Off Topic". Commented Jun 8, 2011 at 7:21
  • I disagree that this is "not a real question". Commented Jun 8, 2011 at 7:29
  • I found some drawback in garbage collection in java. Here i thinking to how to over come from this. Commented Jun 8, 2011 at 7:44

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can u send some more details of escape analysis... its intrested.
See the link in my post.
2

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.

2 Comments

But, I need some idea to DELETE this object( ex: O) in code itself. Because here every loop objects are going to unrechable. So compailer is need to inform to programmer or raise error or insert some code automatically... hv any idea?..
You can't delete objects in Java explicitely.
1

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

Comments

0

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 ?

2 Comments

absolutely... but here i`m going to rethinking that is it possible in compile time?... Garbage collector invoke automatically by jvm in unknown time, its the big problem when my important tasking is processing.
Rust is doing cleanup of memory through compile time code injections. Wish Java in future follow similar approach

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.