Java Tutorial/Language/Garbage Collection

Материал из Java эксперт
Перейти к: навигация, поиск

Run object finalization using System class

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   System.runFinalization();
 }

}</source>





Run the garbage collector using System class

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   System.gc();
 }

}</source>





The Lifetime of an Object

  1. The process of disposing of dead objects is called garbage collection.
  2. Encouraging the Java Virtual Machine (JVM) to do some garbage collecting and recover the memory.



   <source lang="java">

class Sphere {

 double radius; // Radius of a sphere
 Sphere() {
 }
 // Class constructor
 Sphere(double theRadius) {
   radius = theRadius; // Set the radius
 }

} public class MainClass {

 public static void main(String[] arg){
   Sphere sp = new Sphere();
   
   System.gc();
 }

}</source>