SlideShare a Scribd company logo
1 of 15
© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Memory Management
2© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
W's of Memory Management?
Memory Partitioning
Memory Organization
Memory Management in Linux
Swapping
3© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Why Memory Management?
Transition from Uni-process → Multi-process
Every process needs its own memory
Memory Division
Program loaded to “any” available memory address
Relocation
Independence / Non-interference between Processes
Protection
Communication between Processes
Sharing
Process Memory with Attributes (like read only for code)
Logical Organization
Executing Programs bigger than available Memory
Physical Organization (Overlaying & Reusing)
4© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What is Memory Management?
Key task: Division of Memory
Starting from the Division between
OS (Kernel)
Application (User)
Others follow
Relocation, Protection, Sharing
Logical & Physical Organization
May involve movement between primary &
secondary
5© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory Partitioning
Two traditional division mechanisms
Segmentation: Unequal sized
Examples: Linear Allocation, Buddy System
Paging: Equal sized
Examples: 4K-Paged Allocation
Examples in Linux
User-space malloc(): Segmentation
Slab sub-system: Paging
6© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Pros & Cons
Memory Fragmentation
External in Segmentation (gets worser)
Internal in Paging
Space Efficiency
Segmentation: More meta but lesser allocation wastage
Paging: Negligible meta but significant allocation wastage
Time Efficiency
Segmentation: Complex search & Storage Algorithm
Paging: Simple Algorithms
7© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory Organization
All other needs (Relocation, Protection, Sharing, ...)
Beautifully achieved by the concept of virtualization
That is organizing the memory into
Physical & Virtual Address Spaces
And then using the virtual addressing for all purposes
Both may use either Paging or Segmentation
Best to use similar mechanisms
Linux uses Paging for both with 4KiB/8KiB page size
Virtualization can be achieved
Using special hardware called MMU
Using software MMU
8© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
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 & controllers
Latest Intel, ARM, and most other architectures
have in-built MMU
Older x86, ARM7, and few such doesn't have a
hardware MMU
9© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory (RAM)
Working of an MMU
1
1
0
1
Valid Tag Frame Number
TLB (in Cache)
1
Page Table (in RAM)
1
1
1
0
1
0
1
1
0
1
Page Number [31:12] Offset [11:0] Virtual Address
10© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory Management in Linux
In Linux, all these combined
User Space & Kernel Space
Memory Page Partitioning
Physical & Virtual Address Spaces
Device Addresses, Mapping & Access
Takes the following form ...
11© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory Organization in Linux
An Example assuming 32-bit architecture
4GB Process
VA Space 1
User Space
4GB Process
VA Space N
.
.
.
3GB
Virtual
Address
for User
1GB VA
for Kernel
Kernel Space
(fixed by kernel)
Logical Address
(Non-swappable)
Virtual Address
(Swappable)
0x00000000
0xBFFFFFFF
0xFFFFFFFF
0xC0000000
Upto 3GB
Memory
(RAM)
Addressing
Upto 1GB Dev
Addressing
Physical Address Space
(fixed by architecture)
0x00000000
0xBFFFFFFF
0xC0000000
0xFFFFFFFF
Bus Address (incl.
both h/w memory &
registers)
Physical Address
(incl. only memory)
12© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Memory Manager
Provides Access Control to h/w & memory resources
Provides Dynamic Memory to kernel sub-system
Drivers
File Systems
Stacks
Provides 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
13© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Swapping
Optimizes primary & secondary memory usage
Primary memory pages swapped out
When running out of memory
Secondary memory pages swapped in
When needed back
Linux 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 Memory
Related commands: mkswap, swapon/off
14© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
W's of Memory Management?
Memory Partitioning
Segmentation & Paging
Memory Organization
Physical & Virtual Addressing
Working of an MMU
Memory Management in Linux
Swapping & Swap Partition
15© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

More Related Content

What's hot

Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory ManagementNi Zo-Ma
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionGene Chang
 
GPU Virtualization in Embedded Automotive Solutions
GPU Virtualization in Embedded Automotive SolutionsGPU Virtualization in Embedded Automotive Solutions
GPU Virtualization in Embedded Automotive SolutionsGlobalLogic Ukraine
 
Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structuresMukesh Chinta
 
Physical Memory Management.pdf
Physical Memory Management.pdfPhysical Memory Management.pdf
Physical Memory Management.pdfAdrian Huang
 
Linux memory-management-kamal
Linux memory-management-kamalLinux memory-management-kamal
Linux memory-management-kamalKamal Maiti
 
Distributed Shared Memory Systems
Distributed Shared Memory SystemsDistributed Shared Memory Systems
Distributed Shared Memory SystemsArush Nagpal
 
Process management in linux
Process management in linuxProcess management in linux
Process management in linuxMazenetsolution
 
Operating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresOperating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresVaibhav Khanna
 
Operating System-Memory Management
Operating System-Memory ManagementOperating System-Memory Management
Operating System-Memory ManagementAkmal Cikmat
 
Physical Memory Models.pdf
Physical Memory Models.pdfPhysical Memory Models.pdf
Physical Memory Models.pdfAdrian Huang
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPTQUONTRASOLUTIONS
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.Muhammad SiRaj Munir
 
Chapter 2 - Operating System Structures
Chapter 2 - Operating System StructuresChapter 2 - Operating System Structures
Chapter 2 - Operating System StructuresWayne Jones Jnr
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OSKumar Pritam
 

What's hot (20)

Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Linux file system
Linux file systemLinux file system
Linux file system
 
Windows 2019
Windows 2019Windows 2019
Windows 2019
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
GPU Virtualization in Embedded Automotive Solutions
GPU Virtualization in Embedded Automotive SolutionsGPU Virtualization in Embedded Automotive Solutions
GPU Virtualization in Embedded Automotive Solutions
 
Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structures
 
Physical Memory Management.pdf
Physical Memory Management.pdfPhysical Memory Management.pdf
Physical Memory Management.pdf
 
Linux memory-management-kamal
Linux memory-management-kamalLinux memory-management-kamal
Linux memory-management-kamal
 
6.distributed shared memory
6.distributed shared memory6.distributed shared memory
6.distributed shared memory
 
Distributed Shared Memory Systems
Distributed Shared Memory SystemsDistributed Shared Memory Systems
Distributed Shared Memory Systems
 
Process management in linux
Process management in linuxProcess management in linux
Process management in linux
 
Operating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresOperating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphores
 
Operating System-Memory Management
Operating System-Memory ManagementOperating System-Memory Management
Operating System-Memory Management
 
Linux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell ScriptingLinux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell Scripting
 
Physical Memory Models.pdf
Physical Memory Models.pdfPhysical Memory Models.pdf
Physical Memory Models.pdf
 
Os Threads
Os ThreadsOs Threads
Os Threads
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPT
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.
 
Chapter 2 - Operating System Structures
Chapter 2 - Operating System StructuresChapter 2 - Operating System Structures
Chapter 2 - Operating System Structures
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 

Similar to Linux Memory Management (20)

Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
File System Modules
File System ModulesFile System Modules
File System Modules
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
IT109 Microsoft Windows 7 Operating Systems Unit 01
IT109 Microsoft Windows 7 Operating Systems Unit 01IT109 Microsoft Windows 7 Operating Systems Unit 01
IT109 Microsoft Windows 7 Operating Systems Unit 01
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
 
File System Modules
File System ModulesFile System Modules
File System Modules
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Operating Systems
Operating SystemsOperating Systems
Operating Systems
 
Embedded Software Design
Embedded Software DesignEmbedded Software Design
Embedded Software Design
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
 
Oct2009
Oct2009Oct2009
Oct2009
 
Evolution of the Windows Kernel Architecture, by Dave Probert
Evolution of the Windows Kernel Architecture, by Dave ProbertEvolution of the Windows Kernel Architecture, by Dave Probert
Evolution of the Windows Kernel Architecture, by Dave Probert
 
2337610
23376102337610
2337610
 
Embedded Linux
Embedded LinuxEmbedded Linux
Embedded Linux
 
System Calls
System CallsSystem Calls
System Calls
 
Principles of operating system
Principles of operating systemPrinciples of operating system
Principles of operating system
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
 
Low-level Accesses
Low-level AccessesLow-level Accesses
Low-level Accesses
 

More from Anil Kumar Pugalia (20)

Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Processes
ProcessesProcesses
Processes
 
System Calls
System CallsSystem Calls
System Calls
 
Embedded C
Embedded CEmbedded C
Embedded C
 
Playing with R L C Circuits
Playing with R L C CircuitsPlaying with R L C Circuits
Playing with R L C Circuits
 
Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Mobile Hacking using Linux Drivers
Mobile Hacking using Linux DriversMobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
References
ReferencesReferences
References
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
Power of vi
Power of viPower of vi
Power of vi
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
"make" system
"make" system"make" system
"make" system
 
Hardware Design for Software Hackers
Hardware Design for Software HackersHardware Design for Software Hackers
Hardware Design for Software Hackers
 
RPM Building
RPM BuildingRPM Building
RPM Building
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
Timers
TimersTimers
Timers
 
Threads
ThreadsThreads
Threads
 

Recently uploaded

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Recently uploaded (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

Linux Memory Management

  • 1. © 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Linux Memory Management
  • 2. 2© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? W's of Memory Management? Memory Partitioning Memory Organization Memory Management in Linux Swapping
  • 3. 3© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Why Memory Management? Transition from Uni-process → Multi-process Every process needs its own memory Memory Division Program loaded to “any” available memory address Relocation Independence / Non-interference between Processes Protection Communication between Processes Sharing Process Memory with Attributes (like read only for code) Logical Organization Executing Programs bigger than available Memory Physical Organization (Overlaying & Reusing)
  • 4. 4© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What is Memory Management? Key task: Division of Memory Starting from the Division between OS (Kernel) Application (User) Others follow Relocation, Protection, Sharing Logical & Physical Organization May involve movement between primary & secondary
  • 5. 5© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Memory Partitioning Two traditional division mechanisms Segmentation: Unequal sized Examples: Linear Allocation, Buddy System Paging: Equal sized Examples: 4K-Paged Allocation Examples in Linux User-space malloc(): Segmentation Slab sub-system: Paging
  • 6. 6© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Pros & Cons Memory Fragmentation External in Segmentation (gets worser) Internal in Paging Space Efficiency Segmentation: More meta but lesser allocation wastage Paging: Negligible meta but significant allocation wastage Time Efficiency Segmentation: Complex search & Storage Algorithm Paging: Simple Algorithms
  • 7. 7© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Memory Organization All other needs (Relocation, Protection, Sharing, ...) Beautifully achieved by the concept of virtualization That is organizing the memory into Physical & Virtual Address Spaces And then using the virtual addressing for all purposes Both may use either Paging or Segmentation Best to use similar mechanisms Linux uses Paging for both with 4KiB/8KiB page size Virtualization can be achieved Using special hardware called MMU Using software MMU
  • 8. 8© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. 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 & controllers Latest Intel, ARM, and most other architectures have in-built MMU Older x86, ARM7, and few such doesn't have a hardware MMU
  • 9. 9© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Memory (RAM) Working of an MMU 1 1 0 1 Valid Tag Frame Number TLB (in Cache) 1 Page Table (in RAM) 1 1 1 0 1 0 1 1 0 1 Page Number [31:12] Offset [11:0] Virtual Address
  • 10. 10© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Memory Management in Linux In Linux, all these combined User Space & Kernel Space Memory Page Partitioning Physical & Virtual Address Spaces Device Addresses, Mapping & Access Takes the following form ...
  • 11. 11© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Memory Organization in Linux An Example assuming 32-bit architecture 4GB Process VA Space 1 User Space 4GB Process VA Space N . . . 3GB Virtual Address for User 1GB VA for Kernel Kernel Space (fixed by kernel) Logical Address (Non-swappable) Virtual Address (Swappable) 0x00000000 0xBFFFFFFF 0xFFFFFFFF 0xC0000000 Upto 3GB Memory (RAM) Addressing Upto 1GB Dev Addressing Physical Address Space (fixed by architecture) 0x00000000 0xBFFFFFFF 0xC0000000 0xFFFFFFFF Bus Address (incl. both h/w memory & registers) Physical Address (incl. only memory)
  • 12. 12© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Linux Memory Manager Provides Access Control to h/w & memory resources Provides Dynamic Memory to kernel sub-system Drivers File Systems Stacks Provides 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
  • 13. 13© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Swapping Optimizes primary & secondary memory usage Primary memory pages swapped out When running out of memory Secondary memory pages swapped in When needed back Linux 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 Memory Related commands: mkswap, swapon/off
  • 14. 14© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? W's of Memory Management? Memory Partitioning Segmentation & Paging Memory Organization Physical & Virtual Addressing Working of an MMU Memory Management in Linux Swapping & Swap Partition
  • 15. 15© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?