0% found this document useful (0 votes)
8 views18 pages

Computer Memory Management

The document discusses computer memory management, focusing on the allocation, monitoring, and management of memory by the operating system. It explains the two main segments of memory: the stack, which is fast and managed automatically, and the heap, which is larger but requires manual management and can lead to memory leaks if not handled properly. Key differences between stack and heap memory are highlighted, including access speed, variable scope, and memory size limitations.

Uploaded by

Priome Takur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views18 pages

Computer Memory Management

The document discusses computer memory management, focusing on the allocation, monitoring, and management of memory by the operating system. It explains the two main segments of memory: the stack, which is fast and managed automatically, and the heap, which is larger but requires manual management and can lead to memory leaks if not handled properly. Key differences between stack and heap memory are highlighted, including access speed, variable scope, and memory size limitations.

Uploaded by

Priome Takur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Programming I

(PRP-211)

Computer Memory Management


Presented By:
Michael R Chinguwo
Computing and Information Technology Department
The Polytechnic

:265 (0) 993 195 721 :[email protected]


Introduction
 Memory management incorporates all processes and methodologies
for the effective use, allocation, monitoring and management of
computer memory.
 Memory management allows an underlying computer or operating
system (OS) to dynamically distribute memory across all running
processes, while ensuring optimal performance.
 Memory management deals with the management of a computer’s
physical memory or random access memory (RAM).
 Memory management is usually performed and managed by the host
operating system.
Segments of Memory
 When a program is loaded into memory, it’s organized into three areas
of memory, called segments:
i. The text segment,
ii. The stack segment
iii. The heap segment
The Text Segment
 Sometimes also called the code segment.
 Is where the compiled code of the program itself resides.
 This is the machine language representation of the program steps to be
carried out, including all functions making up the program, both user
and system defined.
The Text Segment (Cont’d)
 An executable program generated by a compiler will have the
following:
i. Code segment or text segment:
­ Code segment contains the code executable or code binary
ii. Data segment:
­ Data segment is sub divided into two parts
i. Initialized data segment:
• All the global, static and constant data are stored in the data
segment
ii. Uninitialized data segment:
• All the uninitialized
The Text Segment (Cont’d)
iii. Heap
iv. Stack
Stack
 It's a special region of your computer's memory that stores temporary
variables created by each function (including the main() function).
 The stack is a "LIFO" (last in, first out) data structure, that is managed
and optimized by the CPU quite closely.
 Every time a function declares a new variable, it is "pushed" onto the
stack.
 Then every time a function exits, all of the variables pushed onto the
stack by that function, are freed (that is to say, they are deleted).
 Once a stack variable is freed, that region of memory becomes
available for other stack variables.
Stack (Cont’d)
Advantage Of Using The Stack
­ Memory is managed for you.
­ Don't have to allocate memory by hand, or free it.
­ Reading from and writing to stack variables is very fast (the CPU
organizes stack memory efficiently)
Stack (Cont’d)
 When a function exits, all of its variables are popped off of the stack
(and hence lost forever).
 Stack variables are local in nature.
 This is related to a concept of variable scope, or local vs global
variables.
Stack (Cont’d)
 There is a limit (varies with OS) on the size of variables that can be
store on the stack. This is not the case for variables allocated on the
heap.
Stack (Cont’d)
 To summarize the stack:
­ The stack grows and shrinks as functions push and pop local
variables
­ There is no need to manage the memory yourself, variables are
allocated and freed automatically.
­ The stack has size limits.
­ Stack variables only exist while the function that created them, is
running.
Heap
 The heap is a region of your computer's memory that is not managed
automatically for you, and is not as tightly managed by the CPU.
 It is a more free-floating region of memory (and is larger).
 The heap contains a linked list of used and free blocks.
 New allocations on the heap (by new or malloc) are satisfied by
creating a suitable block from one of the free blocks.
 This requires updating list of blocks on the heap.
Heap (Cont’d)
 The size of the heap is set on application startup, but can grow as
space is needed (the allocator requests more memory from the
operating system).
 Stored in computer RAM like the stack.
 Variables on the heap must be destroyed manually and never fall out
of scope.
 The data is freed with delete, delete[] or free.
 Slower to allocate in comparison to variables on the stack.
 Used on demand to allocate a block of data for use by the program.
Heap (Cont’d)
 You would use the heap if you don’t know exactly how much data you
will need at runtime or if you need to allocate a lot of data.
 Once you have allocated memory on the heap, you are responsible for
using free() to deallocate that memory once you don't need it any
more.
 If you fail to do this, your program will have what is known as a
memory leak.
Heap (Cont’d)
Memory Leak.
 Occurs when a computer program consumes memory but is unable to
release it back to the operating system.
 A memory leak can diminish the performance of the computer by
reducing the amount of available memory.
Heap (Cont’d)
 The heap does not have size restrictions on variable size (apart from
the obvious physical limitations of your computer).
 Heap memory is slightly slower to be read from and written to,
because one has to use pointers to access memory on the heap.
­ We will talk about pointers next semester.
 Variables created on the heap are accessible by any function, anywhere
in your program. Heap variables are essentially global in scope.
Summary
Stack Heap
 Very fast access  Variables can be accessed globally
 Don't have to explicitly de-allocate  No limit on memory size
variables  (Relatively) slower access
 Space is managed efficiently by  No guaranteed efficient use of space,
CPU, memory will not become memory may become fragmented over time
fragmented as blocks of memory are allocated, then
 Local variables only freed
 You must manage memory (you're in
 Limit on stack size (OS-dependent)
charge of allocating and freeing variables)
 Variables cannot be resized  Variables can be resized using realloc()
END

You might also like