Memory
What is Memory?
Conceptually memory can be thought of a sequence of binary storage, commonly grouped into bytes (8 bits), words (the size depends on the architecture), and pages (which depend on the operating system).
Stack vs. Heap
In practice, processes divide contiguous memory into regions called the stack and the heap. The primary function of the stack is to handle memory allocation throughout the execution of the program throughout each function call.
Whenever an operation like a mathematical operator or a function call are initiated, the necessary values are referenced by their positions at the top of the stack (stack[stack_length - n]
where n
is the distance from the end of the stack). Scalar return values (or stack-bound collections like Rust arrays) are given room on the stack and set as well.
However, dynamic types like vectors do not have a static size. In most languages, any item on the stack must have a known size, so this other data must live in the heap instead. However because data is potentially getting allocated, reallocated, and freed in the heap by a lot of different parts of your application, it may not always be possible to get large free blocks. Performance concerns can arise as things need to get shifted around or the program needs to search through the heap for free space.
Heap Management
One strategy for heap management is a linked free list. Each free segment of memory is stored in a node containing the pointer to the next node, the current node’s size, and then the data contained by the block. For new allocations, it becomes necessary to search through this list to find either a first fit or a best fit. As deallocations occur, the list becomes increasingly fragmented. It becomes necessary to defragment the list.
Instruction Sets
- IA-32 vs. IA-64?
- Should optimizations occur during compilation or in the processor?
- Predication vs. speculation?
Operating Systems
- File I/O
- Memory
- Virtual memory, paging, segmentation
- Virtual memory is disk space used for temporarily swapping out memory as needed.
- Segmentation splits memory into chunks based on process but because whole code blocks are stored on disk/memory, it’s susceptible to fragmentation.
- Pages (or page frames)
- Synchronization
malloc
andfree
are library calls that manage the virtual address space. they use system calls likebrk
andsbrk
under the hoodcalloc
==malloc
and zeroes it outrealloc
make more memory (for an array, etc)mmap
for swap?- seg faults, invalid free, memory leaks, double-frees, dangling pointers
- the OS reclaims memory after a program exits (i.e. the OS manage memory for the process, the system libraries allocate memory for the application)
- valgrind and purify
Virtualization
Address Translation
Hardware address translation memory management unit (MMU) base and bound registers
- base is added to virtual memory addresses and limited by the bound
- anything not within that range is an out-of-bounds fault only privileged kernel-mode can set the base/bounds values free list
Resources
- Kato Mivule, A Hybrid Strategy for Memory Management in Web Servers
- Segmented, Paged and Virtual Memory
- Advanced programming for the unix environment book