In Java, I've done things like the following without thinking much about it:
public class Main {
public void run() {
// ...
}
public static void main(String[] args) {
new Main().run();
}
}
However, recently I've become unsure as to whether doing that is safe. After all, there is no reference to the Main
object after it's created (well, there is the this
reference, but does that count?), so it looks like there's a danger that the garbage collector might delete the object while it's in the middle of executing something. So perhaps the main
method should look like this:
public static void main(String[] args) {
Main m = new Main();
m.run();
}
Now, I'm pretty sure that the first version works and I've never had any problems with it, but I'd like to know if it's safe to do in all cases (not only in a specific JVM, but preferably according to what the language specification says about such cases).
If an object method is being executed, it means someone is in possession of that reference. So no, an object can't be GC'd while a method is being executed.
For the most part garbage collection is transparent. It's there to remove the unnecessary complication of manual memory management. So, it will appear not to be collected, but what actually happens is more subtle.
Trivially, a compiler may completely elide the construction of the object. (By compiler, I mean a lower level compiler than javac. The bytecodes will be a literal transliteration of the source.) More obscurely, garbage collection typically runs in separate threads and actually remove the unaccessed object as a method on it is being run.
How can this be observed? The usual suspect in a finaliser. It may run concurrently with a method running on the object. Typically you would get around this problem with synchronized
blocks in both the finaliser and the normal methods, which introduces the necessary happens-before relationship.
m is just a variable which has reference stored. This will be used by programmer to use the same object further to write logic on same object.
While execution, program will be converted to OP-CODES / INSTRUCTIONS .
These INSTRUCTION will have the reference to object(it is a memory location after all).
In case m is present, location of object will be accessed via INDIRECT REFERENCE.
If m is absent, the reference is DIRECT.
So here, object is being used by CPU registers, irrespective of use of reference variable.
This will be available till the flow of execution is in scope of main() function.
Further, as per GC process, GC only removes objects from memory, once GC is sure that the object will not be used any further.
Every object is given chance to survive a number of times(depends on situation and algorithm). Once the number of chances are over, then only object is garbage collected.
In simpler words, objects which were used recently, will be given chance to stay in memory.
Old objects will be removed from memory.
So given your code:
public class Main {
public void run() {
// ...
}
public static void main(String[] args) {
new Main().run();
}
}
the object will not be garbage collected.
Also, for examples, try to look at anonymous class examples. Or examples from event handling in AWT / SWING.
There, you will find a lot of usage like this.