Learn Memory Mgmt: Understanding Static, Stack & Heap Allocation

In summary: This can result in a program running out of space even when the actual amount of physical ram is still available.
  • #1
Chromium
56
0
I'm learning about memory management in my programming languages class, and I thought it would be beneficial to post a small write-up detailing my current understanding so that more knowledgeable people could critique (and hopefully expand) it.

Static: Static allocation refers to objects & variables whose "lifetimes" are equal to that of the running programming (i.e. they're around from the beginning to the end of execution). Global variables are a great example of static objects. Global variables belong to the program as a whole (as opposed to a local variable or an instance field).

Stack: Stack allocation usually refers to a call stack. The first function called (e.g. main) is located at the bottom of the stack. Any subroutines called by main are then pushed on the stack, meanwhile main waits for said subroutines to returns. When a particular function is called, it is given a "frame" on the stack. A frame contains information regarding the function call it represents (e.g. local variables, argument names, etc.). It can also be said that a single stack represents a single thread. Therefore, concurrency would imply multiple stacks because concurrency involves multiple threads.

Heap: Heap allocation usually refers to the space reserved for dynamically-created things (e.g. objects/class instances). A variety of different algorithms are used to manage the space within the heap (e.g. first fit, best fit, etc.). Two problems usually creep up when managing the heap: internal & external fragmentation. Internal fragmentation occurs when a larger-than-required block of memory is assigned to an object (effectively wasting the unused space within the block because you cannot access the space). External fragmentation occurs when the remaining unused space is fragmented into blocks that are too small to really fit any objects that may be created in the future.
 
Technology news on Phys.org
  • #2
that's a pretty good summary; some students do a lot of programming and never achieve this much insight. good job.
 
  • #3
From what I've been told by another programmer, windows .net framework has a collection - compaction algorithm to merge small allocated objects into common memory pages when other objects are released from memory pages. This eliminates the problem of large number of small objects being allocated and released, but consuming partially used pages of memory. I'm not sure of the algorithm used to keep track of the small allocated objects.

Page size on an Intel CPU is 4096 bytes (or 4 MB, but that's rarely used), so it's mostly an issue for small objects.
 
  • #4
Also, most operating system utilize virtual memory, where the assigned memory space does not represent the physical addresses of ram. For example, a program can see pages of virtual memory increased linearly, but they actually lie in totally different areas of ram.
 

Related to Learn Memory Mgmt: Understanding Static, Stack & Heap Allocation

1. What is memory management?

Memory management is the process of allocating and deallocating memory for a computer program. It involves managing the usage of memory resources in order to optimize performance and prevent memory-related errors.

2. What is static allocation?

Static allocation is a type of memory management where memory is allocated at compile time and remains fixed throughout the program's execution. This means that the size and location of memory are determined before the program is run and cannot be changed during runtime.

3. What is stack allocation?

Stack allocation is a type of memory management where memory is allocated and deallocated in a last-in-first-out (LIFO) manner. This means that memory is allocated and released in the reverse order of its allocation. The stack is used for local variables and function calls.

4. What is heap allocation?

Heap allocation is a type of memory management where memory is allocated and deallocated in a first-in-first-out (FIFO) manner. This means that memory is allocated and released in the same order as its allocation. The heap is used for dynamic data structures such as arrays and objects.

5. What are the advantages and disadvantages of each type of allocation?

Static allocation has the advantage of being fast and efficient, as the memory is pre-allocated and does not need to be managed during runtime. However, it has the disadvantage of inflexibility, as the size and location of memory cannot be changed during program execution.

Stack allocation has the advantage of being simple and fast, as it follows a LIFO structure. However, it has a limited amount of memory available and can lead to stack overflow errors if not managed properly.

Heap allocation has the advantage of being flexible, as memory can be allocated and deallocated as needed. However, it is slower than stack allocation and can lead to memory fragmentation if not managed properly.

Similar threads

  • Programming and Computer Science
Replies
15
Views
6K
  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
Replies
2
Views
963
  • Science and Math Textbooks
Replies
1
Views
969
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top