Way2Java

Garbage Collection gc() exit(0)

Garbage Collection gc() exit(0)

Summary: In this "Garbage Collection gc() exit(0)", the basics of garbage collection focusing the methods gc() and exit(0) illustrated.

More on Garbage Collection gc() exit(0) methods

Unwanted (not used in the remaining part of the program) objects generated in the process are known as garbage. Java includes a special built-in style of removing the garbage known as garbage collection. Garbage collector is a low-priority service thread that removes the garbage periodically. It is a two step style and the algorithm is known as Mark and Sweep. First, the collector marks those objects that are not referenced anywhere in the program. In the second step, it removes those marked objects. The speed of removal varies depending upon the size of the heap and the microprocessor speed etc.

The programmer cannot force the JVM to go for garbage collection. It is an in-built phenomenon and is done at the discretion of the JVM. Programmer can maximum ask the JVM to go for garbage collection with the following statement, which need not be honored. The methods used are gc() and exit(0) belonging java.lang.System class.

System.gc( );

gc() method of System class advises the JVM to go for garbage collection. It is only an advise as the Programmer is not given any handle by the Designers to force the JVM to go for garbage collection immediately. Garbage collection is an implicit process occurring automatically.

System.exit(0)

exit(0) is a static method of System class which terminates a normal execution of a program. This style is used in AWT GUI to close the window. The zero parameter indicates a normal shutdown. Other than zero indicates abnormal shutdown.

Help to improve this posting "Garbage Collection gc() exit(0)" with your suggestions and comments.

View All java.lang Examples