Linux Memory Management


Published on

Published in: Technology
1 Comment
  • nice article sir..:)
    for those who want to read more they can see this article on memory management.
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Linux Memory Management

  1. 1. Linux Memory Management © 2010 Anil Kumar Pugalia <> All Rights Reserved.
  2. 2. What to Expect?Ws of Memory Management?Memory PartitioningMemory OrganizationMemory Management in LinuxSwapping © 2010 Anil Kumar Pugalia <> 2 All Rights Reserved.
  3. 3. Why Memory Management?Transition from Uni-process → Multi-processEvery process needs its own memory Memory DivisionProgram loaded to “any” available memory address RelocationIndependence / Non-interference between Processes ProtectionCommunication between Processes SharingProcess Memory with Attributes (like read only for code) Logical OrganizationExecuting Programs bigger than available Memory Physical Organization (Overlaying & Reusing) © 2010 Anil Kumar Pugalia <> 3 All Rights Reserved.
  4. 4. What is Memory Management?Key task: Division of MemoryStarting from the Division between OS (Kernel) Application (User)Others follow Relocation, Protection, Sharing Logical & Physical OrganizationMay involve movement between primary &secondary © 2010 Anil Kumar Pugalia <> 4 All Rights Reserved.
  5. 5. Memory PartitioningTwo traditional division mechanisms Segmentation: Unequal sized Examples: Linear Allocation, Buddy System Paging: Equal sized Examples: 4K-Paged AllocationExamples in Linux User-space malloc(): Segmentation Slab sub-system: Paging © 2010 Anil Kumar Pugalia <> 5 All Rights Reserved.
  6. 6. Pros & ConsMemory Fragmentation External in Segmentation (gets worser) Internal in PagingSpace Efficiency Segmentation: More meta but lesser allocation wastage Paging: Negligible meta but significant allocation wastageTime Efficiency Segmentation: Complex search & Storage Algorithm Paging: Simple Algorithms © 2010 Anil Kumar Pugalia <> 6 All Rights Reserved.
  7. 7. Memory OrganizationAll other needs (Relocation, Protection, Sharing, ...) Beautifully achieved by the concept of virtualizationThat is organizing the memory into Physical & Virtual Address Spaces And then using the virtual addressing for all purposesBoth may use either Paging or SegmentationBest to use similar mechanismsLinux uses Paging for both with 4Kbytes page sizeVirtualization can be achieved Using special hardware called MMU Using software MMU © 2010 Anil Kumar Pugalia <> 7 All Rights Reserved.
  8. 8. Memory Organization ...Linux Kernels (>= 2.5.46) support both Earlier kernels worked only with hardware MMU uClinux used to be the solution for non-MMU processors & controllersLatest Intel, ARM, and most otherarchitectures have in-built MMUOlder x86, ARM7, and few such doesnthave a hardware MMU © 2010 Anil Kumar Pugalia <> 8 All Rights Reserved.
  9. 9. Working of an MMU Page Number [31:12] Offset [11:0] Virtual AddressTLB (in Cache) Memory (RAM)1101Valid Tag Frame NumberPage Table (in RAM)11110101101 © 2010 Anil Kumar Pugalia <> 9 All Rights Reserved.
  10. 10. Memory Management in LinuxIn Linux, all these combined User Space & Kernel Space Memory Page Partitioning Physical & Virtual Address Spaces Device Addresses, Mapping & AccessTakes the following form ... © 2010 Anil Kumar Pugalia <> 10 All Rights Reserved.
  11. 11. Memory Organization in Linux An Example assuming 32-bit architecture 4GB Process0x00000000 0x00000000 VA Space 1 Upto 3GB Memory 3GB . (RAM) Physical Address Virtual Virtual Address . Addressing (incl. only memory) Address (Swappable) . for User 4GB Process0xBFFFFFFF 0xBFFFFFFF VA Space N0xC0000000 Bus Address (incl. 0xC0000000 Upto 1GB Dev both h/w memory 1GB VA Logical Address Addressing & registers) for Kernel (Non-swappable)0xFFFFFFFF 0xFFFFFFFF Physical Address Space Kernel Space User Space (fixed by architecture) (fixed by kernel) © 2010 Anil Kumar Pugalia <> 11 All Rights Reserved.
  12. 12. Linux Memory ManagerProvides Access Control to h/w & memory resourcesProvides Dynamic Memory to kernel sub-system Drivers File Systems StacksProvides Virtual Memory to Kernel & User space Kernel & User Processes run in their own virtual address spaces Providing the various features of a Linux system System reliability, Security Communication Program Execution Support © 2010 Anil Kumar Pugalia <> 12 All Rights Reserved.
  13. 13. SwappingOptimizes primary & secondary memory usagePrimary memory pages swapped out When running out of memorySecondary memory pages swapped in When needed backLinux maintains Page Cache in Primary Memory for Code: Reloadable Data: Unchanged vs Changed Changed Data: Temporary (Dynamic) vs Persistent (Files)Linux uses Swap (raw) partition on Secondary MemoryRelated commands: mkswap, swapon/off © 2010 Anil Kumar Pugalia <> 13 All Rights Reserved.
  14. 14. What all have we learnt?Ws of Memory Management?Memory Partitioning Segmentation & PagingMemory Organization Physical & Virtual Addressing Working of an MMUMemory Management in LinuxSwapping & Swap Partition © 2010 Anil Kumar Pugalia <> 14 All Rights Reserved.
  15. 15. Any Queries?© 2010 Anil Kumar Pugalia <> 15 All Rights Reserved.