Toronto CSC 148 - Java Memory Management

Unformatted text preview:

Java Memory Management1Tracing program executionTrace: To follow the course or trail of.When you need to find and fix a bug or have to understand a tricky piece ofcode that your coworker wrote, you have to trace it.There are three ways to trace code:1. Put in a lot of print statements.2. Use a debugger.3. Trace by hand.Many undergraduate students use the first trick to debug and understandcode. Professional programmers almost nev er do.2A picture of computer memoryInformation about a running program is stored in computer memory. Everyinterface, class, object and running method has a separate region of memoryto kee p track of variable values and othe r related information.The program is exe cuted line by line, with jumps betwee n methods. Executioncauses the stored information to change.We represent each region using this picture, called a memory box:Name ScopeContentsStatic information (interface & class boxes) and instance information (objectboxes) are stored in the heap.Method information is stored in the run-time stack.3Stack: method space(contains method boxes)main : 1MainClassMethod frame:Contents: parameters and local variables Name of method andcurrently−executingline numberName of class (forstatic method) oraddress of object(for instance method)Heap: Static Space (contains static boxes)Static box:Name of classMathObjectdouble PI3.14159...Name ofsuperclass max(int, int)abs(double)Contents: static variablesand static methodsHeap: Object S pace (contains instance boxes)Instance:Memory address1010Contents: instance variables and methodsint id35567981Student Type of object(name of its class)getID()setID(int)4Classes and interfacesThe source code for a class indicates:• interfaces it implements• the superclass it extends (if not explicitly indicated, then superclass isObject)• its class members (static variables and methods), possibly with initialvalues• constructors (if not explicitly indicated, then there’s a default no-argconstructor)• a template for objects of the class: instance members (non-static vari-ables and methods)The source code for an interface indicates:• the interfaces it ex tends• constants it defines (variables, automatically public static final)• method headers (public instance) that implementing classes must provide5Static spaceFor each class and interface me ntioned in the program, draw a me m ory boxin the static space. In each of these memory boxes record:• the class/interface name [ upper left corner]• for a class, the superclass it extends [upper right corner]• the list of interfaces (if any) implemented / e xtended [upper right corner]• the names and values of static variables (if any) [in the main area of thebox]; this includes all interface constants• for a class, the signatures of the static methods (if any) [in the mainarea of the box]6Only one copy of each class and interface (and their static members) existsno matter how many objects are cre ated.Each variable starts with the default value for its type, unless it has an ini-tializer. Record the values.7Object spaceAn object is created by a new expression calling a constructor of some class.Draw a new memory box for it in the objec t space.The object gets a unique address (different from all other objects in use),which we make up and write in the upper left corner. Represent the addressas an arbitrary four digit hexadecimal number (e.g. ABCD, 0007).Divide the memory box for the object into a stack of boxes: the bottom-mostpart for the class of the object, and one part for each ancestor of that class.The object gets a copy of the instance members of each of these classes. Foreach class, w rite the members in the corresponding part of the memory boxand the name of the class in the upper right corner of the part.8Each variable starts with the default value for its type, unless it has an ini-tializer. Record the values.Execute the constructor as if it were an instance method called on the obje ctthrough a reference of the same type as the object.The constructor starts by executing the code in the superclass default con-structor, unless the first statement specifically specifies how to start:• this(...) selects another constructor of the class (based on the argu-ment types)• super(...) selects a superclass constructor ( base d on the argumenttypes)When the constructor is done, the value of the new expression is the addressof the new object.9Special cases with new• You can create a String object without saying “new”.Example:String s = "Wombat"; // Shorthand.String s = new String("Wombat"); // What it means.• What about drawing an instance of a class that you didn’t write, suchas String?– You probably don’t know what the instance v ariables are.– Yet you need to keep track of the contents of the object somehow.Just make up a sensible notation. The next slide contains a few examples.10Drawing Java API objectsTrace these examples:String s = new String("Wombat");Integer i = new Integer(27);Vector v = new Vector();v.addElement(s);v.addElement(i);v.addElement(v);Object memory boxes:0000 String"Wombat"length(). . .0001 Integer27intValue(). . .0010 Vector0000 0001 0010size()elementAt(int). . .11Introduction to finding namesConsider this code:public class A { ??? }public class B extends A {???public void m(???) {???i = 3; // Line 3, say.}}public class C extends B { ... }public class M {public static void main(String[] args) {A a = new C(); a.m();}}The ???’s indicate where i may have been declared. i may be a local vari-able or parameter in m, or a static or instance variable in A or B. (Or somecombination!)We need to find which variable i refers to, its tar get.12m:1 0000B1main:2 MA a 0000A Object5B Object3C ObjectDon’t look here!M Objectmain(Str i ng[])0000 A4B2m()CDon’t look here!13Finding variables and methodsAfter the static information is recorded, we’re always tracing code inside thecurrently running method (at the top of the method stack). This methodcomes from a class or part of an object (recorded in the method’s scope box).Code w ithin the method can directly refer to the local variables and parame-ters inside the method, and ce rtain members. If the method come s from• a clas s C: it can directly refer to the static members of the class and itsancestors, and the interfaces implemented by them.• the C part of an object: it can directly refer to what it could if itwere in class C, and also the instance members of the object’s C part


View Full Document

Toronto CSC 148 - Java Memory Management

Download Java Memory Management
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Memory Management and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Memory Management 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?