Docsity

Memory Management: Understanding Memory Places and Allocation Strategies in Java, Study notes of Computer Science

Toggle sidebar
bg1
CS414-2003S-11 Memory Management 1
11-0: Memory
•Three places in memory that a program can store variables
•Call stack
•Heap
•Code segment
11-1: Memory
Code Segment
Stack
Heap
Executable
Code
Static
Storage
11-2: Memory
•Three places in memory that a program can store variables
•Call stack
•Local Variables
•Heap
•Dynamically allocated variables
•(Most of the variables in Java)
•Code segment
•Static variables
11-3: Static Storage
•If a variable is declared static, there is only one instance of the variable
•Variable is typically stored in the code segment, not the stack or the heap
•Why?
11-4: Static Storage
•If a variable is declared static, there is only one instance of the variable
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Memory Management: Understanding Memory Places and Allocation Strategies in Java and more Study notes Computer Science in PDF only on Docsity!

11-0: Memory

  • Three places in memory that a program can store variables
    • Call stack
    • Heap
    • Code segment

11-1: Memory

Code Segment

Stack

Heap

Executable Code

Static Storage

11-2: Memory

  • Three places in memory that a program can store variables
    • Call stack
      • Local Variables
    • Heap
      • Dynamically allocated variables
      • (Most of the variables in Java)
    • Code segment
      • Static variables

11-3: Static Storage

  • If a variable is declared static, there is only one instance of the variable
  • Variable is typically stored in the code segment, not the stack or the heap
    • Why?

11-4: Static Storage

  • If a variable is declared static, there is only one instance of the variable
  • Variable is typically stored in the code segment, not the stack or the heap
    • Stack storage is too transient
    • Using the code segment guarantees a single instance of the variable

11-5: Static Storage

class StaticVars { int x; static int y; } void main() { StaticVars SV1 = new StaticVars(); StaticVars SV2 = new StaticVars(); SV1.x = 1; SV1.y = 2; SV2.x = 3; SV2.y = 4; print(SV1.x); print(SV1.y); print(SV2.x); print(SV2.y); }

11-6: Static Storage

class StaticVars { int x; static int y; } void main() { StaticVars SV1 = new StaticVars(); StaticVars SV2 = new StaticVars(); SV1.x = 1; SV1.y = 2; SV2.x = 3; SV2.y = 4; print(SV1.x); print(SV1.y); print(SV2.x); print(SV2.y); }

Output: 1 4 3 4 11-7: simpleJava Static Storage

  • What do we need to do to implement static storage in simpleJava?

11-8: simpleJava Static Storage

  • What do we need to do to implement static storage in simpleJava?
  • Looking at each portion of the compiler in turn:
    • Lexical Analysis – what needs to be done?

11-9: simpleJava Static Storage

  • Lexical Analysis
    • Add a new keyword “static” to the language
    • – Add “static” token

11-10: simpleJava Static Storage

  • Parsing & Building AST

11-11: simpleJava Static Storage

AAT for class2.class1.x? 11-19: simpleJava Static Storage

Memory

Operator(-)

Memory Constant(x_offset)

Operator(-)

Memory Constant(class1_offset)

Operator(-)

Register(FP) Constant(class2_offset)

11-20: simpleJava Static Storage

class C1 { static int y; int x; } class C2 { int a; C1 class1; } ... C2 class2;

AAT for class2.class1.y? 11-21: simpleJava Static Storage

Memory

AddressExp

Label("y001")

11-22: simpleJava Static Storage

  • Code Generation

11-23: simpleJava Static Storage

  • Code Generation
    • Add space to code segment to store static variables
    • Make sure the labels match!!

11-24: Heap-based storage

  • There are 2 main memory-allocation dangers associated with heap-based storage
    • Dangling References
    • Memory leaks

11-25: Dangling References

int main() { int *a; int *b; a = (int ) malloc(sizeof(int)); (a) = 4; b = a; free b; ... }

  • What happens if we change (a) [(a) = ...]?

11-26: Memory Leaks

int main() { int *intPtr; intPtr = (int *) malloc(sizeof(int)); intPtr = NULL; ... }

  • Allocated memory that we can’t get to – garbage
  • Eventually, use up heap memory

11-27: Managing the Heap

  • Manage the heap to avoid memory leaks and dangling references
    • Give all decisions to the programmer
    • Automatic memory management

11-28: Programmer Controlled

  • Advantages
    • Memory management system is less complicated
    • Lower run-time overhead for the memory manager
    • Can manage the memory needs for a specific program more efficiently than a general-purpose memory manager (at least in theory)

11-29: Free List

  • List of all available blocks of memory

11-33: Free List Example

Freelist

Allocated Memory

Size of block Next block

984

11-34: Free List Example

class oneElem { int x; } class twoElem { int x; int y; }

oneElem A = new oneElem(); oneElem B = new oneElem(); twoElem C = new twoElem(); twoElem D = new twoElem(); delete A; delete C;

11-35: Free List Example

Freelist Size of block Next block Allocated Memory

8

Size of block Next block

12

Size of block Next block

984

Allocated Memory

11-36: Free List Example

class oneElem { int x; } class twoElem { int x; int y; }

oneElem A = new oneElem(); oneElem B = new oneElem(); twoElem C = new twoElem(); twoElem D = new twoElem(); delete A; delete C; delete D;

11-37: Free List Example

delete B[i]; delete A; /* Point A / for (i=0; i<3000; i = i + 2) B[i] = new smallClass(); / Point B */ }

11-41: First Fit

  • At Point A:

11-42: First Fit

  • At Point B:

11-43: First Fit

  • Plenty of space on the heap
  • Divided into small blocks – can’t service a request for a large block of memory
  • Memory fragmentation

11-44: Best Fit

  • When there are several blocks to choose from on the free list, which do we use to fulfill a memory request?
    • First Fit
      • Return the first block that is large enough
    • Best Fit
      • Return the smallest block that is large enough

11-45: Best Fit

  • At Point B (using Best Fit):

11-46: Best Fit

  • Best Fit will usually lead to less memory fragmentation than first fit
    • Don’t “waste” large memory blocks on small requests
    • Large blocks should then be available when needed
  • Will Best Fit always lead to less memory fragmentation?

11-47: Best Fit vs First Fit

for (i=0; i<100;i++) A[i] = malloc(4); for (i=0;i<100;i++) B[i] = malloc(3);

for (i=0;i<100;i+=2) free(A[i]); for (i=0;i<100;i+=2) free(B[i]);

for (i=0; i<100; i++) C[i] = malloc(2);

A request for a block of size 16 is made 11-53: Segregated Free List Free List Array

2 word blocks

4 word blocks

8 word blocks

16 word blocks

32 word blocks

11-54: Segregated Free List Free List Array

2 word blocks

4 word blocks

8 word blocks

16 word blocks

32 word blocks

A request for a block of size 2 is made 11-55: Segregated Free List Free List Array

2 word blocks

4 word blocks

8 word blocks

16 word blocks

32 word blocks

11-56: Garbage Collection

  • Giving the user control of deallocation has problems:
    • Writing programs that properly deallocate memory is hard
    • Often, there are many pointers to the same block of memory (much like your current project!)
    • It can be difficult to determine when a block of memory should be freed
    • We don’t want to be too aggressive in freeing memory (why not?)

11-57: Garbage Collection

  • Giving the user control of deallocation has problems:
    • Writing programs that properly deallocate memory is hard
    • Often, there are many pointers to the same block of memory (much like your current project!)
    • It can be difficult to determine when a block of memory should be freed
  • We don’t want to be too aggressive in freeing memory (why not?)
  • Solution – don’t let programmer control deallocation!

11-58: Garbage Collection

  • Don’t allow programmer to deallocate any memory
  • Garbage will collect
  • Periodically collect the accumulated garbage, and return it to the free list

11-59: Mark & Sweep

  • When Garbage Collection routine is invoked:
    • Mark all heap memory that is reachable by the program
      • Need to add a “mark” bit to each block of memory – can use the header
    • Sweep through the entire block of memory, moving unmarked blocks to the free list

11-60: Mark Phase for each pointer P on the stack mark(P)

mark(P) { if ((P is not null) and (mark bit of Mem[P] is not set)) set mark bit of Mem[P] for each pointer Q in the block Mem[p] mark(Q) }

11-61: Mark & Sweep class Class1 { int x; int y; } class Class2 { Class1 C int x; } class Class3 { Class1 C1; Class2 C2; } Class3 C3 = new Class3(); C3.C1 = new Class1(); C3.C2 = new Class2(); C3.C2.C1 = new Class1();

11-62: Mark & Sweep

C Saved Registers

Stack Heap

Header

Header

Header

Header

Freelist

SP

C C

x y

C x

x y

C Saved Registers

Stack Heap

Header

Header

Header

Freelist

SP

C C

C x

x y Header

y

x

Header

11-67: Whither Pointers?

  • In order for the Mark phase to work correctly, we need to know which memory locations are pointers, and which are not - Tag pointers - Assume that any value that might be a pointer is a pointer ( Conservative garbage collection) - Create tables that store memory locations of all pointers

11-68: Tagging Pointers

  • If we wish to tag pointers themselves, we have two options:
    • Tag the pointer itself (high order bits)
    • Store tag in preceding word

11-69: Tagging Pointers

  • Tag the pointer itself (high order bits)
    • If the high order bits are 11, then the memory location represents a pointer
    • If the high oder bits are 00, 01, or 10, then the memory location represents an integer or boolean value
  • Using 32-bit words, only 30 bits will be available for pointer values
    • Need to strip the tag before pointers can be dereferenced
  • Using 32-bit words, slightly more than 31 bits are available for integer values (very large negative values pro- hibited)

11-70: Tagging Pointers

  • Store tag in preceding word
    • Set aside a specific bit pattern as a sentinel value (something like -MAXINT)
    • Every pointer requires 2 words of storage – word for the sentinel, and a word for the pointer itself

11-71: Tagging Pointers

class Class1 { int x; Class2 C2; Class3 C3; boolean y; }

Header x

C

C y

Sentinel

Sentinel

11-72: Conservative GC

  • Assume that every memory location that could be a pointer is a pointer
  • The integer y will be considered a pointer if:
    • Heap addresses are in the range LOW .. HIGH, and LOW ¡= y ¡= HIGH
    • The memory location y is the beginning of an allocated block

11-73: Conservative GC

  • Every memory block on the heap that is pointed to by something on the stack will be marked
    • No dangling references
  • Some memory blocks on the heap that are not pointed to by something on the stack may be marked
    • May have some uncollected garbage
  • Since no extra information (tagged pointers, etc.) is needed, Conservative Garbage Collectors can be run on languages not designed with garbage collection in mind (i.e., C)

11-74: Pointer Tables

  • Create a table for each function & class, which keeps track of where the pointers are in that function or class
    • This can be done at compile time
  • Each function & class will need a “kind” field, to store what kind of function or class it is (classes will need a “kind” field anyway, if we want instanceof to work)

11-75: Pointer Tables

class ClassA { int w; int x; } class ClassB { int y; ClassA C1; ClassA C2; int z; } void main() { int a;