Monday, April 19, 2010

Clear Memory In Java Lang

public
static
void main(String[] args) {

    Runtime rt = Runtime.getRuntime();

    System.out.println("1 ## "+rt.freeMemory());

    A a[] = new A[1000];

    for(int i=0;i<1000;i++){

        a[i]=new A();

    }

    System.out.println("2 ## "+rt.freeMemory());

    for(int i=0;i<1000;i++){

        a[i]=null;

    }

    System.out.println("3 set null ## "+rt.freeMemory());

    System.out.println("#################################");

    rt.gc();

    System.out.println("4 call .gc ## "+rt.freeMemory());


 

}

//######################### Out Put #############################

1 ## 4917112

2 ## 4894568

3 set null ## 4894568

#################################

4 call .gc ## 5013408

No comments:

Post a Comment