14 - Click to edit Master subtitle style Chapter Fourteen...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Click to edit Master subtitle style Chapter Fourteen Modern Programming Languages, 2nd ed. Memory Management Chapter Fourteen Modern Programming Languages, 2nd ed. 11 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 Chapter Fourteen Modern Programming Languages, 2nd ed. 22 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 Chapter Fourteen Modern Programming Languages, 2nd ed. 33 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 Chapter Fourteen Modern Programming Languages, 2nd ed. 44 Chapter Fourteen Modern Programming Languages, 2nd ed. Declaring An Array A Java array declaration: Array types are reference typesan 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 Chapter Fourteen Modern Programming Languages, 2nd ed. 55 int a = null; 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: Chapter Fourteen Modern Programming Languages, 2nd ed. 66 int a = null; a = new int[100]; int a = new int[100]; 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 ) Chapter Fourteen Modern Programming Languages, 2nd ed. 77 int i = 0; while (i<a.length) { a[i] = 5; i++; } Chapter Fourteen Modern Programming Languages, 2nd ed. Memory Managers In Java Chapter Fourteen Modern Programming Languages, 2nd ed. 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. Chapter Fourteen Modern Programming Languages, 2nd ed....
View Full Document

This note was uploaded on 09/30/2011 for the course CS 6371 taught by Professor Hamlen during the Spring '11 term at University of Texas at Dallas, Richardson.

Page1 / 63

14 - Click to edit Master subtitle style Chapter Fourteen...

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

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