8

I am in a little bit confusion with system.exit. I founded some things about from this link.

but I have some doubts in my mind. If I use system exit, what will happened to the created objects,variable and ect. Are everything get destroyed once I called system.exit? If "Yes" then why we force to the garbage collection before system.exit() ? If "No" how long the created objects are stored in the JVM (memory)? If run the program again after exit from system, what will happened to the previous objects if they not destroyed once I called System.exit();?

Thanks.

Community
  • 1
  • 1
maXfenda
  • 214
  • 3
  • 10
  • any suggestion or advices,? anything warmly welcome. Thanks – maXfenda Aug 12 '13 at 10:43
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/3715967/when-should-we-call-system-exit-in-java – Andrew Martin Aug 12 '13 at 10:45
  • 1
    *"Are everything get destroyed once I called system.exit?"* - Not exactly (or immediately). `System.exit` starts a "normal" termination of the JVM. Part of the process will be to clean and free memory, but by that stage, all the Java code will have been terminated... – MadProgrammer Aug 12 '13 at 10:46
  • AFAIK System.exit just ends the current running JVM so everything in there is lost. Why do you say "we force to the garbage collection before system.exit()"? – Averroes Aug 12 '13 at 10:48
  • @AndrewMartin I am in a little bit confusion with system.exit. I founded some things about from this "link". see carefully I make that link to the old post. I follow it, but some points make doubts. Thats why I make this post. – maXfenda Aug 12 '13 at 10:48
  • @Averroes No I said that because if all the created this gets destroyed after system.exit that means everything get clear with out having garbage. So then why we using garbage collection (System.gc();). – maXfenda Aug 12 '13 at 10:52
  • We call GC within our applications to tell the JVM to clean the memory of the objects we are using (JVM usually has a delimited memory quota). Once we call System.exit we terminate the JVM where our application lives. The memory management of the memory the JVM was using then passes to the OS. – Averroes Aug 12 '13 at 10:59

3 Answers3

6

If I use system exit, what will happened to the created objects,variable and ect. Are everything get destroyed once I called system.exit?

Only user threads are destroyed by a System exit.

why we force to the garbage collection before system.exit() ?

We don't and it wouldn't be very useful as this might not do anything.

how long the created objects are stored in the JVM (memory)?

Until they are no longer needed and a clean up occurs, of the JVM really exits

If run the program again after exit from system, what will happened to the previous objects if they not destroyed once I called System.exit();?

They are destroyed when the program finishes. In any case, every program gets it's own new set of variables even if run multiple times. There is no sharing of variables between programs.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
5

I think in this case it is useful to think of the JVM as a program running on a computer. System.exit() terminates that program. Nothing within the program is kept by the computer's OS or the JVM runtime, though the program, of course, may write things to long-term storage. But variables, created objs, and etc. are all gone, and cannot be restored.

arcy
  • 12,845
  • 12
  • 58
  • 103
5

The short answer of what you should know about exit:

  • It's useful because it's the only way to set the exit status.

  • Generally the only place you should use it is at the end of a main method.

  • It results in JVM termination (killing the process, thus necessarily freeing all memory).

Chris Martin
  • 30,334
  • 10
  • 78
  • 137