Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Programming Languages and Paradigms Activation Records in Java Activation Records in Java Activation record: A chunk of computer memory which contains the data needed for the activation of a routine Java Virtual Machine’s Role Loads class files needed and executes bytecodes they contain in a running program Organizes memory into structured areas Java Virtual Machine Java Virtual Machine Runtime data areas in JVM Method area – contains class information Heap – memory for class instances and arrays PC registers – program counters for each thread Java stack – stores stack frames Native method stacks – Native methods: written in other languages Runtime Data Areas Method Area Contains class information One for each JVM instance Shared by all threads in JVM One thread access at a time Heap Contains class instance or array (objects) One for each JVM instance Facilitates garbage collection Expands and contracts as program progresses Objects Representation in Heap Runtime Data Areas: Java Stack Java Stack Each thread creates separate Java stack Contains frames: current thread’s state Pushing and popping of frames Stack Frame Local Variables Operand Stack Organized in an array Accessed via array indices … Incoming parameter 2 Incoming parameter 1 Organized in an array Accessed via pushing and popping Always on top of the stack frame Work space for operations Frame Data Constant Pool Resolution: Dynamic Binding Normal Method Return No exception thrown Returns a value to the previous frame Exception Dispatch Local variables Current frame Frame data Operand stack Outgoing parameters Next frame Example: Separate Stack Frames class StackFrameExample { public static void addAndPrint() { double result = addTwoTypes(1, System.out.println(result); } 88.88); public static double addTwoTypes(int i, double d) { return i + d; } } Example: Contiguous Stack Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++; } public void deposit( double amount ) { balance += amount; } } public class Driver { public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } } // In command prompt java Driver // In Java Virtual Machine Driver.main( args ) * a main() A stack frame for main() is pushed into the Java stack Driver class Methods main() Constant Pool “BankAccount” a “BankAccount” b 100 Method Area b Parameters Local variables Frame data 100 ClassLoader loads Driver to the method area … args[0] Heap Operand stack // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } … args[0] * a main() 100 * ClassLoader loads BankAccount() BankAccount to the method area b Parameters Local variables A pointer to the Frame data BankAccount pointer in the heap is Operand stack created (Constant Pool Resolution) Parameters Local variables Frame data Operand stack Parameters Driver class BankAccount class Methods main() Constant Pool “BankAccount” a “BankAccount” b 100 A stack frame for the BankAccount constructor is pushed into the Java stack Methods BankAccount() deposit( double ) Constant Pool 0 Method Area pointer A pointer to the BankAccount class data is created Heap // BankAccount.java private double balance; private static int totalAccounts = 0; … args[0] * a main() b Parameters Local variables Frame data public BankAccount() { balance = 0; totalAccounts++; } 100 Operand stack Parameters * public void deposit( double amount ) { BankAccount() balance += amount; } Local variables Frame data Operand stack Parameters Driver class BankAccount class Methods main() Constant Pool “BankAccount” a “BankAccount” b 100 Methods BankAccount() deposit( double ) Constant Pool 0 Static Variables totalAccounts 10 Method Area The totalAccounts totalAccounts is incremented static variable byof1 BankAccount is initialized with a default value and then assigned with 0 pointer balance 0.0 Heap The balance variable of this instance is initialized assigned with with0a default value // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } * a Methods main() Constant Pool “BankAccount” a “BankAccount” b 100 100 Static Variables totalAccounts * Operand stack Local variables Frame data Operand stack The stack frame for the BankAccount constructor is popped from the Java stack Parameters pointer balance 0.0 1 Method Area * Parameters Methods BankAccount() deposit( double ) Constant Pool 0 Parameters The pointer is returned to the callingLocal framevariables Frame data BankAccount() BankAccount class b main() The pointer is popped from the operand stack and assigned to a Driver class … args[0] Heap // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } Since the BankAccount class was already loaded in the method area, no other loading happens … args[0] * a main() 100 * BankAccount() b Parameters Local variables A pointer to the Frame data BankAccount pointer in the heap is Operand stack created (Constant Pool Resolution) Parameters Local variables Frame data Operand stack Parameters Driver class BankAccount class Methods main() Constant Pool “BankAccount” a “BankAccount” b 100 A stack frame for the BankAccount Constructor is pointer pushed into the Java stack Methods BankAccount() deposit( double ) Constant Pool 0 Static Variables totalAccounts pointer balance 0.0 1 Method Area Heap A pointer to the BankAccount class data is created // BankAccount.java private double balance; private static int totalAccounts = 0; … args[0] * a Parameters b Local variables main() Frame data public BankAccount() { balance = 0; totalAccounts++; } 100 Nothing happens since the totalAccounts was public void deposit( double amountalready ) { initialized BankAccount() when the balance += amount; BankAccount class } was first loaded Operand stack Parameters * Local variables Frame data Operand stack Parameters Driver class BankAccount class Methods main() Constant Pool “BankAccount” a “BankAccount” b 100 Methods BankAccount() deposit( double ) Constant Pool 0 Static Variables totalAccounts The balance variable of this instance is initialized assigned with with0a default value totalAccounts is incremented by 1 pointer balance 0.0 21 Method Area Heap pointer balance 0.0 // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } … args[0] * a Parameters b Local variables main() Frame data 100 The pointer is popped from the operand stack and assigned to b * Operand stack Parameters * BankAccount() The pointer is Local variables returned to the calling frame Frame data Operand stack Driver class BankAccount class Methods main() Constant Pool “BankAccount” a “BankAccount” b 100 Methods BankAccount() deposit( double ) Constant Pool 0 Static Variables totalAccounts The stack frame for the BankAccount Constructor is popped from the Java stack pointer balance pointer balance 0.0 2 Method Area Parameters Heap 0.0 // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } * a Methods main() Constant Pool “BankAccount” a “BankAccount” b 100 Static Variables totalAccounts Operand stack amount=100 Parameters The object reference to the instance is always put as the first local variable of a stack frame Local variables 100 is popped from the operand Framestack data and put into the next frame’s Operand parameters stack Parameters pointer balance 0.0 2 Method Area Local variables 100 Methods BankAccount() deposit( double ) Constant Pool 0 Parameters Frame data deposit( double ) BankAccount class b main() b Driver class … args[0] Heap A stack frame for the deposit method of instance ‘b’ is pointer pushed into the Java balance stack 0.0 // BankAccount.java private double balance; private static int totalAccounts = 0; … args[0] * a Parameters b Local variables main() Frame data public BankAccount() { balance = 0; totalAccounts++; } Operand stack amount=100 Parameters b Local variables public void deposit( double amount ) { BankAccount() balance += amount; } Frame data Operand stack Parameters Driver class BankAccount class Methods main() Constant Pool “BankAccount” a “BankAccount” b 100 Methods BankAccount() deposit( double ) Constant Pool 0 Static Variables totalAccounts pointer balance pointer balance 0.0 2 Method Area 100.0 0.0 Heap The frame knows which balance to modify because of the object reference