Package tigers;
Public class tiger14 {
Public static void main (string [] args) {int [] TimesArray = {10, 100, 1000, 1000}; Execute ("java.lang.stringbuffer"; Execute ("java.lang.string", TimesArray ); Execute ("java.lang.object", TimesArray);} private static void execute (string classname) {try {system.out.println ("< >>>>>> TEST: [ " ClassName "] "); for (INT i: TimesArray) {long buy = usedmemory (classname, i); System.out.Println (" i "Times)");}} catch (Exception e) {e.printStackTrace ();}} private static long usedMemory (String className, int times) throws Exception {Class c = Class.forName (className); int size = times; Object [] obs = new Object [ Times]; // Rungc (); rt.gc (); long start = rt.freeMemory (); for (int i = 0; i Private static void rungc () throws exception {// Attempt to force run garbage collection. Long Past = long.max_value, now = rt.freeMemory (); for (int i = 0; (Past> NOW) && (i <500); i ) {// only after garbage collection does implement, the remaining Memory will be greater than the remaining memory, and the for loop will terminate. Rt.Runfinalization (); // enforces the Finalize () method of the object to prepare for garbage collection. Rt.gc (); // Try to activate the garbage collection thread. Thread.yield (); // delays the current thread to make the garbage collection thread to execute opportunities. PAST = now; now = rt.freeMemory (); // Update the current memory usage status}}}} Garbage recycling using the runtime.gc () method: < >>>>>> TEST: [Java.lang.StringBuffer] 112 (10 Times) 64 (100 Times) 64 (1000 Times) 63 (10000 Times) < >>>>>> TEST: [Java. Lang.String] 184 (10 Times) 40 (100 Times) 40 (1000 Times) 35 (10000 Times) < >>>>>> Test: [Java.lang.Object] 31 (10 Times) 14 (100 Times) 8 (1000 Times) 6 (10000 Times) Implementing Garbage Recycling using a custom RUNGC () method: < >>>>>> TEST: [Java.lang.StringBuffer] -924 (10 Times) 61 (100 Times) 64 (1000 Times) 63 (10000 Times) < >>>>>> TEST: [Java.lang.String] 200 (10 Times) 38 (100 Times) 40 (1000 Times) 35 (10000 Times) < >>>>>> TEST: [Java.lang.Object] 47 (10 Times) 11 (100 Times) 6 (1000 Times) 8 (10000 Times)