Garbage Collection gc() exit(0)

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

7 thoughts on “Garbage Collection gc() exit(0)

    1. S. Nageswara Rao, Corporate Trainer

      This site, I developed with passion. This much subject to get, you may have to require to browse nearly 20 sites. Moreover, concept is first explained in simple English leaving the jargon of technical words, program is written, output screen is given, notes on the program coding is illustrated and finally similar related topic links are given. This much trouble, I think rare to be taken.

    1. S. Nageswara Rao, Corporate Trainer

      Finalizing Objects

      Before an object is garbage collected, the Java runtime system gives the object a chance to clean up after itself. This step is known as finalization and is acheived through a call to the object’s finalize() method. The object should override the finalize() method to perform any final cleanup tasks such as freeing system resources like files and sockets. For information about the finalize() method, see Writing a finalize() Method(in the Writing Java Programs trail).
      You can force object finalization to occur by calling System’s runFinalization() method.

      System.runFinalization();
      This method calls the finalize() methods on all objects that are waiting to be garbage collected.
      Running the Garbage Collector

      You can ask the garbage collector to run at any time by calling System’s gc() method:
      System.gc();
      You might want to run the garbage collector to ensure that it runs at the best time for your program rather than when it’s most convenient for the runtime system to run it. For example, your program may wish to run the garbage collector right before it enters a compute or memory intensive section of code or when it knows there will be some idle time. The garbage collector requires about 20 milliseconds to complete its task so, your program should only run the garbage collector when doing so will have no performance impact on your program–that is, that your program anticipates that the garbage collector will have enough time to finish its job.

Leave a Reply

Your email address will not be published. Required fields are marked *