Internal memory reallocation: partitioning technical guide

  • Memory management is based on how RAM is partitioned and reallocated between processes, seeking to balance performance and utilization.
  • Models such as fixed partitions, variable partitions, paging, and segmentation address internal and external fragmentation in different ways.
  • The combination of paging, segmentation, and virtual memory allows for the execution of more processes and larger processes than physical memory can accommodate.
  • Design and memory management decisions directly impact perceived performance on computers and mobile devices.

partitioning of internal memory

When talking about internal memory reallocation and partitioning This isn't just a theoretical issue concerning operating systems; it's a very practical matter that affects the actual performance of a computer, server, or even a mobile device. Understanding how main memory is divided, reserved, protected, and reused is key to understanding why some systems perform exceptionally well while others struggle, even with the same hardware.

In the following sections we will go over, in considerable detail, the different memory partitioning and reallocation techniquesFrom the simplest single-process systems to advanced models with virtual memory, paging, segmentation, and combinations thereof. We will also look at typical problems such as internal and external fragmentation, and how all of this translates into practical design and management decisions.

Overview of memory management

We can imagine the principal memory as a large set of numbered boxeswhere each cell has an address and can store instructions or data. For a program to run, its code and data must be in this main memory (RAM), from where the processor will read instructions, access operands, and write results.

The operating system module responsible for this task is the memory manager or managerThis function decides which portion of memory is allocated to each process, how each region is protected, and what happens when memory runs out. If this management is flawed or inefficient, the effect is immediately noticeable: more waiting, more disk accesses, and a clear decrease in overall performance.

With the evolution of computer systems, several proposals have been made progressively more sophisticated models Memory management, starting with very simple systems designed to execute a single task, up to modern virtual memory schemes where the program's view of memory (logical space) is very different from the real physical space.

Android phone memory full partitions
Related article:
How to delete the Android partition on DualBoot Tablets to free up space on Windows

Basic model: memory for a single process

In early computers, main memory was divided into two large, clearly separated areasOne partition was reserved for the operating system (often called the monitor), and another for a single user program. Only one process could run at a time, and when it finished, control returned to the operating system, which loaded the next program.

A classic example of this approach is MS-DOS in its early versionsIn this model, the operating system and, in many cases, the BIOS code occupied the upper portion of the address space, leaving the rest for the running program. There was no true multiprogramming or complex dynamic reallocation: it was a simple and straightforward scheme.

Contiguous memory allocation and partitioning

When multiprogramming appears, a model is proposed in which the The logical space of each process occupies a contiguous region of physical memory. That is, the process's code, data, and stack must be in consecutive physical addresses. The operating system is responsible for choosing which contiguous region to place each process in, and when the process finishes, it releases that block.

This approach, known as contiguous memory allocationIt's simple to implement, but it has its drawbacks. As processes are loaded and unloaded, free spaces of varying sizes appear, and the way to divide and reuse these spaces is what gives rise to the different partitioning techniques we'll see below.

Memory management with fixed partitions

In fixed partition schemes, main memory is divides beforehand into fragments of a set sizebefore the programs are executed. Each of these fragments, or partitions, can hold a single process, provided its logical size fits within it.

This method is also known as Multiprogramming with a fixed number of tasks (MFT)The maximum number of processes that can run simultaneously is equal to the number of partitions. It's a relatively easy system to manage, but it faces a constant dilemma: how to choose the partition size to avoid wasting memory.

Process queues and work organization

Workload can be organized in several ways. One option is maintain one queue per partitionso that processes are assigned to the partition list whose size best suits them. Another possibility is a single queue of ready processes and that the scheduler, when a partition becomes free, chooses from the general queue the process that fits in it.

Model single tail It simplifies the structure, but tends to discriminate against small processes: they are easily assigned a partition much larger than they need, wasting space. Processes do not leave the request queue until the operating system assigns them a suitable partition, usually following a FIFO (first in, first out) criterion.

Internal and external fragmentation in fixed partitions

In this context, two key concepts emerge: the internal fragmentation and external fragmentationInternal fragmentation occurs when a process is assigned a partition much larger than it actually requires, so that the excess within that partition remains unused but cannot be reused for another process.

For its part, the external fragmentation This occurs when there are many small, scattered free spaces in memory, so small that it's difficult to place processes within them. Even if the total free memory is considerable, because it's not contiguous or properly grouped, it doesn't allow for the accommodation of new processes of a certain size.

Achieve a balance between partition size and average process size It's complicated. Partitions that are too large encourage internal fragmentation; partitions that are too small can generate a lot of external fragmentation because it's difficult to find processes that fit into those gaps.

Multiple fixed-size partitions with a single queue

In the contiguous assignment technique with multiple fixed-size partitions and a single queueMemory is divided into fixed partitions, and all processes are stored in a single request queue. When a sufficiently large free partition becomes available for the first process in the queue, it is allocated and loaded into memory.

The system normally respects an order. FIFO in the process queueThis prevents processes from skipping to later ones, even if they might be a better fit for the available partition. While this can reduce efficiency, it simplifies scheduling. Once a process finishes executing, it completely releases the partition it was in, making it available again for another process.

This diagram clearly shows the internal fragmentationIf a process occupies only part of a partition, the remaining space cannot be used by another process. In many educational implementations, this unused area is represented with a different color (for example, gray) so that the degree of wasted space within each partition can be seen at a glance.

Frequently asked questions about this model

When this type of partitioning is analyzed in detail, questions arise such as what would happen if Most of the processes that arrive are very small.In that scenario, many partitions, especially the larger ones, waste a large amount of internal memory, since each process leaves a considerable portion of the allocated space unused.

The opposite situation occurs if most of the applications correspond to very large processes, close to the size of the largest partitionsHere it may happen that few partitions are able to accommodate them, which reduces the degree of effective multiprogramming and may increase waiting times in the general queue.

One possible theoretical improvement is to maintain one queue per partition sizeIn this way, processes were grouped according to their size, and an attempt was made to better fit each process to the type of partition that corresponded to it. This organization can reduce internal fragmentation, although it complicates the scheduling logic and increases the overhead of queue management.

Relocation, logical and physical addresses

partition internal memory

To allow some flexibility in memory allocation and reallocation, systems can use relocation of processesThis means that the same program can run on different partitions in different executions, or even be moved from one partition to another during the same execution, as long as the memory references are translated correctly.

From this idea two basic concepts arise: the logical or virtual address, which is the one used by the program (relative to the beginning of its address space), and the physical addresswhich is the actual location in main memory. To perform this translation quickly, the processor has a base register that contains the physical address where the partition assigned to the process begins.

In addition to the base register, a limit record indicating size of the partition or the process's address space. Each time the process references a logical address, the hardware increments the base register value and checks that the offset does not exceed the limit. If it does, a memory violation is detected, and the operating system can intervene to protect system integrity.

Variable partitions: flexibility with cost

To overcome the limitations of fixed partitions, schemes were designed for variable partitionswhere the size of the memory blocks occupied by each process can change over time. Instead of predefining rigid partitions, the system maintains a table of occupied and free regions.

When a new process arrives, a search is made for a sufficiently large contiguous memory block to contain it. The process is only allocated the necessary portion, and if the free block was larger, the excess becomes a new free fragment. When the process terminates, its space is marked as available, and if it borders other free blocks, they are merged to form a larger fragment.

This model reduces certain types of waste, but over time it tends to generate considerable external fragmentationBecause memory becomes filled with many free spaces of varying sizes, even if the total amount of free memory is sufficient, it can become impossible to find a contiguous fragment of the size required by a specific process.

Anatomy of storage: System, cache, and user files
Related article:
Storage Anatomy: System, Cache, and User Files

Allocation strategies in variable partitions

Several classic strategies are used to decide which free fragment to host a process in, each with its advantages and disadvantages. The most direct is the first-fitwhich assigns the process to the first available block of memory. It's fast, but tends to leave many small fragments at the beginning of memory over time.

One variant is the next-fitThis method starts the search from the last allocation point instead of always starting from the beginning. It often causes free space to become fragmented, especially towards the end of memory, which ultimately leads to relatively frequent compaction.

Another approach is best fitThis approach attempts to maximize memory utilization by assigning the process to the smallest free block that can hold it. While this sounds very efficient, in practice it tends to generate many tiny, hard-to-use fragments, often worsening the external fragmentation situation.

Finally, the worst-fit It opts to use the largest free blocks first, hoping that the remaining space will still be of a reasonable size for future processes. This can delay the need for compaction, but it requires traversing all free blocks to make the decision, which implies more overhead.

Memory compaction

When external fragmentation becomes problematic, recourse is made to memory compactionThis procedure involves moving all occupied blocks to one end of memory (usually to lower addresses) to group and merge the free spaces into a single block or very few large blocks.

Compaction allows for the recovery of a large contiguous free memory spaceHowever, it comes at a significant cost: it's a slow operation, as it involves copying entire blocks of memory and updating all associated references or structures. Therefore, efforts are made to postpone it as much as possible by implementing sound allocation policies to delay the need for compaction.

Pagination: breaking of contiguity

To prevent free memory from having to be in contiguous blocks, the following is introduced: paginationIn this model, physical memory is divided into fixed-size units called page frames, and the logical space of each process is similarly divided into pages of the same size.

Each page of a process can be stored in any free page frameThis is possible without requiring all frames to be physically contiguous. For each process, the operating system maintains a page table indicating which logical page is located in which physical frame. Thus, the program continues to perceive its memory as contiguous, even though it is actually spread across RAM.

This technique virtually eliminates the external fragmentationBecause to run a process, only a certain number of free frames are needed, regardless of their location. Internal fragmentation is limited to what remains within the last frame allocated to a process; on average, approximately half the size of a page is wasted in that last frame.

Address translation in pagination

In a paged system, logical addresses are divided into two parts: the page number and the offset within that pageThe page number acts as an index to query the process's page table and obtain the associated physical frame. Then, the offset is concatenated or summed to obtain the final physical address.

Since this translation is done in each memory accessIt would be impractical to do this solely through software. Therefore, specific hardware (such as the TLB, Translation Lookaside Buffer) is used to accelerate queries to the page table and minimize the performance impact. The operating system also maintains a global frame map indicating which frames are free, which are occupied, and by which process.

Choosing the page size is an important commitment: with small pagesInternal fragmentation is reduced, but page tables become larger, consuming additional memory. large pagesLess memory is needed for the tables, but internal fragmentation increases, since the last frame of each process tends to leave more unused space.

Segmentation: logical organization of programs and data

Unlike pagination, which stems from a purely technical perspective, segmentation It focuses more on how programmers and users structure code and data. A program can be viewed as a set of logical components of varying size: functions, modules, tables, stacks, data areas, etc.

In segmentation, the compiler generates a set of segments, each with an identifier, a start, and a sizeThe logical addresses used by the program are expressed as a pair (segment number, offset within the segment). The system ensures that the offset does not exceed the declared segment size, preventing out-of-range references.

One important advantage is the ability to protect and share segments with a high degree of granularity. For example, segments containing code can be marked as read-only, one module can be prevented from accessing another's data, or a code segment can be shared among several processes to save memory. All of this is controlled by a table of segments and their associated protection attributes.

As with paging, segmentation requires a mechanism for translate logical addresses to physical addressesIn this case, the segment table contains the physical base address and maximum size for each segment. The hardware adds the offset to the base address and checks that the limit is not exceeded, thus ensuring protection.

Combination of pagination and segmentation

Many modern systems combine the advantages of segmentation and paginationFrom the programmer's point of view, the process is still organized into logical segments (code, stack, data, etc.), but internally each of those segments is divided into fixed-length pages for their location in physical memory.

In this approach, the segment table no longer points directly to physical addresses, but Each entry points to the beginning of the page table associated with that segment.The logical address consists of a segment number and an offset; the system divides that offset by the page size to obtain the page number within the segment and the offset within the page.

The result is a very flexible model, which allows both coherent logical organization as an efficient physical allocation in fixed blocks. However, it has three clear drawbacks: the translation process becomes more complex and consumes more resources, more space is needed to store the multiple page tables (one per segment), and internal fragmentation can increase due to potentially partially occupied frames at the end of each segment.

Virtual memory and dynamic reallocation

All the methods discussed so far assumed, in one way or another, that a process had to be fully loaded into main memory in order to be executedVirtual memory breaks this limitation, allowing only an active part of the process to reside in RAM and the rest to be kept in secondary memory (e.g., on disk), being retrieved on demand.

Virtual memory is usually implemented starting from paging or segmentation schemesor combinations of both. When a process references an address that is not in main memory, a page fault occurs. The system locates the corresponding page on disk, reserves a free frame in RAM, and loads it. If no free frames are available, a replacement algorithm is used to choose which page to eject to disk.

Throughout this process, the The process that caused the page error is blockedwaiting for its page to be loaded into memory. The page table usually includes a presence bit that indicates whether each logical page of the process is currently in main memory or only on disk. This allows the hardware and operating system to quickly determine if a reference is valid and whether the page needs to be loaded.

This model allows the memory is used much betterThis is because it focuses on keeping only the actively used parts of processes in RAM. Furthermore, by being able to accommodate more processes simultaneously, it improves processor utilization. However, if the system experiences excessive page faults, disk accesses spike and performance degrades drastically, a phenomenon known as thrashing.

Swapping and its relationship to partitioning

In several of the memory management models described, especially in fixed partitions, variable partitions, and pagingIt is common to use swapping. This involves temporarily moving blocked processes to secondary storage, thus freeing up main memory for new processes or for existing ones to grow.

What is TWRP mode on Android?
Related article:
What is TWRP, how does it work, and what is it used for on Android?

The module responsible for this task, often called interchangeIt must decide which processes are swapped to disk and when they are brought back. It also manages the secondary memory area reserved for storing images of swapped processes. Combined with relocation techniques, it allows the same process to return to memory in a different partition or set of frames than the original, as long as the address translation remains consistent.

Internal memory reallocation in specific devices

All this theoretical framework also has a direct impact on consumer devices, such as mobile phones with MTK chipsets (MediaTek). Many of these devices organize the internal memory in logical partitions separate (for example, one for user data and another for applications), which sometimes leaves a partition practically unused if the user does not use it for its intended purpose.

In some chipset models and generations (for example, MTK657x vs MTK6582Users have sought methods to "merge" or resize these internal partitions in order to make better use of the total space. This reassignment involves modifying low-level partitioning tables and, in practice, behaves similarly to changing the partitioning scheme on a desktop system, with all the associated risks (data loss, boot problems, etc.).

The actual viability of these operations depends on how the manufacturer has defined the partition map and the tools available for the specific chipset. Although tutorials and specific utilities exist for some MTK series, it is not always possible to apply the same methods to later generations, and extreme caution is advised because this is not a mere high-level operation, but a profound modification of the internal memory structure.

Good memory management practices

Although most memory management details are handled automatically and transparently by the operating system, users and administrators can take some action. simple decisions to improve performance of the system. The first, and most obvious, is to have a sufficient amount of RAM installed: the minimums recommended by manufacturers are usually very conservative.

Beyond the hardware, it's important Avoid running more programs than strictly necessary. At the same time. Having multiple applications open that are not actually being used only serves to occupy memory and force the system to resort more frequently to swapping techniques or virtual memory, multiplying disk accesses.

We also need to be vigilant graphic accessories and embellishments These include animations, desktop gadgets, small background services, and browser extensions that, while seemingly harmless, consume RAM and CPU cycles. On machines with limited resources, these types of elements should be the first to be disabled if a significant performance drop is observed.

Overall, understanding how it works partitioning and reallocation of internal memory It helps to better interpret the behavior of our systems, to choose more appropriate configurations, and to assess when it is worthwhile to invest in more physical memory, adjust system parameters, or rethink how we organize the processes and services we keep running. Share the information so that more users can learn about the topic.


restore Android version
You might be interested in:
How to restore to a previous version of Android
Add as preferred source