dynamic memory allocation
play

Dynamic Memory Allocation CS 351: Systems Programming Michael - PowerPoint PPT Presentation

Dynamic Memory Allocation CS 351: Systems Programming Michael Saelee <lee@iit.edu> Computer Science Science registers from: cache (SRAM) main memory (DRAM) local hard disk drive (HDD/SSD) remote storage (networked drive / cloud)


  1. Dynamic Memory Allocation CS 351: Systems Programming Michael Saelee <lee@iit.edu>

  2. Computer Science Science registers from: cache (SRAM) main memory (DRAM) local hard disk drive (HDD/SSD) remote storage (networked drive / cloud) The Memory Hierarchy

  3. Computer Science Science we now have: Virtual Memory

  4. Computer Science Science now what?

  5. Computer Science Science - code, global variables, jump tables, etc. - allocated at fork/exec - lifetime: permanent Static Data

  6. Computer Science Science The Stack - function activation records - local vars, arguments, return values - lifetime: LIFO pages allocated as needed (up to preset stack limit)

  7. Computer Science Science explicitly requested from the kernel - for dynamic allocation - lifetime: arbitrary ! The Heap

  8. Computer Science Science - starts out empty - brk pointer marks top of the heap brk The Heap

  9. Computer Science Science heap mgmt syscall: void *sbrk(int inc); /* increments brk by inc, returns old brk value */ brk The Heap

  10. Computer Science Science brk void *hp = sbrk(N); N hp The Heap

  11. Computer Science Science after the kernel allocates heap space for a process, it is up to the process to manage it!

  12. Computer Science Science “manage” = tracking memory in use, tracking memory not in use, reusing unused memory

  13. Computer Science Science job of the dynamic memory allocator — typically included as a user-level library and/or language runtime feature

  14. Computer Science Science User Process application dynamic program memory allocator malloc sbrk Heap OS kernel RAM Disk

  15. Computer Science Science User Process application dynamic program memory allocator free(p) Heap OS kernel RAM Disk

  16. Computer Science Science User Process application dynamic program memory allocator free(p) Heap OS kernel RAM Disk (heap space may not be returned to the kernel!)

  17. Computer Science Science the DMA constructs a user-level abstraction (re-usable “blocks” of memory) on top of a kernel-level one (virtual memory)

  18. Computer Science Science the user-level implementation must make good use of the underlying infrastructure (the memory hierarchy)

  19. Computer Science Science e.g., the DMA should: - maintain data alignment - maximize throughput of requests - help maximize memory utilization - leverage locality how to quantify this?

  20. Computer Science Science utilization = fraction of memory in use - “in use” is a relative concept - for DMA, “in use” = amount of memory actually requested by user (aka payload ) - vs. heap space obtained via sbrk

  21. Computer Science Science Heap (given: DMA requests p1 = malloc(1024); // util = 1K/4K = 25% memory in 4KB chunks) 4KB

  22. Computer Science Science Heap (given: DMA requests p1 = malloc(1024); // util = 1K/4K = 25% memory in 4KB chunks) p2 = malloc(2048); // util = 3K/4K = 75% 4KB

  23. Computer Science Science Heap (given: DMA requests p1 = malloc(1024); // util = 1K/4K = 25% memory in 4KB chunks) p2 = malloc(2048); // util = 3K/4K = 75% free(p1); // util = 2K/4K = 50% 4KB

  24. Computer Science Science Heap (given: DMA requests p1 = malloc(1024); // util = 1K/4K = 25% memory in 4KB chunks) p2 = malloc(2048); // util = 3K/4K = 75% free(p1); // util = 2K/4K = 50% p3 = malloc(2048); // util = 4K/8K = 50% 8KB 4KB

  25. Computer Science Science Heap (given: DMA requests p1 = malloc(1024); // util = 1K/4K = 25% memory in 4KB chunks) p2 = malloc(2048); // util = 3K/4K = 75% free(p1); // util = 2K/4K = 50% p3 = malloc(2048); // util = 4K/8K = 50% free(p3); // util = 2K/8K = 25% free(p2); 8KB // util = 0/8K = 0% // all non-leaking // programs end in 0%

  26. Computer Science Science makes no sense to measure utilization at the end of process execution, and it makes no sense to arbitrarily measure utilization during execution

  27. Computer Science Science instead, measure peak memory utilization - ratio between maximum aggregate payload and maximum heap size - “high water mark” measure - assuming the heap never shrinks, end heap size = max heap size

  28. Computer Science Science p1 = malloc(1024); // util = 1K/4K = 25% p2 = malloc(2048); // util = 3K/4K = 75% free(p1); // util = 2K/4K = 50% p3 = malloc(2048); - max agg. payload = 4K // util = 4K/8K = 50% free(p3); - max heap size = 8K // util = 2K/8K = 25% free(p2); - peak memory util = 50% // util = 0/8K = 0% // all non-leaking // programs end in 0%

  29. Computer aggregate payload Science Science p1 = malloc(100); // 100 p2 = malloc(200); // 300 free(p1); // 200 p3 = malloc(300); // 500 free(p2); // 300 p4 = malloc(100); // 400 peak memory util p5 = malloc(200); // 600 free(p3); // 300 p6 = malloc(100); // 400 = 700 / 1024 p7 = malloc(300); // 700 free(p4); // 600 ≈ 68% free(p5); // 400 p8 = malloc(200); // 600 // measured heap size // at end is 1K

  30. Computer Science Science utilization is affected by memory fragmentation two forms: 1. internal fragmentation 2. external fragmentation

  31. Computer Science Science when allocating blocks of memory, it is convenient to make them self-describing i.e., store metadata alongside blocks with size, allocation status, etc.

  32. Computer Science Science allocator must also adhere to alignment requirements (to help optimize cache/ memory fetches)

  33. Computer Science Science metadata internal “block” payload fragmentation padding (for alignment)

  34. Computer Science Science amount of internal fragmentation is easy to predict, as it’s based on pre-determined factors - metadata = fixed amount - k -byte alignment → max k –1 padding

  35. Computer Science Science Heap

  36. Computer Science Science Heap

  37. Computer Science Science Heap external fragmentation

  38. Computer Science Science Heap external fragmentation may affect future heap utilization; i.e., by preventing free space from being re-used

  39. Computer Science Science Heap malloc?

  40. Computer Science Science Heap

  41. Computer Science Science Heap forced to request more heap space malloc?

  42. Computer Science Science hard to predict the effect of external fragmentation on utilization in general, we might: - prefer fewer, larger spans of free space - try to keep similarly sized blocks together in memory

  43. Computer Science Science but these recommendations are heuristics ! - may be defeated by pathological cases - don’t account for real-world behavior

  44. Computer Science Science It has been proven that for any possible allocation algorithm, there will always be the possibility that some application program will allocate and deallocate blocks in some fashion that defeats the allocator’s strategy and forces it into severe fragmentation ... Not only are there no provably good allocation algorithms, there are proofs that any allocator will be bad for some possible applications . P . Wilson, M. Johnstone, M. Neely, D. Boles, Dynamic Memory Allocation: A Survey and Critical Review

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend