Friday, June 8, 2012

Write a program to find the total memory, free memory and free memory after executing garbage collection gc(()).?


♣♣♣ WRITE A PROGRAM TO FIND THE TOTAL MEMORY, FREE MEMORY AND FREE MEMORY AFTER EXECUTING GARBAGE COLLECTION gc(()).♣♣♣

import java.io.*;

class pro12
        {
                public static void main(String ar[]) throws IOException
                {
                InputStreamReader a =new InputStreamReader(System.in);
                BufferedReader b=new BufferedReader(a);
               Runtime rn=Runtime.getRuntime();
               long mem1=0,mem2=0;
                           Integer arr[]=new Integer[1000];
                           System.out.println("\n\t\tTotal Memory is "+rn.totalMemory());
                           mem1=rn.freeMemory();
                           System.out.println("\n\tInital Free Memory is "+mem1);
                           rn.gc();
                           mem1=rn.freeMemory();
                           System.out.println("\n\tFree Memory after Garbage Collector "+mem1);
                           for(int i=0;i<1000;i++)
                           arr[i]=new Integer(i);
                           mem2=rn.freeMemory();
                           System.out.println("\n\tFree Memory after allocation "+mem2);
                           System.out.println("\n\tMemory used by alloction "+ (mem1-mem2));
                           for(int i=0;i<1000;i++)
                           arr[i]=null;
                           rn.gc();
                           mem2=rn.freeMemory();
            System.out.println("\n\tFree Memory after collecting discarded integers  "+mem2);
                }
        }

OUTPUT :-
                        Total Memory is 5177344
                        Initial Free Memory is 4949960
                        Free Memory after Garbage Collector 5039128
                        Free Memory after allocation 5020712
                        Memory used by allocation 18416
                        Free Memory after collecting discarded integer 5039128

No comments:

Post a Comment