资源描述:
《java程序执行内存分析》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、JAVA程序执行内存分析内存分析内存中的存放codesegmentdatasegmentstackheap存放代码静态变量,字符串变量局部变量new出来的东西Birthdate类classBirthDate{privateintday;privateintmonth;privateintyear;publicBirthDate(intd,intm,inty){day=d;month=m;year=y;}publicvoidsetDay(intd){day=d;}publicvoidsetMonth(intm){month=m;}publicvoidsetY
2、ear(inty){year=y;}publicintgetDay(){returnday;}publicintgetMonth(){returnmonth;}publicintgetYear(){returnyear;}publicvoiddispaly(){System.out.println(day+"-"+month+"-"+year);}}Test类publicclassTest{publicstaticvoidmain(String[]args){Testtest=newTest();intdate=9;BirthDated1=newBirth
3、Date(7,7,1970);BirthDated2=newBirthDate(1,1,2000);test.change1(date);test.change2(d1);test.chang3(d2);System.out.println("date="+date);d1.display();d2.display();}publicvoidchang1(inti){i=1234;}publicvoidchang2(BirthDateb){b=newBirthDate(22,2,2004);}publicvoidchang3(BirthDateb){b.s
4、etDay(22);}}内存中变量存放说明Testtest=newTest();test为局部变量,指向new出来的对象test:xxxstackheap内存中变量存放说明intdate;定义局部变量,占一块内存stackdate:9内存中变量存放说明BirthDated1=newBirthDate(7,7,1970);局部变量d1,指向new出来的对象stackd1:xxxy←1970m←7d←7heapdaymonthyear构造方法调用结束后,d,m,y将被释放内存中变量存放说明test.change1(date);调用Test中的change1方法
5、stackdate:9ii:9i:1234传值赋值调用结束,i被释放内存中变量存放说明test.change2(d1);heapstackd1:xxxb:xxxday:7month:7year:1970传地址new:xxxday:22month:2year:2004传地址123456方法调用结束后b和new消失7内存中变量存放说明test.chang3(d2);stackheapd2:xxxb:xxxday:1month:1year:2000d:221324传值传值方法调用结束后b和d消失5三维坐标中的点classPoint{intx,y,z;Point(
6、inta,intb,intc){x=a;y=b;z=c;}voidgetDistance(Pointp){return(x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z);}}Pointp1=newPoint(1,2,3);System.out.println(""+getDistance(newPoint(0,0,0)));上例内存分析System.out.println(""+getDistance(newPoint(0,0,0)));stackheapx:1y:2z:3x:0y:0z:0p1:xxxp:xxx
7、132传地址调用结束后p被释放this关键字的引用publicclassLeaf{inti=0;Leaf(inti){this.i=i;}Leafincreament(){i++;returnthis;}voidprint(){System.out.println("i="+i);}publicstaticvoidmain(String[]args){Leafleaf=newLeaf(100);leaf.increament().increament().print();}}this关键字的引用Leafleaf=newLeaf(100);stackheap
8、thisi:100i:100调用结束后i被释放leaf:xxx想象