0% found this document useful (0 votes)
17 views13 pages

Unit 4 Part1B

The document discusses heap memory management, including allocation, deallocation, and fragmentation. It covers the role of the memory manager in allocating and freeing memory. It also discusses optimizing memory usage through exploiting locality and combining free space.

Uploaded by

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

Unit 4 Part1B

The document discusses heap memory management, including allocation, deallocation, and fragmentation. It covers the role of the memory manager in allocating and freeing memory. It also discusses optimizing memory usage through exploiting locality and combining free space.

Uploaded by

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

Unit 4

Heap Management - Managed by Memory Manager

● The heap is the portion of the store that is used for data that lives indefinitely, or until the program
explicitly deletes it.
● While local variables typically become inaccessible when their procedures end, many languages
enable us to create objects or other data whose existence is not tied to the procedure activation
that creates them.
● Eg. objects may be passed or pointers to them may be passed |from procedure to procedure, so
they continue to exist long after the procedure that created them is gone.
Role of Heap Manager

1. Allocation
a. When a program requests memory for a variable or object, the memory manager
produces a chunk of contiguous heap memory of the requested size.
b. If possible, it satisfies an allocation request using free space in the heap; if no chunk of
the needed size is available, it seeks to increase the heap storage space by getting
consecutive bytes of virtual memory from the operating system.
c. If space is exhausted, the memorymanager passes that information back to the
application program.
2. Deallocation
a. The memory manager returns deallocated space to the pool of free space, so it can
reuse the space to satisfy other allocation requests.
b. Memory managers typically do not return memory to the operating system, even if
the program's heap usage drops.
Required Properties of Memory Manager

● Space Efficiency
○ A memory manager should minimize the total heap space needed by a program. Doing so
allows larger programs to run in a fixed virtual address space. Space efficiency is achieved by
minimizing fragmentation,"
● Program Efficiency.
○ A memory manager should make good use of the memory subsystem to allow programs to
run faster
○ By attention to the placement of objects in memory, the memory manager can make better
use of space and, hopefully, make the program run faster.
● Low Overhead.
○ Because memory allocations and deallocations are frequent operations in many programs, it
is important that these operations be as efficient as possible.
○ That is, we wish to minimize the overhead - the fraction of execution time spent performing
allocation and deallocation
Memory Hierarchy
Key Points

● The efficiency of a program is determined by the time taken to execute a program.


● the smaller faster ones closer to the processor, and the larger slower ones further
away.
● Typically, a processor has a small number of registers, whose contents are under
software control.
● Next, it has one or more levels of cache, usually made out of static RAM, that are
kilobytes to several megabytes in size.
● The next level of the hierarchy is the physical (main) memory, made out of hundreds of
megabytes or gigabytes of dynamic RAM.
● The physical memory is then backed up by virtual memory, which is implemented by
gigabytes of disks.
● Upon a memory access, the machine first looks for the data in the closest
(lowest-level) storage and, if the data is not there, looks in the next higher level, and
so on
Locality in Program - programs spend most of their time
executing a relatively small fraction of the code and touching only a
small fraction of the data

Temporal locality - if the memory locations it accesses are likely to be accessed again within a short
period of time.

Spatial locality - if memory locations close to the location accessed are likely also to be accessed within a
short period of time.

Why only a small portion of code is generally used?

1. Programs often contain many instructions that are never executed. Programs built with
components and libraries use only a small fraction of the provided functionality.
2. Only a small fraction of the code that could be invoked is actually executed in a typical run of the
program. For example, instructions to handle illegal inputs and exceptional cases, though critical to
the correctness of the program, are seldom invoked on any particular run.
3. The typical program spends most of its time executing innermost loops and tight recursive cycles in
a program.
Optimizing using memory hierarchy

● The policy of keeping the most recently used instructions in the cache tends to work well.
● One effective technique 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.
○ For example, programs that visit large amounts of data repeatedly, each time
performing a small amount of computation, do not perform well.
○ It is better if we can bring some data from a slow level of the memory hierarchy to a
faster level (e.g., disk to main memory) once, and perform all the necessary
computations on this data while it resides at the faster leve
Reducing Fragmentation

● Best Fit
● Worst Fit
● Next Fit
Efficient Implementation of best fit - Binning
● separate free space chunks 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.
● For example, the Lea memory manager, used in the GNU C compiler gcc, aligns all chunks to 8-byte
boundaries.
● There is always a chunk of free space that can be extended by requesting more pages from the
operating system. Called the wilderness chunk, this chunk is treated by Lea as the largest-sized bin
because of its extensibility.
Combining 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.

Data Structures Used:

Boundary Tags.

At both the low and high ends of each chunk, whether free or allocated, we keep vital information. 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.
Manual Deallocation of Memory

Manual memory management is error-prone.

1. failing ever to delete data that cannot be referenced is called a memory leak error
2. referencing deleted data is a dangling-pointer-dereference error
● It is hard for programmers to tell if a program will never refer to some
storage in the future, so the first common mistake is not deleting storage
that will never be referenced.
● Memory leakage slows down program but correctness will not be impacted.
● Automatic garbage collection gets rid of memory leaks by deallocating all the
garbage
● 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.
● Notice that reading through a dangling pointer may return an arbitrary
value. Writing through a dangling pointer arbitrarily changes the value of the
new variable

You might also like