Runtime獲取內存jvm佔用情況

 

package org.example.menory;

import java.util.ArrayList;

class Student {
    int age = 1;
    String name = "xx";
}

public class TestMemory {
    public static void main(String[] args) throws Exception {
        ArrayList<Student> arrayList = new ArrayList<>();

        while (true) {
            for (int i = 0; i < 100000; i++) {
                arrayList.add(new Student());
            }

            int curMemory = (int) (Runtime.getRuntime().totalMemory() / 1000 / 1000);
            int totalMemory = (int) (Runtime.getRuntime().maxMemory() / 1000 / 1000);

            System.out.println(curMemory + "/" + totalMemory);
            Thread.sleep(80);


            if (arrayList.size() > 1000000) {
                arrayList.clear();
                System.out.println("clear!!!");
            }


        }
    }
}

52/259
52/259
52/259
52/259
52/259
52/259
52/259
52/259
52/259
52/259
52/259
clear!!!










 

可以看出來,java還是比較強的,如果沒有內存泄漏,一直不釋放這種情況(比如一直往ArrayList中添加),那麼還是保持內存十分的穩定的。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章