Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practice of exit(0) in C and System.exit(0) in Java

To use exit(0) in C is not a good practice, if there are alternatives, since it does not free resources for example. But to use System.exit(0) in Java - how is it here? Could one trust the garbage collector in this context?

C language:

 exit(0);

Java:

 System.exit(0)
like image 743
user2991252 Avatar asked Nov 24 '13 22:11

user2991252


People also ask

What does System exit 0 do in Java?

As mentioned earlier, System. exit(0) method terminates JVM which results in termination of the currently running program too. Status is the single parameter that the method takes. If the status is 0, it indicates the termination is successful.

What is the difference between System exit 0 and System exit 1 in Java?

exit(0) : Generally used to indicate successful termination. exit(1) or exit(-1) or any other non-zero value – Generally indicates unsuccessful termination. Note : This method does not return any value.

Is it OK to use System exit in Java?

exit() or Runtime. exit(), turning it into an exception rather than shutting down the whole JVM. You are relatively safe to use System. exit() in command-line Java application, as they run on their own JVM and can be used to signal success and failure by returning different status code in case of failure.

Can we use exit 0 in C?

exit is a jump statement in C/C++ language which takes an integer (zero or non zero) to represent different exit status. Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been executed without any error or interrupt.


1 Answers

But to use System.exit(0) in java - how is it here? Could one trust the garbage-collector in this context?

When you call System.exit in Java, the garbage collector is not normally run1. However, in any JVM that I've ever heard of, there is something else that reclaims all of the objects that were allocated. (Typically it is handled at the operating system level.)

The fact that the GC doesn't run is only significant if you are relying on object finalizers to so something important before the JVM terminates.

Hypothetically, if your Java application used JNI (etc) to call native methods, then those methods could access system resources that might be problematic. However:

  1. As a general rule the operating system does take care of such things. At least it does for modern versions of Linux and UNIX, AFAIK.

  2. The garbage collector has no knowledge of those resources anyway. If the OS can't reclaim them, then the Java garbage collector won't help.

If you did need to clean up such resources acquired by a Java program (via native code) then the best approach would be to implement the cleanup in native code methods, and use a "shutdown hook" to run them. The shutdown hooks will be run if you call System.exit.


1 - A garbage collection will be performed on JVM exit if you have previously called runFinalizersOnExit(true). However, this is a deprecated method. The Oracle site explains it like this:

Q: Why is Runtime.runFinalizersOnExit deprecated?

A: Because it is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock. While this problem could be prevented if the class whose objects are being finalized were coded to "defend against" this call, most programmers do not defend against it. They assume that an object is dead at the time that its finalizer is called.

Further, the call is not "thread-safe" in the sense that it sets a VM-global flag. This forces every class with a finalizer to defend against the finalization of live objects!

In short, this is a dangerous approach, and it won't directly deal with the kind of resources that the OP is worried about.

like image 154
Stephen C Avatar answered Sep 24 '22 11:09

Stephen C