Learn Memory Mgmt: Understanding Static, Stack & Heap Allocation

  • Thread starter Thread starter Chromium
  • Start date Start date
  • Tags Tags
    Management Memory
Click For Summary

Discussion Overview

The discussion focuses on memory management concepts in programming, specifically static, stack, and heap allocation. Participants share insights and seek critiques on their understanding, touching on theoretical aspects and practical implications in programming environments.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant describes static allocation as involving objects and variables with lifetimes equal to the program's execution, citing global variables as examples.
  • Another participant explains stack allocation in terms of a call stack, detailing how function calls create frames that hold local variables and arguments, and notes the relationship between stacks and concurrency.
  • A participant mentions heap allocation as the space for dynamically-created objects, discussing fragmentation issues such as internal and external fragmentation.
  • One participant introduces a compaction algorithm used in the Windows .NET framework to manage small allocated objects and reduce memory waste, although they express uncertainty about the specific algorithm used.
  • Another participant points out that most operating systems utilize virtual memory, which allows programs to see a linear increase in memory pages that do not correspond to physical RAM addresses.

Areas of Agreement / Disagreement

Participants generally agree on the basic concepts of memory management, but there are varying levels of detail and understanding regarding specific algorithms and implementations, indicating that the discussion remains open and exploratory.

Contextual Notes

Some participants express uncertainty about specific algorithms related to memory management, such as the one used for tracking small allocated objects in the .NET framework. Additionally, the discussion does not resolve the complexities of virtual memory management and its implications.

Chromium
Messages
56
Reaction score
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
that's a pretty good summary; some students do a lot of programming and never achieve this much insight. good job.
 
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.
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 15 ·
Replies
15
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K