- #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.
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.