Memory Management in Operating Systems: Explicit Memory Allocation and Errors - Prof. Emer, Study notes of Operating Systems

An overview of memory management in operating systems, focusing on explicit memory allocation and associated errors. The concept of memory management as a run-time system, explicit memory management using functions like malloc and realloc, and common errors such as dangling pointers and buffer overflows.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-o7c-1
koofers-user-o7c-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMPSCI 377 Operating Systems Fall 2005
Lecture 14: November 10
Lecturer: Emery Berger Scribe: Eric Hodge, Eric Patrick
Today:
Memory Management
14.1 Memory Management
14.1.1 Introduction
- Not memory management in kernel, but memory management between app and
OS - the ”run time system.”
- Java runs in virtual machine (in run time system.)
- C/C++ runs in libraries (libc.so, libc++.so)
- Explicit memory management (c / c++)
- Garbage collection (Java, Python, Perl)
14.1.2 Explicit Memory Management
- One of the oldest fields in computer science
- Must say explicitly what you want to do with memory (ask for it.)
- Malloc (size) - returns a pointer to space big enough for size bytes.
- Calloc (size, times) - multiplies size * times, also fills memory with zeros.
- Realloc (old obj, size) - reallocates old object to a chunk of memory of size.
- realloc (null, sz) = malloc(sz)
- Realloc ( p, 0) = free(p)
- Min size returned by malloc = 8 bytes, sizeof (double) ==8 bytes
- Free(ptr) - dispose of object.
- Takes object at ptr and gives it back to runtime system.
- If you don’t free your objects, you get a memory leak.
- Things slow down due to paging.
realloc(NULL, size) == malloc(size)
realloc(p,0) == free(p)
14.1.3 Errors Involved in Memory Management
Dangling Pointer Error:
P = malloc()
x=p
14-1
pf3
pf4

Partial preview of the text

Download Memory Management in Operating Systems: Explicit Memory Allocation and Errors - Prof. Emer and more Study notes Operating Systems in PDF only on Docsity!

CMPSCI 377 Operating Systems Fall 2005

Lecture 14: November 10

Lecturer: Emery Berger Scribe: Eric Hodge, Eric Patrick

Today:

  • Memory Management

14.1 Memory Management

14.1.1 Introduction

  • Not memory management in kernel, but memory management between app and OS - the ”run time system.”
  • Java runs in virtual machine (in run time system.)
  • C/C++ runs in libraries (libc.so, libc++.so)
  • Explicit memory management (c / c++)
  • Garbage collection (Java, Python, Perl)

14.1.2 Explicit Memory Management

  • One of the oldest fields in computer science
  • Must say explicitly what you want to do with memory (ask for it.)
  • Malloc (size) - returns a pointer to space big enough for size bytes.
  • Calloc (size, times) - multiplies size * times, also fills memory with zeros.
  • Realloc (old obj, size) - reallocates old object to a chunk of memory of size.
  • realloc (null, sz) = malloc(sz)
  • Realloc ( p, 0) = free(p)
  • Min size returned by malloc = 8 bytes, sizeof (double) ==8 bytes
  • Free(ptr) - dispose of object.
  • Takes object at ptr and gives it back to runtime system.
  • If you don’t free your objects, you get a memory leak.
  • Things slow down due to paging. realloc(NULL, size) == malloc(size) realloc(p,0) == free(p)

14.1.3 Errors Involved in Memory Management

Dangling Pointer Error: P = malloc() x=p

14-2 Lecture 14: November 10

Free(p) Z = malloc() z... x...

z may have overwritten x -you had a pointer to some space -but now you’ve freed it -and now it can be overwritten -you can still try to reference it without no guarantees

Buffer overflow

  • allocating too small a space and overwriting the end of memory block.
  • Used by h4X0Rz.
  • Professor Berger is l33t.

Some other errors...

  • free objects that you didn’t allocate -free objects twice

14.1.4 Memory Allocation

What malloc() actually does:

  • Process is instantiated.
  • Loader (ld.so in linux) loads program to memory, and points program counter to right place and begins running. Memory Structure:
  • Stack grows down.
  • Heap grows up.
  • Code text segment beneath heap.
  • In between stack and heap is a protected page to prevent collision between stack and pointer, is fixed.
  • One way of managing heap size is to use a breakpoint (sbrk(int) to set pointer.)

mmap():

  • mmap() often maps a file to memory.
  • Most UNIXs have a file called /dev/zero.
    • Anonymous file.
  • When calling mmap(), allocates memory in swap file for mmap() call.
  • Munmap(ptr, sz) deallocates.

14-4 Lecture 14: November 10

  • This prevents race condition from accessing same place in memory
  • To avoid having threads slow down program, have multiple free lists.

14.1.9 Garbage Collection

  • No such thing as free() method.
  • Find all unused objects and deallocate them.
  • Garbage collection tests for reachability.
  • Roots Globals, stack, and registers.
  • Use roots to find pointers, then find more pointersetc.
  • Build reachability tree. If there is no pointer to an object, it is unreachable and is garbage. Use mark-sweep. Everything is initially garbage.
  • For every object in tree, set mark bit to 1 when it is reachable.
  • When done searching tree, sweep through heap, and deallocate all garbage.
  • Garbage collector is called complete if it is guaranteed to reclaim all memory.
  • Stop-the-world garbage collector stops program during garbage collection.

14.1.10 Semi-space collector

  • Known as copying garbage collector.
  • Divide heap in two.
  • Once 1st heap is filled, run garbage collection.
  • Look at roots and see what gets pointing to from roots.
  • If it IS pointed to, copy to 2nd heap.
  • Deallocate first heap.
  • Then 2nd heap becomes from space.
  • Generations - allocate to nursury.
  • If object survives, copy out. If not, reset nursery.