本文目錄一覽:
java內存分析
很簡單,構造時候就已經”Hello World”做了臨時存儲了
String temp = “Hello World”;
String str = new String(temp)
如何分析java的內存佔用情況
hi:
虛擬機的內存情況查看,使用Runtime類進行。如下:
//虛擬機內存使用量查詢
class RamRun implements Runnable{
private Runtime runtime;
public void run(){
try{
runtime=Runtime.getRuntime();
System.out.println(“處理器的數目”+runtime.availableProcessors());
System.out.println(“空閑內存量:”+runtime.freeMemory()/ 1024L/1024L + “M av”);
System.out.println(“使用的最大內存量:”+runtime.maxMemory()/ 1024L/1024L + “M av”);
System.out.println(“內存總量:”+runtime.totalMemory()/ 1024L/1024L + “M av”);
}catch(Exception e){
e.printStackTrace();
}
}
}
java內存分析(棧堆)
首先SuperWords a1=new SuperWords();
SubWords a2=new SubWords();
分別在棧中產生了一個內存塊a1指向堆中的SuperWords和一個內存塊a2指向堆中的SubWords!因為SubWords是繼承SuperWords的!所以它在內存中的圖形為SuperWords內存塊中有個SubWords的內存塊!
a1.set_words1(“cool”);
在a1指向的堆塊new出來的內存中的屬性words1值賦為cool!
a2.set_words2(“beautiful”);
在a2指向的堆塊中new出來的內存中的屬性words2的值賦為beautiful!
a1.show_message1();
調用 System.out.println(“The whole words is “+words1+” “+words2); 打印
因為words2沒有賦值所以輸出為:The whole words is cool null
a2.show_message2();
調用System.out.println(“The whole words is “+words2+” “+words1); 打印
因為word1沒有賦值所以輸出為:The whole words is beautiful null!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/183006.html