首页 > 编程知识 正文

java学习笔记之内存分析,java程序内存分析

时间:2023-12-29 20:31:51 阅读:330794 作者:APTL

本文目录一览:

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!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。