Example usage for java.lang System gc

List of usage examples for java.lang System gc

Introduction

In this page you can find the example usage for java.lang System gc.

Prototype

public static void gc() 

Source Link

Document

Runs the garbage collector in the Java Virtual Machine.

Usage

From source file:Main.java

public static void main(String[] args) {

    System.gc();
    System.out.println("Cleanup completed...");
}

From source file:Main.java

public static void main(String[] args) {

    System.gc();

}

From source file:MainClass.java

public static void main(String args[]) {
    map = new WeakHashMap();
    map.put("A", "B");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("A")) {
                try {
                    Thread.sleep(1000);
                    System.gc();
                } catch (InterruptedException ignored) {
                }//from w  w w.  j  av  a 2s . c om
                System.out.println("Has A");
                System.gc();
            }
        }
    };
    Thread t = new Thread(runner);
    t.start();
    System.out.println("Main waiting");
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
    System.gc();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Object f = new Object() {
        public void finalize() {
            System.out.println("Running finalize()");
        }//w w w  .j  a v a  2 s .c  o m
    };
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            System.out.println("Running Shutdown Hook");
        }
    });

    f = null;
    System.gc();

    System.out.println("Calling System.exit()");
    System.exit(0);
}

From source file:Main.java

public static void main(String args[]) {
    final Map<String, String> map = new WeakHashMap<String, String>();
    map.put(new String("A"), "B");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("A")) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignored) {
                }//from  www .java 2s.c  om
                System.gc();
            }
        }
    };

    Thread t = new Thread(runner);
    t.start();
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
}

From source file:Weak.java

public static void main(String args[]) {
    final Map map = new WeakHashMap();
    map.put(new String("Java2s"), "www.java2s.com");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("Java2s")) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignored) {
                }//from w  w w .j av  a2  s. co m
                System.out.println("Waiting");
                System.gc();
            }
        }
    };
    Thread t = new Thread(runner);
    t.start();
    System.out.println("Main waiting");
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }/*  w w w.  j  av a2  s  .  c om*/
    };
    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = new SoftReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }/*w w  w  .  j a  v a 2 s  . c  o m*/
    };

    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = new SoftReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }/*from w w w  . ja v a  2  s. c  o m*/
    };

    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = null;
    System.out.println("Testing PhantomReference.");
    reference = new PhantomReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }//from w w  w.  j  ava  2  s .  c o  m
    };

    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = null;
    System.out.println("Testing WeakReference.");
    reference = new WeakReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}