SlideShare a Scribd company logo
Heap Memory Management
Mrs.B.Vijayalakshmi
Assistant Professor(SG)/CSE
Ramco Institute of Technology, Rajapalayam
Heap
•Portion of the store used for data that lives
indefinitely or until explicitly deleted by the
program
Memory Manager
• The task of MM is to keep track of all the free space that can
be available in the heap
• The subsystem that allocates and deallocates space within
the heap
• Serves as an interface between application programs and
the operating system
Memory Allocation
 Produces a chunk of contiguous heap memory of the
requested size
 If no chunk of the needed size is available, it increases the
heap storage by getting consecutive bytes of virtual
memory from the OS
Memory Deallocation
 Returns deallocated space to the pool of free space
(so it can reuse the space)
 Typically does not return memory to the OS, even if
heap usage drops.
Properties of Memory Manager
• Space Efficiency-A MM should minimize the total heap
space needed by a program.
• Program Efficiency-A MM should make good use of the
memory sub system to allow programs to run faster.
• Low Overhead -Allocation and deallocation are frequent
operations in many programs hence to minimize the
overhead - the fraction of execution time spent performing
allocation and deallocation.
Memory Hierarchy of a Computer
• Compiler optimization is very much dependent upon the memory management
technique.
• The efficiency of the program is determined by the
• No of instruction getting executed
• The amount of time required by each instruction for execution.
• The time taken by the instruction to execute greatly depends upon how much
time it takes to access different parts of the memory.
• During Memory access the data is looked in the lower level of memory and if is
not found there then the next level is accessed and so on.
• A processors has several registers which can be controlled by the software.
Typical Memory Hierarchy Configurations
Locality in Programs
• Most programs exhibit a high degree of locality; that is, they spend
most of their time executing a relatively small fraction of the code
and touching only a small fraction of the data.
Two types of Locality
• A program has temporal locality if the memory locations it accesses
are likely to be accessed again within a short period of time.
• A program has spatial locality if memory locations close to the
location accessed are likely also to be accessed within a short period
of time.
90/10 Rule comes from the
empirical observation:
“A program spends 90% of its
time in 10% of its code.”
Optimization Using the Memory Hierarchy
• Keeping the most recently used instructions in the cache tends to work
well
• When a new instruction is executed, there is a high probability that the
next instruction also will be executed.
• To improve the spatial locality of instructions is to have the compiler place
basic blocks (sequences of instructions that are always executed
sequentially) that are likely to follow each other contiguously - on the
same page, or even the same cache line, if possible.
• Instructions belonging to the same loop or same function also have a high
probability of being executed together.
• We can also improve the temporal and spatial locality of data accesses in a
program by changing the data layout or the order of the computation
Reducing Fragmentation
• At the beginning of program execution, the heap is one contiguous unit of free
space.
• As the program allocates and deallocates memory, this space is broken up into
free and used chunks of memory, and the free chunks need not reside in a
contiguous area of the heap.
• We refer to the free chunks of memory as holes.
• With each allocation request, the memory manager must place the requested
chunk of memory into a large-enough hole.
• With each deallocation request, the freed chunks of memory are added back to
the pool of free space.
• We coalesce contiguous holes into larger holes, as the holes can only get smaller
otherwise.
• The memory may end up getting fragmented, consisting of large numbers of
small, noncontiguous holes.
• It is then possible that no hole is large enough to satisfy a future request, even
though there may be sufficient aggregate free space
Best-Fit and Next-Fit Object Placement
• We reduce fragmentation by controlling how the memory manager places
new objects in the heap.
• Best-fit - To allocate the requested memory in the smallest available hole
that is large enough. Helps to spare the large holes to satisfy subsequent,
larger requests.
• First-fit, where an object is placed in the first (lowest-address) hole in
which it fits, takes less time to place objects, but has been found inferior to
best-fit in overall performance.
• To implement best-fit placement more efficiently, we can separate free
space into bins, according to their sizes.
• One practical idea is to have many more bins for the smaller sizes, because
there are usually many more small objects.
Dynamic Storage-Allocation Problem
• How to satisfy a request of size n from a list of free holes
 First-fit: Allocate the first hole that is big enough
 Best-fit: Allocate the smallest hole that is big enough; must
search entire list, unless ordered by size
 Produces the smallest leftover hole
 Worst-fit: Allocate the largest hole; must also search entire list
 Produces the largest leftover hole
Managing and Coalescing Free Space
• When an object is deallocated manually, the memory manager must
make its chunk free, so it can be allocated again.
• In some circumstances, it may also be possible to combine (coalesce)
that chunk with adjacent chunks of the heap, to form a larger
chunk.
• If we keep a bin for chunks of one fixed size, then we may prefer not
to coalesce adjacent blocks of that size into a chunk of double the
size.
Coalescing of adjacent free blocks
• There are two data structures that are useful to support coalescing of adjacent free
blocks:
• Boundary Tags.
• At both ends, we keep a free/used bit that tells whether or not the block is
currently allocated (used) or available (free).
• Adjacent to each free/used bit is a count of the total number of bytes in the chunk.
• A Doubly Linked, Embedded Free List.
• The free chunks (but not the allocated chunks) are also linked in a doubly linked
list.
• The pointers for this list are within the blocks themselves, say adjacent to the
boundary tags at either end.
• Thus, no additional space is needed for the free list; they must accommodate two
boundary tags and two pointers, even if the object is a single byte.
• The order of chunks on the free list is left unspecified. For example, the list could be
sorted by size, thus facilitating best-fit placement.
Manual Deallocation Request
• Manual memory management is error-prone. The common mistakes
take two forms:
• Failing ever to delete data that cannot be referenced is called a
memory leak error
• Automatic garbage collection gets rid of memory leaks by
deallocating all the garbage.
• Referencing deleted data is a dangling-pointer-dereference error.
• A related form of programming error is to access an illegal address.
Common examples of such errors include dereferencing null pointers
and accessing an out-of-bounds array element.
Dangling pointers
• The second common mistake is to delete some storage and then try
to refer to the data in the deallocated storage. Pointers to storage
that has been deallocated are known as dangling pointers.
• Once the freed storage has been reallocated to a new variable, any
read, write, or deallocation via the dangling pointer can produce
seemingly random effects. We refer to any operation, such as read,
write, or deallocate, that follows a pointer and tries to use the object
it points to, as dereferencing the pointer
• Dereferencing a dangling pointer after the freed storage is reallocated
almost always creates a program error that is hard to debug
Programming Conventions and Tools
Tools to help programmers in managing memory:
• Object ownership is useful when an object's lifetime can be statically
reasoned about.
• The idea is to associate an owner with each object at all times.
• The owner is a pointer to that object, presumably belonging to some
function invocation.
• The owner (i.e., its function) is responsible for either deleting the
object or for passing the object to another owner
Programming Conventions and Tools
• Reference counting is useful when an object's lifetime needs to be
determined dynamically.
• The idea is to associate a count with each dynamically allocated
object.
• Whenever a reference to the object is created, we increment the
reference count; whenever a reference is removed, we decrement the
reference count.
• When the count goes to zero, the object can no longer be
referenced and can therefore be deleted
Programming Conventions and Tools
• Region-based allocation is useful for collections of objects whose
lifetimes are tied to specific phases in a computation.
• When objects are created to be used only within some step of a
computation, we can allocate all such objects in the same region.
• We then delete the entire region once that computation step
completes.
References
• Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, Compilers:
Principles, Techniques and Tools, Second Edition, Pearson Education,
2009.

More Related Content

What's hot

Thrashing allocation frames.43
Thrashing allocation frames.43Thrashing allocation frames.43
Thrashing allocation frames.43
myrajendra
 
MEDIUM ACCESS CONTROL
MEDIUM ACCESS CONTROLMEDIUM ACCESS CONTROL
MEDIUM ACCESS CONTROL
junnubabu
 
Ddbms1
Ddbms1Ddbms1
Ddbms1
pranjal_das
 
key distribution in network security
key distribution in network securitykey distribution in network security
key distribution in network security
babak danyal
 
Concurrency Control in Distributed Database.
Concurrency Control in Distributed Database.Concurrency Control in Distributed Database.
Concurrency Control in Distributed Database.
Meghaj Mallick
 
Replication in Distributed Systems
Replication in Distributed SystemsReplication in Distributed Systems
Replication in Distributed Systems
Kavya Barnadhya Hazarika
 
ML_ Unit 2_Part_B
ML_ Unit 2_Part_BML_ Unit 2_Part_B
ML_ Unit 2_Part_B
Srimatre K
 
Lamport’s algorithm for mutual exclusion
Lamport’s algorithm for mutual exclusionLamport’s algorithm for mutual exclusion
Lamport’s algorithm for mutual exclusion
Neelamani Samal
 
Distributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Mutual Exclusion and Distributed Deadlock DetectionDistributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Mutual Exclusion and Distributed Deadlock Detection
SHIKHA GAUTAM
 
Distributed Mutual exclusion algorithms
Distributed Mutual exclusion algorithmsDistributed Mutual exclusion algorithms
Distributed Mutual exclusion algorithms
MNM Jain Engineering College
 
Deadlock
DeadlockDeadlock
Deadlock
Rajandeep Gill
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
Vaibhav Khanna
 
8. mutual exclusion in Distributed Operating Systems
8. mutual exclusion in Distributed Operating Systems8. mutual exclusion in Distributed Operating Systems
8. mutual exclusion in Distributed Operating Systems
Dr Sandeep Kumar Poonia
 
Memory allocation (4)
Memory allocation (4)Memory allocation (4)
Memory allocation (4)
rockymani
 
Message and Stream Oriented Communication
Message and Stream Oriented CommunicationMessage and Stream Oriented Communication
Message and Stream Oriented Communication
Dilum Bandara
 
Artificial Intelligence - Reasoning in Uncertain Situations
Artificial Intelligence - Reasoning in Uncertain SituationsArtificial Intelligence - Reasoning in Uncertain Situations
Artificial Intelligence - Reasoning in Uncertain Situations
Laguna State Polytechnic University
 
Directory structure
Directory structureDirectory structure
Directory structure
sangrampatil81
 
3.3 hierarchical methods
3.3 hierarchical methods3.3 hierarchical methods
3.3 hierarchical methods
Krish_ver2
 
Global state recording in Distributed Systems
Global state recording in Distributed SystemsGlobal state recording in Distributed Systems
Global state recording in Distributed Systems
Arsnet
 
Load Balancing in Parallel and Distributed Database
Load Balancing in Parallel and Distributed DatabaseLoad Balancing in Parallel and Distributed Database
Load Balancing in Parallel and Distributed Database
Md. Shamsur Rahim
 

What's hot (20)

Thrashing allocation frames.43
Thrashing allocation frames.43Thrashing allocation frames.43
Thrashing allocation frames.43
 
MEDIUM ACCESS CONTROL
MEDIUM ACCESS CONTROLMEDIUM ACCESS CONTROL
MEDIUM ACCESS CONTROL
 
Ddbms1
Ddbms1Ddbms1
Ddbms1
 
key distribution in network security
key distribution in network securitykey distribution in network security
key distribution in network security
 
Concurrency Control in Distributed Database.
Concurrency Control in Distributed Database.Concurrency Control in Distributed Database.
Concurrency Control in Distributed Database.
 
Replication in Distributed Systems
Replication in Distributed SystemsReplication in Distributed Systems
Replication in Distributed Systems
 
ML_ Unit 2_Part_B
ML_ Unit 2_Part_BML_ Unit 2_Part_B
ML_ Unit 2_Part_B
 
Lamport’s algorithm for mutual exclusion
Lamport’s algorithm for mutual exclusionLamport’s algorithm for mutual exclusion
Lamport’s algorithm for mutual exclusion
 
Distributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Mutual Exclusion and Distributed Deadlock DetectionDistributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Mutual Exclusion and Distributed Deadlock Detection
 
Distributed Mutual exclusion algorithms
Distributed Mutual exclusion algorithmsDistributed Mutual exclusion algorithms
Distributed Mutual exclusion algorithms
 
Deadlock
DeadlockDeadlock
Deadlock
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
 
8. mutual exclusion in Distributed Operating Systems
8. mutual exclusion in Distributed Operating Systems8. mutual exclusion in Distributed Operating Systems
8. mutual exclusion in Distributed Operating Systems
 
Memory allocation (4)
Memory allocation (4)Memory allocation (4)
Memory allocation (4)
 
Message and Stream Oriented Communication
Message and Stream Oriented CommunicationMessage and Stream Oriented Communication
Message and Stream Oriented Communication
 
Artificial Intelligence - Reasoning in Uncertain Situations
Artificial Intelligence - Reasoning in Uncertain SituationsArtificial Intelligence - Reasoning in Uncertain Situations
Artificial Intelligence - Reasoning in Uncertain Situations
 
Directory structure
Directory structureDirectory structure
Directory structure
 
3.3 hierarchical methods
3.3 hierarchical methods3.3 hierarchical methods
3.3 hierarchical methods
 
Global state recording in Distributed Systems
Global state recording in Distributed SystemsGlobal state recording in Distributed Systems
Global state recording in Distributed Systems
 
Load Balancing in Parallel and Distributed Database
Load Balancing in Parallel and Distributed DatabaseLoad Balancing in Parallel and Distributed Database
Load Balancing in Parallel and Distributed Database
 

Similar to Heap Memory Management.pptx

Cache Memory.pptx
Cache Memory.pptxCache Memory.pptx
Cache Memory.pptx
ssusere16bd9
 
memory managment on computer science.ppt
memory managment on computer science.pptmemory managment on computer science.ppt
memory managment on computer science.ppt
footydigarse
 
Os unit 3
Os unit 3Os unit 3
Os unit 3
SandhyaTatekalva
 
4 memory management bb
4   memory management bb4   memory management bb
4 memory management bb
Shahid Riaz
 
Internet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IVInternet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IV
Arti Parab Academics
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
Afaq Mansoor Khan
 
Memory management
Memory managementMemory management
Memory management
PATELARCH
 
UNIT 3-EXPLAINING THE MEMORY MANAGEMENT LOGICAL AND AND PHYSICAL DATA FLOW DI...
UNIT 3-EXPLAINING THE MEMORY MANAGEMENT LOGICAL AND AND PHYSICAL DATA FLOW DI...UNIT 3-EXPLAINING THE MEMORY MANAGEMENT LOGICAL AND AND PHYSICAL DATA FLOW DI...
UNIT 3-EXPLAINING THE MEMORY MANAGEMENT LOGICAL AND AND PHYSICAL DATA FLOW DI...
LeahRachael
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
Iffat Anjum
 
Stack and heap
Stack and heapStack and heap
Memory Management
Memory ManagementMemory Management
Memory Management
DEDE IRYAWAN
 
Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory management
rprajat007
 
C dynamic ppt
C dynamic pptC dynamic ppt
C dynamic ppt
RJ Mehul Gadhiya
 
Memory Management
Memory ManagementMemory Management
Memory Management
jayalakshmi268
 
Lecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptxLecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptx
Amanuelmergia
 
Designing data intensive applications
Designing data intensive applicationsDesigning data intensive applications
Designing data intensive applications
Hemchander Sannidhanam
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)
Prashanth Kumar
 
Selecting the right cache framework
Selecting the right cache frameworkSelecting the right cache framework
Selecting the right cache framework
Mohammed Fazuluddin
 
Cache Memory.pptx
Cache Memory.pptxCache Memory.pptx
Cache Memory.pptx
AshokRachapalli1
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
Kumar Pritam
 

Similar to Heap Memory Management.pptx (20)

Cache Memory.pptx
Cache Memory.pptxCache Memory.pptx
Cache Memory.pptx
 
memory managment on computer science.ppt
memory managment on computer science.pptmemory managment on computer science.ppt
memory managment on computer science.ppt
 
Os unit 3
Os unit 3Os unit 3
Os unit 3
 
4 memory management bb
4   memory management bb4   memory management bb
4 memory management bb
 
Internet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IVInternet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IV
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Memory management
Memory managementMemory management
Memory management
 
UNIT 3-EXPLAINING THE MEMORY MANAGEMENT LOGICAL AND AND PHYSICAL DATA FLOW DI...
UNIT 3-EXPLAINING THE MEMORY MANAGEMENT LOGICAL AND AND PHYSICAL DATA FLOW DI...UNIT 3-EXPLAINING THE MEMORY MANAGEMENT LOGICAL AND AND PHYSICAL DATA FLOW DI...
UNIT 3-EXPLAINING THE MEMORY MANAGEMENT LOGICAL AND AND PHYSICAL DATA FLOW DI...
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory management
 
C dynamic ppt
C dynamic pptC dynamic ppt
C dynamic ppt
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Lecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptxLecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptx
 
Designing data intensive applications
Designing data intensive applicationsDesigning data intensive applications
Designing data intensive applications
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)
 
Selecting the right cache framework
Selecting the right cache frameworkSelecting the right cache framework
Selecting the right cache framework
 
Cache Memory.pptx
Cache Memory.pptxCache Memory.pptx
Cache Memory.pptx
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 

More from Viji B

ML & AI based Cloud Compiler.pptx
ML & AI based Cloud Compiler.pptxML & AI based Cloud Compiler.pptx
ML & AI based Cloud Compiler.pptx
Viji B
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B
 
Innovative pratice crossword
Innovative pratice crosswordInnovative pratice crossword
Innovative pratice crossword
Viji B
 
Innovative practice role play sorting
Innovative practice role play sortingInnovative practice role play sorting
Innovative practice role play sorting
Viji B
 
Innovative Practice- Learning by doing- Complete the code and trace the exec...
Innovative  Practice- Learning by doing- Complete the code and trace the exec...Innovative  Practice- Learning by doing- Complete the code and trace the exec...
Innovative Practice- Learning by doing- Complete the code and trace the exec...
Viji B
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree ds
Viji B
 

More from Viji B (6)

ML & AI based Cloud Compiler.pptx
ML & AI based Cloud Compiler.pptxML & AI based Cloud Compiler.pptx
ML & AI based Cloud Compiler.pptx
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Innovative pratice crossword
Innovative pratice crosswordInnovative pratice crossword
Innovative pratice crossword
 
Innovative practice role play sorting
Innovative practice role play sortingInnovative practice role play sorting
Innovative practice role play sorting
 
Innovative Practice- Learning by doing- Complete the code and trace the exec...
Innovative  Practice- Learning by doing- Complete the code and trace the exec...Innovative  Practice- Learning by doing- Complete the code and trace the exec...
Innovative Practice- Learning by doing- Complete the code and trace the exec...
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree ds
 

Recently uploaded

20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 

Recently uploaded (20)

20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 

Heap Memory Management.pptx

  • 1. Heap Memory Management Mrs.B.Vijayalakshmi Assistant Professor(SG)/CSE Ramco Institute of Technology, Rajapalayam
  • 2. Heap •Portion of the store used for data that lives indefinitely or until explicitly deleted by the program
  • 3. Memory Manager • The task of MM is to keep track of all the free space that can be available in the heap • The subsystem that allocates and deallocates space within the heap • Serves as an interface between application programs and the operating system
  • 4. Memory Allocation  Produces a chunk of contiguous heap memory of the requested size  If no chunk of the needed size is available, it increases the heap storage by getting consecutive bytes of virtual memory from the OS
  • 5. Memory Deallocation  Returns deallocated space to the pool of free space (so it can reuse the space)  Typically does not return memory to the OS, even if heap usage drops.
  • 6. Properties of Memory Manager • Space Efficiency-A MM should minimize the total heap space needed by a program. • Program Efficiency-A MM should make good use of the memory sub system to allow programs to run faster. • Low Overhead -Allocation and deallocation are frequent operations in many programs hence to minimize the overhead - the fraction of execution time spent performing allocation and deallocation.
  • 7. Memory Hierarchy of a Computer • Compiler optimization is very much dependent upon the memory management technique. • The efficiency of the program is determined by the • No of instruction getting executed • The amount of time required by each instruction for execution. • The time taken by the instruction to execute greatly depends upon how much time it takes to access different parts of the memory. • During Memory access the data is looked in the lower level of memory and if is not found there then the next level is accessed and so on. • A processors has several registers which can be controlled by the software.
  • 8. Typical Memory Hierarchy Configurations
  • 9. Locality in Programs • Most programs exhibit a high degree of locality; that is, they spend most of their time executing a relatively small fraction of the code and touching only a small fraction of the data. Two types of Locality • A program has temporal locality if the memory locations it accesses are likely to be accessed again within a short period of time. • A program has spatial locality if memory locations close to the location accessed are likely also to be accessed within a short period of time. 90/10 Rule comes from the empirical observation: “A program spends 90% of its time in 10% of its code.”
  • 10. Optimization Using the Memory Hierarchy • Keeping the most recently used instructions in the cache tends to work well • When a new instruction is executed, there is a high probability that the next instruction also will be executed. • To improve the spatial locality of instructions is to have the compiler place basic blocks (sequences of instructions that are always executed sequentially) that are likely to follow each other contiguously - on the same page, or even the same cache line, if possible. • Instructions belonging to the same loop or same function also have a high probability of being executed together. • We can also improve the temporal and spatial locality of data accesses in a program by changing the data layout or the order of the computation
  • 11. Reducing Fragmentation • At the beginning of program execution, the heap is one contiguous unit of free space. • As the program allocates and deallocates memory, this space is broken up into free and used chunks of memory, and the free chunks need not reside in a contiguous area of the heap. • We refer to the free chunks of memory as holes. • With each allocation request, the memory manager must place the requested chunk of memory into a large-enough hole. • With each deallocation request, the freed chunks of memory are added back to the pool of free space. • We coalesce contiguous holes into larger holes, as the holes can only get smaller otherwise. • The memory may end up getting fragmented, consisting of large numbers of small, noncontiguous holes. • It is then possible that no hole is large enough to satisfy a future request, even though there may be sufficient aggregate free space
  • 12. Best-Fit and Next-Fit Object Placement • We reduce fragmentation by controlling how the memory manager places new objects in the heap. • Best-fit - To allocate the requested memory in the smallest available hole that is large enough. Helps to spare the large holes to satisfy subsequent, larger requests. • First-fit, where an object is placed in the first (lowest-address) hole in which it fits, takes less time to place objects, but has been found inferior to best-fit in overall performance. • To implement best-fit placement more efficiently, we can separate free space into bins, according to their sizes. • One practical idea is to have many more bins for the smaller sizes, because there are usually many more small objects.
  • 13. Dynamic Storage-Allocation Problem • How to satisfy a request of size n from a list of free holes  First-fit: Allocate the first hole that is big enough  Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size  Produces the smallest leftover hole  Worst-fit: Allocate the largest hole; must also search entire list  Produces the largest leftover hole
  • 14. Managing and Coalescing Free Space • When an object is deallocated manually, the memory manager must make its chunk free, so it can be allocated again. • In some circumstances, it may also be possible to combine (coalesce) that chunk with adjacent chunks of the heap, to form a larger chunk. • If we keep a bin for chunks of one fixed size, then we may prefer not to coalesce adjacent blocks of that size into a chunk of double the size.
  • 15. Coalescing of adjacent free blocks • There are two data structures that are useful to support coalescing of adjacent free blocks: • Boundary Tags. • At both ends, we keep a free/used bit that tells whether or not the block is currently allocated (used) or available (free). • Adjacent to each free/used bit is a count of the total number of bytes in the chunk. • A Doubly Linked, Embedded Free List. • The free chunks (but not the allocated chunks) are also linked in a doubly linked list. • The pointers for this list are within the blocks themselves, say adjacent to the boundary tags at either end. • Thus, no additional space is needed for the free list; they must accommodate two boundary tags and two pointers, even if the object is a single byte. • The order of chunks on the free list is left unspecified. For example, the list could be sorted by size, thus facilitating best-fit placement.
  • 16. Manual Deallocation Request • Manual memory management is error-prone. The common mistakes take two forms: • Failing ever to delete data that cannot be referenced is called a memory leak error • Automatic garbage collection gets rid of memory leaks by deallocating all the garbage. • Referencing deleted data is a dangling-pointer-dereference error. • A related form of programming error is to access an illegal address. Common examples of such errors include dereferencing null pointers and accessing an out-of-bounds array element.
  • 17. Dangling pointers • The second common mistake is to delete some storage and then try to refer to the data in the deallocated storage. Pointers to storage that has been deallocated are known as dangling pointers. • Once the freed storage has been reallocated to a new variable, any read, write, or deallocation via the dangling pointer can produce seemingly random effects. We refer to any operation, such as read, write, or deallocate, that follows a pointer and tries to use the object it points to, as dereferencing the pointer • Dereferencing a dangling pointer after the freed storage is reallocated almost always creates a program error that is hard to debug
  • 18. Programming Conventions and Tools Tools to help programmers in managing memory: • Object ownership is useful when an object's lifetime can be statically reasoned about. • The idea is to associate an owner with each object at all times. • The owner is a pointer to that object, presumably belonging to some function invocation. • The owner (i.e., its function) is responsible for either deleting the object or for passing the object to another owner
  • 19. Programming Conventions and Tools • Reference counting is useful when an object's lifetime needs to be determined dynamically. • The idea is to associate a count with each dynamically allocated object. • Whenever a reference to the object is created, we increment the reference count; whenever a reference is removed, we decrement the reference count. • When the count goes to zero, the object can no longer be referenced and can therefore be deleted
  • 20. Programming Conventions and Tools • Region-based allocation is useful for collections of objects whose lifetimes are tied to specific phases in a computation. • When objects are created to be used only within some step of a computation, we can allocate all such objects in the same region. • We then delete the entire region once that computation step completes.
  • 21. References • Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, Compilers: Principles, Techniques and Tools, Second Edition, Pearson Education, 2009.