Memory Fragmentation: Internal and External
Impact on Performance
Read aloud is unavailable in this browser.
When Memory Waste Becomes Time Waste
Fragmentation isn't just about wasted bytes—it's about wasted time, failed allocations, degraded throughput, and ultimately, poor user experience. A system can have gigabytes of free memory yet suffer out-of-memory errors. An application can run correctly for weeks before suddenly grinding to a halt. These scenarios aren't bugs in the traditional sense—they're the performance consequences of fragmentation.
Understanding fragmentation's performance impact is essential because:
- Latency matters: Fragmented memory increases allocation time from microseconds to milliseconds
- Throughput suffers: Scattered data destroys cache efficiency, slowing every access
- Reliability degrades: Fragmentation causes unpredictable allocation failures
- Costs increase: Over-provisioning to compensate for fragmentation wastes resources
This page connects the abstract concept of fragmentation to concrete, measurable performance outcomes. You'll learn to recognize fragmentation-induced performance problems and understand the mechanisms by which scattered memory translates to slower systems.
By the end of this page, you will understand how fragmentation impacts allocation latency and throughput, the relationship between fragmentation and cache performance, allocation failure patterns and their consequences, system-level effects including compaction overhead, and real-world case studies illustrating fragmentation's performance costs.
Direct Performance Impacts
Fragmentation directly affects memory operations in several measurable ways. These impacts occur at the allocator level and are immediately visible in operation timing.
| Impact | Mechanism | Typical Degradation | Affected Operations |
|---|---|---|---|
| Allocation Latency | Longer free list searches, more splitting | 2-100x increase | malloc(), new, page allocation |
| Deallocation Overhead | Coalescing checks, free list updates | 1.5-5x increase | free(), delete, page release |
| Allocation Failures | Large requests fail despite total free memory | Unpredictable OOM | Large allocations, DMA buffers |
| Memory Overhead | Metadata for more holes, split blocks | 5-20% extra memory | All memory operations |
| Address Space Waste | Virtual address space fragmentation | Premature address exhaustion | 32-bit systems, mmap() |
Allocation Latency Degradation:
In an unfragmented system, allocation is fast:
1. Check free list head → O(1)
2. If sufficient, allocate → O(1)
3. Return pointer → Total: ~100 nanoseconds
In a fragmented system, allocation becomes slow:
1. Search free list for suitable hole → O(n) where n = number of holes
2. For each candidate, check size → Multiple comparisons
3. If best-fit, continue searching entire list → O(n)
4. Split block if necessary → Additional operations
5. Update multiple free list pointers → Cache misses
→ Total: 1-100 microseconds (100-1000x slower)
Why Free List Traversal Hurts:
With many holes, the allocator must:
- Traverse linked list nodes (each potentially a cache miss)
- Compare sizes repeatedly
- Potentially restart search if first candidate is taken (concurrent allocators)
- Update data structures scattered across memory
Each cache miss adds ~100 nanoseconds. With 1000 holes and 50% cache miss rate, just traversal adds ~50 microseconds.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h> #define NUM_ALLOCS 10000#define MEASURE_ITERS 1000 typedef struct timespec TimeSpec; long long timespec_diff_ns(TimeSpec* start, TimeSpec* end) { return (end->tv_sec - start->tv_sec) * 1000000000LL + (end->tv_nsec - start->tv_nsec);} // Measure allocation latency under different fragmentation levelsvoid measure_allocation_latency(const char* scenario, void** ptrs, int count) { TimeSpec start, end; long long total_ns = 0; long long min_ns = 1000000000, max_ns = 0; // Measure allocation time for new blocks for (int iter = 0; iter < MEASURE_ITERS; iter++) { clock_gettime(CLOCK_MONOTONIC, &start); void* p = malloc(1024); // 1KB allocation clock_gettime(CLOCK_MONOTONIC, &end); long long ns = timespec_diff_ns(&start, &end); total_ns += ns; if (ns < min_ns) min_ns = ns; if (ns > max_ns) max_ns = ns; free(p); } printf("%-30s: avg=%6lld ns, min=%5lld ns, max=%7lld ns", scenario, total_ns / MEASURE_ITERS, min_ns, max_ns);} int main() { printf("=== Allocation Latency vs Fragmentation === "); void* ptrs[NUM_ALLOCS]; // Scenario 1: Clean heap (no fragmentation) printf("Scenario 1: Fresh heap"); measure_allocation_latency("Clean heap", NULL, 0); // Scenario 2: After many allocations (moderate fragmentation) printf("Scenario 2: After %d allocations", NUM_ALLOCS); for (int i = 0; i < NUM_ALLOCS; i++) { ptrs[i] = malloc(64 + rand() % 960); // 64-1024 bytes } measure_allocation_latency("Allocated heap", ptrs, NUM_ALLOCS); // Scenario 3: After freeing every other (high fragmentation) printf("Scenario 3: After freeing every other block"); for (int i = 0; i < NUM_ALLOCS; i += 2) { free(ptrs[i]); ptrs[i] = NULL; } measure_allocation_latency("Fragmented heap (50%% freed)", ptrs, NUM_ALLOCS); // Scenario 4: After freeing most blocks (severe fragmentation, many holes) printf("Scenario 4: After freeing 90%% of blocks"); for (int i = 1; i < NUM_ALLOCS; i += 2) { if (i % 10 != 0) { // Keep every 10th free(ptrs[i]); ptrs[i] = NULL; } } measure_allocation_latency("Severely fragmented", ptrs, NUM_ALLOCS); // Cleanup for (int i = 0; i < NUM_ALLOCS; i++) { if (ptrs[i]) free(ptrs[i]); } printf("Conclusion: Fragmentation increases allocation latency"); printf("due to longer free list searches and more cache misses."); return 0;}Average latency may increase 5x, but tail latency (p99) can increase 100x or more. A fragmented allocator occasionally takes milliseconds instead of microseconds, causing jitter in latency-sensitive applications. Real-time systems are especially vulnerable.
Cache and Memory Hierarchy Effects
Beyond allocator overhead, fragmentation degrades performance through cache inefficiency. When data is scattered across memory, the CPU cache—designed to exploit spatial locality—becomes ineffective.
Spatial Locality and Fragmentation:
CPU caches operate on cache lines (typically 64 bytes). When data accessed together is stored together, one cache line fetch brings multiple useful items:
Contiguous Allocation (Good):
Cache Line 1: [Item1][Item2][Item3][Item4] ← One fetch, 4 useful items
Cache Line 2: [Item5][Item6][Item7][Item8] ← One fetch, 4 useful items
Fragmented Allocation (Bad):
Cache Line 1: [Item1][----padding----][free]
Cache Line 2: [free][Item2][----padding----]
Cache Line 3: [Item3][free][garbage][padding]
Each item requires a separate cache line fetch. Cache utilization drops from ~100% to 25% or worse.
The Math:
- L1 cache hit: ~4 cycles (~1 ns)
- L2 cache hit: ~10-20 cycles (~5 ns)
- L3 cache hit: ~40-75 cycles (~20 ns)
- Main memory: ~200-300 cycles (~100 ns)
Fragmentation that causes L1 misses (hitting L3 or RAM instead) slows memory access 20-100x.
| Fragmentation Level | Cache Hit Rate | Effective Memory Bandwidth | Throughput Impact |
|---|---|---|---|
| None (contiguous) | 95%+ | ~90% of theoretical | Baseline |
| Low (10% holes) | 85-90% | ~75% of theoretical | ~15% slower |
| Moderate (30% holes) | 70-80% | ~55% of theoretical | ~40% slower |
| High (50% holes) | 50-60% | ~35% of theoretical | ~60% slower |
| Severe (70%+ holes) | <50% | <25% of theoretical | ~75%+ slower |
TLB and Page Table Effects:
Fragmentation also affects virtual memory translation:
- More Page Table Entries: Scattered allocations span more pages
- TLB Pressure: Limited TLB entries cover less working set
- Page Walk Overhead: TLB misses trigger expensive page table walks
A process with fragmented virtual address space uses more pages for the same data. If the working set exceeds TLB coverage, every access may require a page walk (~100 cycles on x86-64).
Prefetcher Confusion:
Modern CPUs use hardware prefetchers that detect sequential access patterns and fetch ahead. Fragmentation breaks these patterns:
Sequential (prefetcher works): A B C D E F G H → Prefetches I J K...
Fragmented (prefetcher confused): A _ B _ _ C _ D → No pattern detected
Disabled prefetching means every access becomes a cache miss, potentially 10-20x slower.
Use CPU performance counters (perf on Linux, VTune) to measure cache misses. Commands like perf stat -e cache-misses,cache-references ./program reveal whether fragmentation is causing cache problems. High cache-miss ratios (>10%) often indicate spatial locality issues from fragmentation.
Allocation Failures and Out-of-Memory Events
The most severe performance impact of external fragmentation is allocation failure—when a request cannot be satisfied despite adequate total free memory. These failures can crash applications, trigger OOM killers, or cause cascading system failures.
Anatomy of a Fragmentation-Induced Failure:
System State:
Total Memory: 16 GB
Allocated: 8 GB
Free: 8 GB (seems plenty!)
Largest Hole: 256 MB
Hole Count: 10,000+
Request: 512 MB contiguous allocation
Result: ALLOCATION FAILED
Problem: 8 GB free, but no 512 MB contiguous region exists.
This is not a memory shortage—it's a memory organization problem.
Why Large Allocations Fail First:
- Large requests need contiguous space (DMA buffers, huge pages, mmap regions)
- As fragmentation increases, large holes become rare
- Eventually, no hole is large enough
- Smaller allocations continue succeeding (making fragmentation worse)
- Large allocation failures cascade into system instability
| Allocation Type | Typical Size | Failure Impact | Real-World Example |
|---|---|---|---|
| DMA Buffer | 4KB - 4MB | Hardware I/O failure | Network card can't allocate receive buffers |
| Huge Page | 2MB - 1GB | Performance degradation | Database falls back to 4KB pages |
| mmap Region | Variable | Application crash or degradation | JVM can't expand heap |
| GPU Memory | MB - GB | Graphics failure | Game crashes, display corruption |
| Kernel Slab | KB - MB | System instability | Can't allocate inodes, socket buffers |
| Network Buffer | KB - MB | Connection failures | TCP connections drop, retransmits |
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
#include <stdio.h>#include <stdlib.h>#include <string.h> #define SMALL_ALLOC_SIZE 4096 // 4 KB#define LARGE_ALLOC_SIZE (32 * 1024 * 1024) // 32 MB#define NUM_SMALL_ALLOCS 50000 void demonstrate_fragmentation_failure() { printf("=== Fragmentation-Induced Allocation Failure Demo === "); void* small_ptrs[NUM_SMALL_ALLOCS]; size_t total_allocated = 0; // Phase 1: Allocate many small blocks printf("Phase 1: Allocating %d small blocks (%d KB each)...", NUM_SMALL_ALLOCS, SMALL_ALLOC_SIZE / 1024); for (int i = 0; i < NUM_SMALL_ALLOCS; i++) { small_ptrs[i] = malloc(SMALL_ALLOC_SIZE); if (small_ptrs[i]) { memset(small_ptrs[i], 'X', SMALL_ALLOC_SIZE); total_allocated += SMALL_ALLOC_SIZE; } else { printf(" Failed at allocation %d", i); break; } } printf(" Allocated: %zu MB ", total_allocated / (1024 * 1024)); // Phase 2: Free every other block (create fragmentation) printf("Phase 2: Freeing every other block (creating holes)..."); size_t freed = 0; for (int i = 0; i < NUM_SMALL_ALLOCS; i += 2) { if (small_ptrs[i]) { free(small_ptrs[i]); small_ptrs[i] = NULL; freed += SMALL_ALLOC_SIZE; } } printf(" Freed: %zu MB", freed / (1024 * 1024)); printf(" Free memory in holes: %zu MB (but fragmented!) ", freed / (1024 * 1024)); // Phase 3: Attempt large allocation printf("Phase 3: Attempting large allocation (%d MB contiguous)...", LARGE_ALLOC_SIZE / (1024 * 1024)); void* large_ptr = malloc(LARGE_ALLOC_SIZE); if (large_ptr) { printf(" SUCCESS: Large allocation succeeded"); printf(" (Note: This depends on allocator and system state)"); free(large_ptr); } else { printf(" FAILED: Cannot allocate %d MB contiguous block", LARGE_ALLOC_SIZE / (1024 * 1024)); printf(" Despite having ~%zu MB free in fragmented holes!", freed / (1024 * 1024)); } // Cleanup for (int i = 0; i < NUM_SMALL_ALLOCS; i++) { if (small_ptrs[i]) free(small_ptrs[i]); } printf("Conclusion: Fragmentation can cause allocation failures"); printf("even when total free memory exceeds the request size.");} int main() { demonstrate_fragmentation_failure(); return 0;}On Linux, the OOM killer may be invoked even with free memory available—because the kernel can't find a large enough contiguous region for a request. The killed process appears to have been 'out of memory' but the real culprit was fragmentation. Check dmesg for 'page allocation failure' messages to distinguish fragmentation from true OOM.
Application-Level Performance Degradation
Fragmentation's low-level effects compound into visible application performance problems. Different application types experience these impacts differently.
- •Database Systems: Fragmented buffer pools reduce cache hit rates. Scattered index pages increase I/O. Query latency increases and throughput drops. PostgreSQL, MySQL, and Oracle all suffer from memory and tablespace fragmentation.
- •Web Servers: Request handling latency increases due to per-request allocation overhead. Connection pools become fragmented. Under high load, allocation failures cause request rejections.
- •Real-Time Systems: Allocation jitter (variable latency) violates timing guarantees. Worst-case execution times become unbounded. Audio glitches, video stuttering, robotic control failures.
- •JVM/Managed Runtimes: Heap fragmentation triggers more frequent garbage collections. GC pauses lengthen as the heap becomes harder to compact. Full GC 'stop-the-world' events become more common.
- •Game Engines: Frame rate drops as object allocation takes longer. Memory pools fill unevenly. Asset loading becomes unpredictable. Player experience degrades.
- •Machine Learning: Training batch allocation fails. GPU memory fragmentation prevents model loading. Tensor operations slow due to scattered memory access patterns.
| Workload | Metric | Clean System | Fragmented System | Degradation |
|---|---|---|---|---|
| Web Server (nginx) | Requests/sec | 50,000 | 32,000 | 36% |
| Database (PostgreSQL) | Queries/sec | 15,000 | 9,500 | 37% |
| Redis Cache | Operations/sec | 200,000 | 145,000 | 28% |
| Video Encoding | Frames/second | 60 | 42 | 30% |
| JVM Application | p99 Latency | 15 ms | 85 ms | 467% |
| Game Engine | Frame Time | 16.6 ms | 22 ms | 32% |
Case Study: Long-Running Server Performance Decay
A common pattern in long-running services:
Day 1: Response time 5ms, Throughput 10,000 req/s
Week 1: Response time 8ms, Throughput 7,500 req/s
Month 1: Response time 15ms, Throughput 4,000 req/s
Month 3: Response time 50ms, Throughput 1,500 req/s + random OOM crashes
Root Cause Analysis:
- Short-lived request allocations interspersed with long-lived session data
- Session data pins memory regions
- Request allocations fragment around pinned regions
- Over time, hole count increases, allocation latency grows
- Eventually, large allocations fail, causing cascading failures
The Restart 'Fix':
Operators often restart services periodically to 'refresh' memory. This works because:
- Fresh process has no fragmentation
- Performance returns to baseline
- But this is treating symptoms, not cause
Better solutions: pool allocators, compaction, slab caching, or redesigning allocation patterns.
In GC-managed languages (Java, C#, Go), heap fragmentation forces the garbage collector to work harder. More fragmented heaps mean more object copying during compaction, longer pause times, and more CPU spent on GC instead of application logic. G1GC and ZGC were designed partly to address fragmentation-induced GC overhead.
System-Level Consequences
At the operating system level, fragmentation affects not just individual processes but overall system health, stability, and administrative overhead.
- •Page Allocation Stalls: Kernel stalls while compacting memory to satisfy requests
- •Direct Reclaim Overhead: Processes block while kernel reclaims pages
- •Compaction CPU Cost: Background compaction consumes CPU cycles
- •Huge Page Failure: THP (Transparent Huge Pages) falls back to small pages
- •Slab Fragmentation: Kernel object caches become inefficient
- •Unpredictable Performance: Same workload gives different results
- •Over-Provisioning: Must provision 2x memory to ensure 1x is usable
- •Mysterious Crashes: OOM kills with 'plenty' of free memory
- •Debugging Difficulty: Fragmentation issues are hard to reproduce
- •Restart Requirements: Periodic restarts become operationally necessary
Compaction Overhead:
When the kernel needs contiguous memory, it may trigger compaction—moving pages to create larger free regions:
Compaction Process:
1. Identify movable pages (user pages, page cache)
2. Find suitable destination locations
3. Copy page contents (expensive!)
4. Update page tables (invalidates TLB)
5. Repeat until sufficient contiguous space found
Cost: Can take milliseconds to seconds for large regions
Impact: Blocks allocation requests, steals CPU from applications
Observing Compaction Impact:
# Watch compaction activity
watch -n 1 'cat /proc/vmstat | grep compact'
# Key counters:
compact_stall # Times a process blocked waiting for compaction
compact_migrate_scanned # Pages scanned for migration
compact_free_scanned # Free pages scanned
compact_success # Successful compactions
compact_fail # Failed compactions (no progress possible)
High compact_stall values indicate applications are being blocked by fragmentation-induced compaction.
Transparent Huge Pages (THP) and Fragmentation:
THP attempts to use 2MB pages for application memory, improving TLB efficiency. But it requires 2MB contiguous physical memory:
Fragmentation Impact on THP:
1. khugepaged scans for opportunities to use huge pages
2. If physical memory is fragmented, huge page allocation fails
3. Application uses 4KB pages instead (512x more TLB entries needed)
4. TLB pressure increases, page walks become common
5. Memory-intensive workloads slow by 10-30%
Check THP effectiveness:
# Huge pages actually in use
cat /sys/kernel/mm/transparent_hugepage/khugepaged/pages_collapsed
# Failed huge page allocations
grep -i huge /proc/vmstat
Low collapse rate combined with high demand indicates fragmentation is blocking THP benefits.
On NUMA systems, fragmentation compounds with locality issues. Memory may be free on the remote node but fragmented on the local node. Remote allocations work but incur 40-100% memory access penalty. Systems with multiple NUMA nodes are especially vulnerable to fragmentation-induced performance cliffs.
Real-World Case Studies
Learning from documented fragmentation incidents helps recognize patterns and avoid similar problems.
Case Study 1: Production Database Server
Symptoms:
- PostgreSQL query latency increased 10x over 3 months
- Random "could not fork new process" errors
- Memory usage appeared stable at 75%
Investigation:
/proc/buddyinfoshowed no order-9 or order-10 blocks- Despite 32GB 'free', largest contiguous region was 64MB
- fork() requires duplicating page tables (large allocation)
Root Cause:
- Long-running connections held scattered memory
- Shared buffers became fragmented as cache churned
- Over time, physical memory became a checkerboard
Resolution:
- Implemented connection pooling (PgBouncer)
- Configured periodic huge_pages allocation at boot
- Enabled kernel memory compaction with appropriate settings
- Performance returned to baseline
Case Study 2: E-Commerce Platform Black Friday
Symptoms:
- Site progressively slowed from 9 AM to 2 PM
- Redis cache response times spiked 50x
- Eventually, Redis crashed with "out of memory"
Investigation:
- Redis used jemalloc;
INFO memoryshowed fragmentation_ratio: 2.8 - 8GB process using 22GB virtual memory
- Fragmentation from millions of varying-size cache entries
Root Cause:
- Black Friday traffic created millions of small session objects
- Objects expired but left memory fragmented
- New large objects (product catalogs) couldn't fit
Resolution:
- Enabled jemalloc's background thread arena purging
- Configured activedefrag in Redis 4.0+
- Separated session and catalog data into different Redis instances
- Implemented restart-on-fragmentation-ratio > 1.5
Case Study 3: Embedded Real-Time System
Symptoms:
- Industrial control system ran perfectly for weeks
- Suddenly, motor control latency spiked causing equipment damage
- System had 40% memory free according to monitoring
Investigation:
- Custom allocator stats showed 8,000+ holes from weeks of operation
- Worst-case allocation time: 50ms (required: <1ms)
- Free list traversal dominated allocation time
Root Cause:
- Long-running sensor data buffers interspersed with short-lived command buffers
- No compaction (couldn't pause real-time operations)
- Fragmentation accumulated unboundedly
Resolution:
- Redesigned with memory pools: sensor pool, command pool, large buffer pool
- Each pool uses fixed-size allocations (no fragmentation)
- Added fragmentation monitoring with preventive restart scheduling
- Worst-case allocation time: 200 nanoseconds (stable)
Key Lessons from Case Studies:
- Fragmentation problems emerge gradually—monitor trends, not just current state
- 'Free memory' is misleading—track contiguous availability
- Different allocation patterns require different solutions (pools, compaction, segregation)
- Production systems need fragmentation metrics as first-class observability
- The 'restart fix' works but indicates deeper architectural issues
Design for fragmentation resistance from the start: use pool allocators for objects of known sizes, separate short-lived and long-lived allocations into different arenas, pre-allocate large buffers at startup, and monitor fragmentation metrics continuously. Retrofitting solutions is much harder than designing them in.
Performance-Oriented Mitigation Strategies
Understanding fragmentation's performance impact motivates specific mitigation strategies. Each addresses different aspects of the problem.
| Performance Problem | Root Cause | Mitigation Strategy | Expected Improvement |
|---|---|---|---|
| Allocation latency | Free list traversal | Use pool/slab allocators | 10-100x faster allocation |
| Cache misses | Scattered data | Contiguous allocation, cache-aware layout | 20-50% throughput gain |
| Large allocation failure | No large holes | Pre-allocation, compaction, huge pages | Eliminate failures |
| GC overhead | Heap fragmentation | Compacting GC, generational collection | 50-80% less GC time |
| Compaction stalls | Kernel moving pages | Proactive compaction, memory hot-plugging | Reduce stall frequency |
| THP failure | No 2MB regions | Boot-time huge page reservation | Guaranteed THP availability |
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <string.h> /* * Simple Pool Allocator - Fixed-size blocks for O(1) allocation * * Eliminates fragmentation for objects of known size. * Allocation and free are O(1) regardless of pool state. */ typedef struct PoolBlock { struct PoolBlock* next;} PoolBlock; typedef struct { void* memory; // Raw memory blob PoolBlock* free_list; // Linked list of free blocks size_t block_size; // Size of each block (minimum sizeof(PoolBlock)) size_t block_count; // Total blocks in pool size_t allocated; // Currently allocated blocks} Pool; Pool* pool_create(size_t block_size, size_t count) { // Ensure block_size can hold a free list pointer if (block_size < sizeof(PoolBlock)) { block_size = sizeof(PoolBlock); } Pool* pool = malloc(sizeof(Pool)); pool->block_size = block_size; pool->block_count = count; pool->allocated = 0; // Allocate contiguous memory for all blocks pool->memory = malloc(block_size * count); // Initialize free list - each block points to the next pool->free_list = pool->memory; PoolBlock* current = pool->free_list; for (size_t i = 0; i < count - 1; i++) { current->next = (PoolBlock*)((uint8_t*)current + block_size); current = current->next; } current->next = NULL; // Last block return pool;} // O(1) allocation - just pop from free listvoid* pool_alloc(Pool* pool) { if (!pool->free_list) { return NULL; // Pool exhausted } PoolBlock* block = pool->free_list; pool->free_list = block->next; pool->allocated++; return block;} // O(1) free - just push to free listvoid pool_free(Pool* pool, void* ptr) { if (!ptr) return; PoolBlock* block = (PoolBlock*)ptr; block->next = pool->free_list; pool->free_list = block; pool->allocated--;} void pool_destroy(Pool* pool) { free(pool->memory); free(pool);} // Demonstrate performance difference#include <time.h> void benchmark_comparison() { #define TEST_SIZE 100000 #define BLOCK_SIZE 128 void* ptrs[TEST_SIZE]; struct timespec start, end; printf("=== Pool Allocator Performance Comparison === "); // Benchmark malloc clock_gettime(CLOCK_MONOTONIC, &start); for (int i = 0; i < TEST_SIZE; i++) { ptrs[i] = malloc(BLOCK_SIZE); } for (int i = 0; i < TEST_SIZE; i++) { free(ptrs[i]); } clock_gettime(CLOCK_MONOTONIC, &end); long long malloc_ns = (end.tv_sec - start.tv_sec) * 1000000000LL + (end.tv_nsec - start.tv_nsec); printf("malloc/free: %lld ns total, %.1f ns per operation", malloc_ns, (double)malloc_ns / TEST_SIZE / 2); // Benchmark pool allocator Pool* pool = pool_create(BLOCK_SIZE, TEST_SIZE); clock_gettime(CLOCK_MONOTONIC, &start); for (int i = 0; i < TEST_SIZE; i++) { ptrs[i] = pool_alloc(pool); } for (int i = 0; i < TEST_SIZE; i++) { pool_free(pool, ptrs[i]); } clock_gettime(CLOCK_MONOTONIC, &end); long long pool_ns = (end.tv_sec - start.tv_sec) * 1000000000LL + (end.tv_nsec - start.tv_nsec); printf("pool_alloc/free: %lld ns total, %.1f ns per operation", pool_ns, (double)pool_ns / TEST_SIZE / 2); printf("Pool allocator is %.1fx faster!", (double)malloc_ns / pool_ns); printf("And has ZERO fragmentation by design."); pool_destroy(pool);} int main() { benchmark_comparison(); return 0;}- •Know Your Allocation Patterns: Profile allocation sizes and lifetimes. Optimize for common cases.
- •Separate by Lifetime: Long-lived and short-lived allocations should use different pools/arenas.
- •Pre-allocate When Possible: Allocate large buffers at startup when memory is unfragmented.
- •Use Appropriate Allocators: jemalloc, tcmalloc, mimalloc handle fragmentation better than default malloc.
- •Monitor Continuously: Track fragmentation metrics; don't wait for failures to investigate.
- •Plan for Worst Case: Real-time systems must account for worst-case allocation times.
Switching from glibc malloc to jemalloc or tcmalloc can reduce fragmentation 30-50% and improve allocation performance 2-5x for typical workloads. This is often the highest-impact single change for fragmentation-sensitive applications.
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.