《JAVA程序执行内存分析.ppt》由会员分享,可在线阅读,更多相关《JAVA程序执行内存分析.ppt(66页珍藏版)》请在三一办公上搜索。
1、JAVA程序执行内存分析,内存分析,内存中的存放,code segment,data segment,stack,heap,存放代码,静态变量,字符串变量,局部变量,new出来的东西,Birthdate类,class BirthDate private int day;private int month;private int year;public BirthDate(int d,int m,int y)day=d;month=m;year=y;public void setDay(int d)day=d;public void setMonth(int m)month=m;public v
2、oid setYear(int y)year=y;public int getDay()return day;public int getMonth()return month;public int getYear()return year;public void dispaly()System.out.println(day+-+month+-+year);,Test类,public class Test public static void main(String args)Test test=new Test();int date=9;BirthDate d1=new BirthDate
3、(7,7,1970);BirthDate d2=new BirthDate(1,1,2000);test.change1(date);test.change2(d1);test.chang3(d2);System.out.println(date=+date);d1.display();d2.display();public void chang1(int i)i=1234;public void chang2(BirthDate b)b=new BirthDate(22,2,2004);public void chang3(BirthDate b)b.setDay(22);,内存中变量存放说
4、明,Test test=new Test();test为局部变量,指向new出来的对象,test:xxx,stack,heap,内存中变量存放说明,int date;定义局部变量,占一块内存,stack,date:9,内存中变量存放说明,BirthDate d1=new BirthDate(7,7,1970);局部变量d1,指向new出来的对象,stack,d1:xxx,y1970,m7,d7,heap,day,month,year,构造方法调用结束后,d,m,y将被释放,内存中变量存放说明,test.change1(date);调用Test中的change1方法,stack,date:9,i
5、,i:9,i:1234,传值,赋值,调用结束,i被释放,内存中变量存放说明,test.change2(d1);,heap,stack,d1:xxx,b:xxx,day:7,month:7,year:1970,传地址,new:xxx,day:22,month:2,year:2004,传地址,1,2,3,4,5,6,方法调用结束后b和new消失,7,内存中变量存放说明,test.chang3(d2);,stack,heap,d2:xxx,b:xxx,day:1,month:1,year:2000,d:22,1,3,2,4,传值,传值,方法调用结束后b和d消失,5,三维坐标中的点,class Poi
6、nt int x,y,z;Point(int a,int b,int c)x=a;y=b;z=c;void getDistance(Point p)return(x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z);Point p1=new Point(1,2,3);System.out.println(+getDistance(new Point(0,0,0);,上例内存分析,System.out.println(+getDistance(new Point(0,0,0);,stack,heap,x:1,y:2,z:3,x:0,y:0,z:0,p1:x
7、xx,p:xxx,1,3,2,传地址,调用结束后p被释放,this关键字的引用,public class Leaf int i=0;Leaf(int i)this.i=i;Leaf increament()i+;return this;void print()System.out.println(i=+i);public static void main(String args)Leaf leaf=new Leaf(100);leaf.increament().increament().print();,this关键字的引用,Leaf leaf=new Leaf(100);,stack,hea
8、p,this,i:100,i:100,调用结束后i被释放,leaf:xxx,想象this在类中,this关键字的引用,leaf.increament().increament().print();,leaf:xxx,this,i:102,leaf1:xxx,leaf2:xxx,this是当前对象的引用,stack,heap,this关键字的引用,leaf.increament().increament().print();相当于以下程序段:leaf.increament();leaf.increament();leaf.print();leaf.increament().increament(
9、).print();,返回leaf对象的引用l1,指向leaf对象,返回leaf对象的引用l2,指向leaf对象,内存分析(一),class PointXY/分析程序在内存中执行过程private double x;private double y;PointXY(double x1,double y1)x=x1;y=y1;public double getX()return x;public double getY()return y;public void setX(double i)x=i;public void setY(double i)y=i;,内存分析(二),class Circ
10、leprivate PointXY o;private double radius;Circle(PointXY p,double r)o=p;radius=r;Circle(double r)o=new PointXY(0.0,0.0);radius=r;boolean contains(PointXY p)double x=p.getX()-o.getX();double y=p.getY()-o.getY();if(x*x+y*yradius*radius)return false;else return true;public void setO(double x,double y)o
11、.setX(x);o.setY(y);public PointXY getO()return o;public double getRadius()return radius;public void setRadius(double r)radius=r;public double area()return 3.14*radius*radius;,内存分析(三),public class TestCircle public static void main(String args)Circle c1=new Circle(new PointXY(1.0,2.0),2.0);Circle c2=
12、new Circle(5.0);System.out.println(c1:(+c1.getO().getX()+,+c1.getO().getY()+),+c1.getRadius();System.out.println(c2:(+c2.getO().getX()+,+c2.getO().getY()+),+c1.getRadius();System.out.println(c1 area=+c1.area();System.out.println(c2 area=+c2.area();c1.setO(5,6);c2.setRadius(9.0);System.out.println(c1
13、=(+c1.getO().getX()+,+c1.getO().getY()+),+c1.getRadius();System.out.println(c2=(+c1.getO().getX()+,+c2.getO().getY()+),+c2.getRadius();System.out.println(c1 area=+c1.area();System.out.println(c2 area=+c2.area();PointXY p1=new PointXY(5.2,6.3);System.out.println(c1.contains(p1);System.out.println(c1.
14、contains(new PointXY(10.0,9.0);,Circle c1=new Circle(new PointXY(1.0,2.0),2.0);,c1:xxx,x:1.0,y:2.0,x1:1.0,y1:2.0,o,radius:2.0,p:xxx,r:2.0,紫色的调用结束后释放,红色的几位调用结束后内存布局,stack,heap,Circle c2=new Circle(5.0);,c2:xxx,o,radius:5.0,r:5.0,x1:0.0,y1:0.0,x:0.0,y:o.o,stack,heap,System.out.println(c1:(+c1.getO().g
15、etX()+,+c1.getO().getY()+),+c1.getRadius();,c1:xxx,o,radius:2.0,x:1.0,y:2.0,和o里的值一样,1.0,返回值,返回值,stack,heap,2.0,返回值,c1.setO(5,6);c2.setRadius(9.0);,o,radius:2.0,o,radius:5.09.0,x:1.05,y:2.06,x:0.0,y:0.0,c1:xxx,c2:xxx,x:5,y:6,i:5,i:6,r:9.0,heap,stack,System.out.println(c1.contains(p1);,o,radius:2.0,x:
16、5,y:6,x:5.2,y:6.3,c1:xxx,p1:xxx,p:xxx,x,y,stack,heap,true,static关键字的使用,public class Cat private static int sid=0;private String name;int id;Cat(String name)this.name=name;id=sid+;public void info System.out.println(My name is+name+NO.+id);public static void main(String args)Cat.sid=100;Cat.mimi=new
17、Cat(mimi);Cat.pipi=new Cat(pipi);mimi.info();pipi.info();,static关键字的使用,stack,heap,data seg,sid:100101102,id:100,name,id:101,name,mimi:xxx,pipi:xxx,mimi,pipi,name,name,类的继承,student,Person,name,age,school,stack,heap,子类,父类,Java权限修饰符,super和this的使用,cc:xxx,valeu,value,this,super,子类,父类,Testsupr.class Stu s
18、1=new Stu(C,s1);,pers,name,location,school,this,super,heap,beijing,C,s1,s1,name,school,n,l,school,name,location,data seg,stack,hashcodes table,hashcodes table,对象,对象,对象,对象,对象,每一个对象都有独一无二的hashcodes,通过它可以找到所在对象的位置,对象转型casting(TestAnimals),class Animalclass Dg extends AnimalAnimal a=new Animal();/a=new
19、Dg();/Dg d1=(Dg)a;/,对象转型casting(TestAnimals),Animal a,Animal,name,furcolor,程序中把a当成一只普通的Animal,不能访问Dg中的成员,Dg,不能访问,表明,实质,只能访问作为父类的那部分成员,Dg d1,动态绑定和多态(TestPolymoph),Cate c,name:catname,eyeColor:blue,name:l1,Anima pet,lady l1,实质上指向,code seg,Anima enjoy,Cate enjoy,Do enjoy,根据实际对象类型来调用,方法,星际的多态应用,父类GameOb
20、ject,draw(),继承,使用多态,异常的概念,public void someMethod()throws SomeException if(someCondition()throw new SomeException(错误原因);,声明该方法可能抛出的异常,构造并抛出异常对象,抛出异常,try someMethod();catch(SomeException e)/异常处理代码;,调用该方法时试图捕捉异常,定义处理异常的代码,异常的分类,Throwable,Error,Exception,RuntimeException,可抛出的,系统错误,处理不了,能处理的错误,经常出的错误,可以不
21、catch,必须得catch的异常,运行时异常,非运行时异常,method()throws SomeException.,包含,异常类的父类,异常的捕获和处理,try/可能抛出异常的语catch(SomeException e1).catch(SomeException e2).finally.,统一的出口,都要被执行,可进行资源的清除工作,readFile()throws IOException.method1()throws IOException readFile()method2()throws IOException method1()public static main(Strin
22、g s)try method2()catch(IOException e).,抛异常,抛异常,抛异常,捕获,处理,main方法可不处理,向外抛,int s;数组定义s=new int5;,xxx,s,stack,heap,自动初始化都为0,Date days;数组分配空间和赋值days=new Date3;for(int i=0;i3;i+)daysi=new Date(2004,4,i+1);,xxx,xxx,xxx,xxx,2004,4,1,2004,4,2,2004,4,3,days,stack,heap,数组动态初始化,int a;/定义a=new int3;/分配空间a0=1;a1=
23、2;a2=3;/赋值Date days;/定义days=new Date2;/分配空间days0=new Date(2004,4,1);/赋值days1=new Date(2004,4,2);/赋值,数组定义与为数组元素分配空间和赋值分开进行,数组元素静态初始化,int a3=1,2,3;Date days=new Date(2004,4,1),new Date(2004,4,2),new Date(2004,4,3);,在定义数组的同时就分配空间和赋值,数组元素的默认初始化,数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐式初始化,int a
24、=new int3;System.out.println(a1);Date days=new Date3;System.out.prinyln(days2);,数组作为返回值,.,Date d,xxxx,返回值,数组模拟链表,1,2,3,4,0,5,0,1,5,4,3,2,3,4,5,0,2,0,2,3,4,5,删除该元素,二维数组,xxxx,a0,a2,a00,a01,a10,a11,a20,a21,.,.,.,heap,stack,a1,xxxx,xxxx,xxxx,a,一维数组,数组的数组,Java中多维数组的声明和初始化应按从高维到低维的顺序进行,int a=new int3;/动态初
25、始化a0=new int2;a1=new int4;a2=new int3;int a=new int35;/动态初始化int a=4,2,13,32,424,32,1,32,12;/静态初始化二维数组的访问:aij,二维数组的输出,int a=1,2,3,4,5,6,7,8,9;for(int i=0;ia.length;i+)for(int j=0;jai.length;j+)System.out.print(a+i+j+=+aij+,);System.out.println();该循环可用于二维数组的整体赋值和输出,二维字符串数组,数组元素,一维,字符串,二维,String类不可变,St
26、ring s1=hello;String s2=world;s1+=s2;,hello,world,helloworld,s1,s2,新建的对象,不可变,不可变,容器API,Collection,Set,List,Map,HashSet,LinkedList,ArrayList,HashMap,无顺序,不可重复,有顺序,可重复(equals),一个一个向里装,一对一对向里装,Iterator容器,next,元素,游标,Array与Linked比较,去掉,Array,Linked,改时要建一个新的数组,读时沿着链表向下查找,InputStream流,PipedInputStream,Filter
27、InputStream,ByteArrayInputStream,SequenceInputStream,StringBufferInputStream,ObjectInputStream,LineNumberInputStream,DataInputStream,BufferedInputStream,PushbackInputStream,FileInputStream,InputStream,节点流,处理流,OutputStream类,FileOutputStream,PipedOutputStream,FilterOutputStream,ByteArrayOutputStream,O
28、bjectOutputStream,DataOutputStream,BufferedOutputStream,PrintStream,OutputStream,处理流,节点流,节点流类型,处理流,数据源,处理流,节点流,线程状态转换,创建,终止,阻塞状态,就绪状态,运行状态,start(),阻塞解除,导致阻塞的事件,调度,线程同步中的死锁,线程2,线程1,锁定,锁定,应用层,表示层,会话层,传输层,网络层,数据链路层,物理层,应用层,传输层,网络层,物理+数据链路层,OSI参考模型,TCP/IP参考模型,最底层,Socket通信模型,Server,Client,socket.close(),socket.close(),OutputStream,InputStream,OutputStream,InputStream,ServerSocket s(port#)s.accept()/等待连接Socket对象,Socket(host,port#(Attempt toconnect),AWT,Component,MenuComponent,Container,Window,Panel,Frame,Dialog,Applet,Button,TextArea,Label,TextField,List,.,事件监听,事件源对象,