09 - 04/24/09 20:07:04 CS 61B: Lecture 9 Monday, February...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
04/24/09 20:07:04 1 09 CS 61B: Lecture 9 Monday, February 9, 2009 Today’s reading: THE STACK AND THE HEAP ====================== Java stores stuff in two separate pools of memory: the stack and the heap. The _heap_ stores all objects, including all arrays, and all class variables (i.e. those declared "static"). The _stack_ stores all local variables, including all parameters. When a method is called, the Java Virtual Machine creates a _stack_frame_ (also known as an _activation_record_) that stores the parameters and local variables for that method. One method can call another, which can call another, and so on, so the JVM maintains an internal _stack_ of stack frames, with "main" at the bottom, and the most recent method call on top. Here’s a snapshot of the stack while Java is executing the SList.insertEnd method. The stack frames are on the left. Everything on the right half of the page is in the heap. Read the stack from bottom to top, because that’s the order in which the stack frames were created. STACK | HEAP | method call parameters & local variables | ----------------------------------------------| --- | ------------------- this |.+----------->|item |.| next |X| SListNode.SListNode --- --- | -------+----------- obj |.+--------------------------\ | --- | | | ----------------------------------------------| v v --- | ------------ obj |.+----------------------->| string | --- | ------------ SList.insertEnd --- | ^ this |.+--------------------------+---------\ --- | | | ----------------------------------------------| | | --- | | | str |.+--------------------------/ v --- --- | --------------------- list |.+----------------->|head |X| size | 0 | --- | --------------------- SList.main --- | --------- ----------- args |.+------------------------>| . | .-+-->| words | --- | --+------ ----------- | | ----------- ----------------------------------------------| \--->| input | ----------- The method that is currently executing (at any point in time) is the one whose stack frame is on top. All the other stack frames represent methods waiting for the methods above them to return before they can continue executing. When a method finishes executing, its stack frame is erased from the top of the stack, and its local variables are erased forever. The java.lang library has a method "Thread.dumpStack" that prints a list of the
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/21/2010 for the course CS 61B taught by Professor Canny during the Spring '01 term at University of California, Berkeley.

Page1 / 2

09 - 04/24/09 20:07:04 CS 61B: Lecture 9 Monday, February...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online