Why should you avoid System.exit ()?

I read in Anthony Rizk’s book “Getting Started Developing BlackBerry Development,” although the System.exit() method exits the application, so it’s recommended that you avoid this and properly clear the application from exiting by closing all screens. My question is: why avoid System.exit() ?

+6
source share
4 answers

This is a really interesting question!

There is a difference in the behavior of System.exit() for the Java SE API and the BB Java API:

  • In the Java SE API: the currently running Java virtual machine ends.
  • In the BB Java API: the currently running Java application ends.

Also check out what Carol Hamer and Andrew Davison said in the Blackberry Games Evolution Study:

Warning: the BlackBerry platform does not run your application in a separate virtual machine, which means that you have to be very careful in cleaning. The remnants of an earlier run (for example, static variables and other data stored in memory) could potentially affect subsequent application runs. It also means that theres is a global namespace, so if two classes have the same name, errors can occur.

So yes, there is the only JVM device for BB. And yes, in a BB application, calling System.exit() just stops your application, leaves all your static data in RAM, unless you do a preliminary cleanup .

Thus, you should not avoid System.exit() - this is the legal / correct way to close the BB application , but just do the cleanup before this call .

UPDATE:

by email Oh. I created a test application (using JDE 4.7.0 + Storm 9530 4.7.0 simulator) to check if static material really remains in RAM after calling System.exit() . And it turns out that he no longer stays there. The next time I enter the application, the static variables are zero (as expected, they will be in Java SE). Therefore, it is not clear to me that Carol Hamer and Andrew Davison say that "remnants of an early run (such as static variables and other data still in memory) can potentially affect subsequent runs of the application."

+6
source

This is because it can short circuit your own ordered output methods, for example. flushing buffered output streams / scripts, logging sessions, deleting files, executing DBMS transactions, ...

+2
source

In my opinion, it is incorrect to kill your container by calling System.exit (). The right way to do this is to create the destroy () method, which allows you to clear your threads and close any resources you open.

To put things into laymen, System.exit () may leave lingering data / session information.

+1
source

From what I remember about the development of BB, System.exit () simply closes the application without destroying the objects you created, leaving them in the garbage collector. Therefore, the application will not be effectively deleted from memory. The closing screens, one after the other, will actually free them.

I could figure it out a bit, but there is enough information about the best practices on the net :)

0
source

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


All Articles