廣告

2015年3月5日 星期四

[java] monitoring menory & try list.clear() vs new List : Which one will be better


package test_fb_api;

import java.util.ArrayList;
import java.util.List;

public class testMemory {

    public static void main(String[] args) {

        int kb = 1024;

        //Getting the runtime reference from system
        Runtime runtime = Runtime.getRuntime();

        //Print used memory
        System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / kb);

        //Print free memory
        System.out.println("Free Memory:" + runtime.freeMemory() / kb);

        //Print total available memory
        System.out.println("Total Memory:" + runtime.totalMemory() / kb);

        //Print Maximum available memory
        System.out.println("Max Memory:" + runtime.maxMemory() / kb);

        List list = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            for (int j = 0; j < 10000; j++) {
                list.add("abcdefghij" + i + j);
            }
            if (i % 200 == 0) {
                try {
                 System.gc();
                    Thread.sleep(100);
                    System.out.println("Free Memory:" + runtime.freeMemory() / kb + " Kb");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        System.out.println("done");
    }
}


you can add code to line38 to monitoring
1. list.clear
2. list = new list();

ref:http://stackoverflow.com/questions/6757868/map-clear-vs-new-map-which-one-will-be-better

沒有留言:

張貼留言