Docsity

Memory Management - Modern Programming Languages - Lecture Slides, Slides of Advanced Computer Programming

Partial preview of the text

Download Memory Management - Modern Programming Languages - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!

Memory Management

Chapter Fourteen Modern Programming Languages, 2nd ed. (^) Docsity.com 1

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. (^) Docsity.com 2

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. (^) Docsity.com 4

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

Chapter Fourteen Modern Programming Languages, 2nd ed. 5

int[] a = null;

Docsity.com

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. 7

int i = 0;

while (i<a.length) {

a[i] = 5;

i++;

Docsity.com

Memory Managers In Java

Chapter Fourteen Modern Programming Languages, 2nd ed. 8

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.

Docsity.com

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

Chapter Fourteen Modern Programming Languages, 2nd ed. (^) Docsity.com 10

A Stack Illustration

Chapter Fourteen Modern Programming Languages, 2nd ed. 11

7:

top: 8

6:

5:

4:

3:

2:

1:

0:

An empty stack of 8

words. The stack will

grow down, from high

addresses to lower

addresses. A reserved

memory location (perhaps

a register) records the

address of the lowest

allocated word.

Docsity.com

Chapter Fourteen Modern Programming Languages, 2nd ed. 13

The program calls

m.push(2), which

returns 2: the address of the

first of the 2 words

allocated for an activation

record. The stack is now

full—there is not room even

for m.push(1).

For m.pop(), just do

top = memory[top]

to return to previous

configuration.

7:

top: 1

6:

5:

4:

3:

2:

1:

0:

first activation record

8

second activation record

4

Docsity.com

A Java Stack Implementation

Chapter Fourteen Modern Programming Languages, 2nd ed. 14

public class StackManager { private int[] memory; // the memory we manage private int top; // index of top stack block

/**

  • StackManager constructor.
  • @param initialMemory int[] of memory to manage */ public StackManager(int[] initialMemory) { memory = initialMemory; top = memory.length; }

Docsity.com

Chapter Fourteen Modern Programming Languages, 2nd ed. 16

  • Pop the top stack frame. This works only if the
  • stack is not empty. */ public void pop() { top = memory[top]; } }

Docsity.com

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. (^) Docsity.com 17

First Fit

 A linked list of free blocks, initially

containing one big free block

 To allocate:

– Search free list for first adequate block

– If there is extra space in the block, return the

unused portion at the upper end to the free list

– Allocate requested portion (at the lower end)

 To free, just add to the front of the free list

Chapter Fourteen Modern Programming Languages, 2nd ed. (^) Docsity.com 19

Heap Illustration

Chapter Fourteen Modern Programming Languages, 2nd ed. 20

7:

freeStart: 0

6:

5:

4:

3:

2:

1:

0:

10

9:

8:

A heap manager m with a memory

array of 10 words, initially empty.

The link to the head of the free list is

held in freeStart.

Every block, allocated or free, has its

length in its first word.

Free blocks have free-list link in their

second word, or –1 at the end of the

free list.

Docsity.com