{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

14 - Memory Management Click to edit Master subtitle style...

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

View Full Document Right Arrow Icon
Click to edit Master subtitle style Chapter Fourteen Modern Programming Languages, 2nd ed. Memory Management 11
Background image of page 1

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

View Full Document Right Arrow Icon
Chapter Fourteen Modern Programming Languages, 2nd ed. Dynamic Memory Allocation Lots of things need memory at runtime: Activation records Objects Explicit allocations: new , malloc , etc. Implicit allocations: strings, file buffers, arrays with dynamically varying size, etc. Language systems provide an important hidden player: runtime memory management 22
Background image of page 2
Chapter Fourteen Modern Programming Languages, 2nd ed. Outline 14.2 Memory model using Java arrays 14.3 Stacks 14.4 Heaps 14.5 Current heap links 14.5 Garbage collection 33
Background image of page 3

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

View Full Document Right Arrow Icon
Chapter Fourteen Modern Programming Languages, 2nd ed. Memory Model For now, assume that the OS grants each running program one or more fixed-size regions of memory for dynamic allocation We will model these regions as Java arrays To see examples of memory management code And, for practice with Java 44
Background image of page 4
Chapter Fourteen Modern Programming Languages, 2nd ed. Declaring An Array A Java array declaration: Array types are reference types—an array is really an object, with a little special syntax The variable a above is initialized to null It can hold a reference to an array of int values, but does not yet 55 int[] a = null;
Background image of page 5

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

View Full Document Right Arrow Icon
Chapter Fourteen Modern Programming Languages, 2nd ed. Creating An Array Use new to create an array object: We could have done it with one declaration statement, like this: 66 int[] a = null; a = new int[100]; int[] a = new int[100];
Background image of page 6
Chapter Fourteen Modern Programming Languages, 2nd ed. Using An Array Use a[i] to refer to an element (as lvalue or rvalue): a is an array reference expression and i is an int expression Use a.length to access length Array indexes are 0..( a.length-1 ) 77 int i = 0; while (i<a.length) { a[i] = 5; i++; }
Background image of page 7

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

View Full Document Right Arrow Icon
Chapter Fourteen Modern Programming Languages, 2nd ed. Memory Managers In Java 88 public class MemoryManager { private int[] memory; /** * MemoryManager constructor. * @param initialMemory int[] of memory to manage */ public MemoryManager(int[] initialMemory) { memory = initialMemory; } } We will show Java implementations this way. The initialMemory array is the memory region provided by the operating system.
Background image of page 8
Chapter Fourteen Modern Programming Languages, 2nd ed. Outline 14.2 Memory model using Java arrays 14.3 Stacks 14.4 Heaps 14.5 Current heap links 14.5 Garbage collection 99
Background image of page 9

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

View Full Document Right Arrow Icon
Chapter Fourteen Modern Programming Languages, 2nd ed. Stacks Of Activation Records For almost all languages, activation records must be allocated dynamically For many, it suffices to allocate on call and deallocate on return This produces a stack of activation records: push on call, pop on return A simple memory management problem 1010
Background image of page 10
Chapter Fourteen Modern Programming Languages, 2nd ed.
Background image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}