`
Process by which OS efficiently handles a computer’s memory hierarchy to allocate space to processes and optimize performance.
Logical addresses are generated by CPU and are used by the OS to reference memory locations. Physical addresses are actual locations in memory. The OS uses the MMU to map logical addresses to physical addresses.
Binding maps logical addresses to physical addresses.
Memory is allocated in contiguous blocks, which is simple but leads to fragmentation.
Processes are divided and loaded into memory blocks that don’t have to be contiguous / adjacent. This mostly eliminates fragmentation issues.
Components of Paging:
Contents of a Page Table Entry:
Components of Segmentation:
Feature | Paging | Segmentation |
---|---|---|
Division | Fixed-size pages and frames. | Variable-sized segments. |
Addressing | Page number + Offset. | Segment number + Offset. |
Table Used | Page Table. | Segment Table. |
Fragmentation | Internal fragmentation (unused space in pages). | External fragmentation (gaps between segments). |
Use Case | OS and hardware memory management. | Reflects logical program structure. |
Flexibility | Less flexible; fixed sizes. | More flexible; aligns with logical structure. |
Virtual memory is a memory management technique that creates an illusion of a larger memory space by using a combination of physical memory (RAM) and disk space. It allows processes to use more memory than is physically available.
Pages are loaded into memory only when required. If a program accesses a page that is not in memory, a page fault occurs.
The OS pauses the process, loads the required page from the disk into memory, updates the page table, and resumes the process.
When memory is full, the OS must decide which page to replace.
Occurs when the system spends more time handling page faults than executing processes! Can be caused due to insufficient memory, poor page replacement algorithms, or excessive multitasking.
Tracks the set of pages actively used by a process over a fixed time window, and ensures processes get enough memory to avoid thrashing.
They are pre-allocated blocks of memory reserved for specific types of objects. This technique is used to speed up memory allocation and deallocation by reusing blocks.
How it works:
A process is temporarily moved from RAM to disk (swap space) to free memory for other processes. When needed back, the process is swapped back into memory. OS selects a process for swapping which is IDLE or low priority. Saves the process’s state and loads the required memory pages. Swap space is a reserved area on the disk for swapped out processes.
Spooling (Simultaneous Peripheral Operations On-Line) is a technique where data is temporarily held to be used and executed by a device, program, or system. It is commonly used for managing data for printers and other peripheral devices.
How it works:
Benefits:
Occurs when memory is divided into fixed-size blocks and a process is allocated a block larger than its size. The unused space within the block is wasted. Eg. A process needs 7KB but is allocated to a block of 25KB.
Solution would be to use smaller block sizes (e.g. paging) or use dynamic partitioning.
When free memory is divided into non-contiguous blocks, making it unusable for large processes that exceed the size of the largest free block.
Solution would be compaction when we rearrange memory to merge free spaces or non-contiguous memory allocation like using paging or segmentation to break processes into smaller units.
Used to reduce external fragmentation by moving allocated memory blocks together and leaving a large block of free memory. This helps in allocating larger contiguous memory blocks.
How it works:
Role of TLB in Virtual Memory:
TLB is a small, fast cache in the CPU that stores recently used mappings of logical addresses to physical addresses. Without the TLB, every memory access would require a page table lookup in RAM, which is comparatively slower. It is about 100 times faster than RAM. TLB speeds up address translation by quickly providing the physical frame for a given page.
A TLB Hit:
A TLB Miss:
Page Fault Rate: The fraction of memory accesses that result in a page fault. Lower is better. A lower rate means that most pages requested are in memory, while a higher rate indicates poor performance.
Page Fault Rate = (Number of Page Faults) / (Number of Memory Accesses)
Access Time in Paging and TLB: The effective memory access time depends on TLB performance.
Kernel manages memory differently than user processes, as it requires higher performance and needs to allocate/deallocate memory frequently for small objects like PCBs.
A technique used to manage memory for objects of the same size.
How it works:
Benefits:
In this, the memory is divided into blocks of sizes that are in power 2. For eg, 1KB, 2KB, 4KB, 8KB…
How it works:
This causes fast allocation and deallocation and merging the buddies reduces the external fragmentation. Internal fragmentation can still occur when block size exceeds process needs.
A memory optimization technique used when a process creates a copy of itself, for eg. fork.
How it works:
This reduces memory usage for processes that mostly read shared data and speeds up process creation.
This uses page sharing where pages are shared until a write occurs, and dirty bit which indicates a page has been modified and requires copying.
NUMA is a memory architecture used in multi-processor systems where memory access time depends on the memory location relative to the processor.
How it works:
Memory-mapped files allow files to be accessed as if they were part of the main memory, providing a convenient way to perform file I/O.
How it works:
Memory leaks occur when a program allocates memory but fails to deallocate it after use, leading to wasted memory resources.
Causes:
Checkpointing is a technique used to save the state of the system or process to disk. This allows recovery in case of a failure or crash.
Is used to sort datasets that are too large to fit into memory, relying on external storage like disk.
When is it needed?
Process:
Sorting Phase: The data is divided into manageable chunks that fit into memory. Each chuck is sorted in memory using an internal sorting alogrithm like quicksort or mergesort. Sorted chunks are also known as runs.
Merging Phase: The sorted runs are merged together to produce the final sorted output. This is done by K-way merge where K is the number of runs being merged at a time.
For a small example in Java, we have 3 files.
MyBufferReader.java
ExternalSort.java
Test.java
The MyBufferReader
class is a custom buffer reader that reads integers from a temporary file. It implements the Comparable
interface to allow comparison between different buffer readers based on their current number.
import java.util.*;
import java.io.*;
public class MyBufferReader implements Comparable<MyBufferReader> {
private final File tempFile;
private Integer currentNumber;
public boolean hasNumber = false;
private BufferedReader br;
public MyBufferReader(File tempFile) throws IOException {
this.tempFile = tempFile;
br = new BufferedReader(new FileReader(this.tempFile));
reload();
}
private void reload() throws IOException {
String s = br.readLine();
if (s == null) {
hasNumber = false;
currentNumber = null;
}
else {
currentNumber = Integer.parseInt(s);
hasNumber = true;
}
}
public void close() throws IOException {
this.br.close();
}
public int peek() {
return currentNumber;
}
public int pop() throws IOException {
int temp = peek();
reload();
return temp;
}
@Override
public int compareTo(MyBufferReader o)
{
return this.currentNumber - o.currentNumber;
}
}
MyBufferReader
’s current number.The ExternalSort
class handles the external sorting process. It reads the input file, sorts the data in batches, and merges the sorted batches into the final output file.
import java.io.*;
import java.util.*;
public class ExternalSort
{
File inputFile;
public ExternalSort(File inputFile) {
this.inputFile = inputFile;
}
public void sort() throws IOException {
List<File> tempFiles = createSortedTempFiles(10);
PriorityQueue<MyBufferReader> pq = new PriorityQueue<>();
for (File t : tempFiles) {
pq.add(new MyBufferReader(t));
}
File output = new File("./output.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(output));
while (!pq.isEmpty()) {
MyBufferReader m = pq.poll();
bw.write(m.pop() + "");
bw.newLine();
if (m.hasNumber) {
pq.add(m);
} else {
m.close();
}
}
bw.close();
}
public List<File> createSortedTempFiles(long batchSize) throws IOException {
List<File> tempFiles = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(inputFile));
String number;
List<Integer> numbers = new ArrayList<>();
while (( number = br.readLine()) != null ) {
numbers.add(Integer.parseInt(number));
if (numbers.size() >= batchSize) {
File tempFile = sortAndCreate(numbers);
tempFiles.add(tempFile);
numbers.clear();
}
}
if (!numbers.isEmpty()) {
File tempFile = sortAndCreate(numbers);
tempFiles.add(tempFile);
}
return tempFiles;
}
private File sortAndCreate(List<Integer> numbers) throws IOException {
Collections.sort(numbers);
String fileName = "./" + UUID.randomUUID() + ".txt";
File tempFile = new File(fileName);
tempFile.deleteOnExit();
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
for (int num : numbers) {
bw.write(num + "");
bw.newLine();
}
bw.close();
return tempFile;
}
}
The Test
class contains the main
method, which serves as the entry point for the program. It generates a random input file, initializes the ExternalSort
class, and starts the sorting process.
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
File f = new File("input.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter((f)));
Random r = new Random();
for (int i = 0; i < 200; i++) {
bw.write(r.nextInt(200) + "");
bw.newLine();
}
bw.close();
ExternalSort externalSort = new ExternalSort(f);
externalSort.sort();
}
}
ExternalSort
with the input file.sort()
method to perform the external sorting.