Course: From Basic Science to Web Hosting
Module 04 — Linux Fundamentals
How Linux Uses Memory
Difficulty: Beginner → Intermediate
Prerequisites: Lesson 019 — Linux Processes
Estimated time: 35–45 minutes
The next fundamental question is:
When Nginx, PHP, MySQL and WordPress are running, where do their instructions and data actually live?
The answer begins with memory.
1. The Basic Memory Chain
We previously learned:
Program
↓
Process
↓
CPU
But the CPU needs somewhere to obtain instructions and data.
So:
Storage
↓
RAM
↓
CPU
A simplified computer looks like:
CPU
↑↓
RAM
↑↓
Storage
2. Storage vs RAM
Consider your WordPress installation.
The files physically exist on persistent storage:
/storage/websites/templates.cresignsys.com/public/
But when WordPress is running, the CPU does not normally execute PHP directly from the SSD.
The operating system loads needed code/data into memory.
Conceptually:
SSD
↓
Filesystem
↓
RAM
↓
CPU
3. Why Not Run Everything Directly From SSD?
Storage is persistent, but RAM is designed for much faster access.
A simplified hierarchy is:
CPU registers
↓
CPU cache
↓
RAM
↓
SSD
↓
Long-term storage
Generally:
Higher
↑
Speed
Lower
↓
Capacity
There are many technical details, but this hierarchy is the important starting point.
4. What Is RAM?
RAM means:
Random Access Memory
It is the computer’s main working memory.
When programs are running, they need memory for:
Instructions
Variables
Buffers
Stacks
Heaps
Libraries
Operating-system data
5. A Simple Example
Suppose a program contains:
5
+
3
The program’s instructions are stored persistently somewhere.
When executing:
SSD
↓
RAM
↓
CPU
The CPU fetches instructions/data through the memory system.
6. RAM Is Volatile
Normal RAM is generally volatile.
If power disappears:
RAM
↓
contents lost
Storage such as SSD is non-volatile:
Power OFF
↓
Data remains on SSD
Therefore:
RAM
= working state
SSD
= persistent state
7. What Does a Process See?
A process doesn’t normally work directly with raw physical RAM addresses.
Instead, it receives a:
Virtual Address Space
Conceptually:
Process
↓
Virtual addresses
↓
Memory-management hardware + kernel
↓
Physical memory
This is one of the most important concepts in modern operating systems.
8. Why Virtual Memory?
Imagine two processes:
Nginx
PHP
Both could theoretically use a virtual address such as:
0x1000
But Linux can map those virtual addresses to different physical memory locations.
Conceptually:
Nginx
0x1000
↓
Physical RAM location A
PHP
0x1000
↓
Physical RAM location B
So processes can have isolated address spaces.
9. Memory Isolation
This helps prevent:
PHP
↓
directly modifying
↓
Nginx's memory
or:
WordPress
↓
directly modifying
↓
kernel memory
The CPU’s memory-management mechanisms enforce access restrictions.
10. Virtual Address Space
A simplified process memory layout might look like:
High addresses
┌──────────────────┐
│ Stack │
├──────────────────┤
│ │
│ ... │
│ │
├──────────────────┤
│ Heap │
├──────────────────┤
│ Data │
├──────────────────┤
│ Program code │
└──────────────────┘
Low addresses
This is a conceptual model; actual layouts vary by architecture, operating system, executable format, security features, and process.
11. Program Code
The executable instructions occupy memory mappings associated with the program.
Conceptually:
Program file
↓
Code mapping
↓
CPU executes instructions
For example:
Nginx executable
↓
memory mapping
↓
CPU
12. Data
Programs need storage for data.
For example:
configuration
global variables
program state
Conceptually:
Code
+
Data
↓
Running program
13. Stack
The stack is used for function-call-related data and automatic/local variables.
Imagine:
main()
↓
functionA()
↓
functionB()
The stack helps maintain information about these calls.
Conceptually:
Function B
↓
Function A
↓
main()
When a function returns, its stack frame can be removed.
14. Simple Stack Example
Imagine:
function add(a, b)
When called:
add(5, 3)
the execution may need memory for:
a
b
return information
temporary state
Some of this can be associated with stack frames, depending on the compiler and architecture.
15. Heap
The heap is used for dynamically allocated memory.
Conceptually:
Program
↓
"Give me more memory"
↓
Allocator
↓
Heap
For example, applications may dynamically allocate memory while processing requests.
Languages and runtimes manage this differently.
PHP has its own memory-management behavior on top of the operating system’s memory facilities.
16. Stack vs Heap
A simplified comparison:
| Stack | Heap |
|---|---|
| Function-call data | Dynamic allocations |
| Automatic lifetime | Flexible lifetime |
| Usually fast | More complex management |
| Limited per-thread region | Can grow substantially |
| Thread-specific | Process-wide in general |
Do not treat this table as an exact physical RAM layout; it is a conceptual model of process virtual memory.
17. Shared Libraries
Applications often use shared libraries.
For example:
Program
↓
Shared library
↓
Functions
Instead of every program containing its own copy of common libraries, the operating system can map shared library code into processes.
This can reduce duplication.
18. What Is a Page?
Virtual memory is commonly managed in units called:
Pages
Instead of managing every byte independently, memory systems manage chunks.
A common page size is:
4 KB
but other page sizes can exist.
Conceptually:
Virtual memory
├── Page
├── Page
├── Page
├── Page
└── ...
19. Page Tables
The CPU’s memory-management hardware uses structures called:
Page tables
They help translate:
Virtual address
↓
Physical address
Conceptually:
Process
↓
Virtual address
↓
Page table
↓
Physical RAM
The Linux kernel manages these mappings and the relevant memory structures.
20. TLB
Doing a page-table lookup for every memory access would be expensive.
Modern CPUs therefore use a cache called the:
TLB
Translation Lookaside Buffer.
It caches recent virtual-to-physical address translations.
Conceptually:
Virtual address
↓
TLB
├── hit → fast translation
└── miss → page-table lookup
This is a deeper CPU/memory concept, but it explains why virtual memory can be efficient.
21. What Happens When RAM Is Full?
Suppose your server has:
8 GB RAM
and running processes demand more memory.
Linux has several mechanisms for handling memory pressure.
One is:
Swap
22. What Is Swap?
Swap is storage space used as an extension of memory management.
Conceptually:
RAM
↓
memory pressure
↓
some pages may be moved to swap
↓
SSD
This allows the system to keep operating under some memory-pressure situations.
But:
Swap is not equivalent to RAM.
Storage is much slower than RAM.
23. Why Heavy Swap Is Bad
Imagine PHP-FPM repeatedly needs a memory page that has been moved to storage.
Then:
CPU
↓
needs memory
↓
page not in RAM
↓
storage access
↓
page brought back
↓
CPU continues
If this happens excessively, performance can collapse.
This behavior is often associated with:
Thrashing
24. Swap Is Not Always Bad
A common misconception is:
“Any swap usage means the server is broken.”
Not necessarily.
Linux may use swap for various reasons, and some inactive pages can be swapped out while plenty of useful RAM remains available.
The important question is:
Is the system experiencing harmful memory pressure?
25. OOM
If memory pressure becomes severe and the system cannot satisfy memory demands, Linux has an:
Out-Of-Memory mechanism
commonly called:
OOM
The kernel’s OOM killer may terminate selected processes to recover memory.
Conceptually:
Memory demand
↓
Available memory insufficient
↓
Severe pressure
↓
OOM handling
↓
Process may be terminated
26. Why This Matters to Web Hosting
Imagine:
Server RAM = 4 GB
and you run:
Nginx
PHP-FPM
MySQL
WordPress
Other services
Now PHP creates many workers.
Conceptually:
PHP-FPM
├── Worker 1 → 100 MB
├── Worker 2 → 100 MB
├── Worker 3 → 100 MB
├── Worker 4 → 100 MB
└── ...
Memory consumption can grow substantially.
27. PHP-FPM and Memory
Suppose each worker uses approximately:
100 MB
and there are:
20 workers
A rough upper-bound-style estimate would be:
20 × 100 MB
= 2000 MB
≈ 2 GB
This is only a simplified illustration. Real PHP-FPM memory behavior varies, and shared memory/code can complicate simple multiplication.
But the principle is important:
More concurrent workers can mean more memory consumption.
28. MySQL and Memory
MySQL also uses memory for:
Buffers
Caches
Connections
Temporary structures
Internal data
So:
RAM
├── Linux
├── Nginx
├── PHP-FPM
├── MySQL
└── Other services
All compete for available memory.
29. Linux Page Cache
Linux also uses otherwise-available RAM for filesystem caching.
Conceptually:
SSD
↓
File
↓
Linux page cache
↓
RAM
If a frequently accessed file is cached in RAM, future reads can be faster.
This means:
RAM shown as “used” is not necessarily a problem.
Some memory is being productively used for cache.
30. Free vs Available
When checking Linux memory, don’t look only at:
free
A more useful concept is:
available
because Linux can reclaim some caches when applications need memory.
Use:
free -h
You might see:
total used free shared buff/cache available
Mem: ... ... ... ... ... ...
Swap: ... ... ... ... ... ...
The exact values depend on your server.
31. free -h
The -h means human-readable.
Use:
free -h
This is one of the first commands to run when investigating memory problems.
32. top
You can also use:
top
Look at:
%Cpu
MiB Mem
MiB Swap
Then inspect which processes are consuming memory.
33. ps Memory Usage
You can sort processes by memory usage:
ps aux --sort=-%mem | head
This can help identify processes using the most memory.
For CPU:
ps aux --sort=-%cpu | head
34. Memory and WordPress
A WordPress request can involve:
Browser
↓
Nginx
↓
PHP-FPM
↓
WordPress
↓
Plugins
↓
Theme
↓
MySQL
Each layer can consume resources.
Therefore a slow website might be caused by:
CPU
RAM
Database
PHP
Disk I/O
Network
External API
Plugin
Theme
Not necessarily Nginx.
35. PHP Memory Limit
PHP itself can impose memory limits.
For example, PHP may have a configuration value:
memory_limit
This limits memory available to a PHP request under PHP’s own memory-management rules.
It is different from:
Server RAM
For example:
Server RAM = 8 GB
PHP memory_limit = 256M
These are completely different concepts.
36. Server RAM vs PHP Memory Limit
Think:
Physical/virtual server
↓
8 GB RAM
PHP process/request
↓
PHP memory_limit
The PHP limit applies within PHP’s execution environment; it doesn’t mean the entire server has only that amount of memory.
37. MySQL Memory vs Server RAM
Similarly:
MySQL configuration
↓
buffers / caches / connections
↓
RAM
If MySQL is configured too aggressively for a small VPS, it can compete heavily with PHP and the operating system.
38. The Memory Flow
A simplified request:
Browser
↓
Nginx
↓
PHP-FPM
↓
WordPress
↓
MySQL
Memory involvement:
RAM
├── Nginx process memory
├── PHP-FPM worker memory
├── WordPress/PHP memory
├── MySQL memory
├── Linux kernel memory
└── filesystem cache
39. Why More Websites Require More Planning
Suppose you host:
1 website
versus:
50 websites
Each additional site may add:
PHP workload
Database workload
Nginx configuration
Cache
Background jobs
Cron tasks
Traffic
Therefore hosting capacity is not determined by disk space alone.
You must consider:
CPU
RAM
Storage I/O
Network
Database workload
Concurrent requests
40. Memory and VPS Hosting
A virtual private server may give you a defined amount of virtual CPU and RAM.
Conceptually:
Physical server
├── VM A
├── VM B
├── VM C
└── VM D
Your VM sees its allocated resources according to the virtualization/cloud platform.
Inside your VM:
Ubuntu
↓
Linux
↓
Processes
↓
Memory management
41. The Deepest Connection
Remember the original chain:
Electron
↓
Transistor
↓
Logic
↓
CPU
Now:
CPU
↓
Memory-management hardware
↓
RAM
↓
Linux kernel
↓
Processes
↓
Applications
So even a simple command such as:
free -h
ultimately depends on:
Hardware
↓
Memory controller
↓
CPU
↓
Linux kernel
↓
/proc and memory-management interfaces
↓
free
↓
Terminal output
42. A Useful Mental Model
Think of RAM as a working area, not as permanent storage.
COMPUTER
│
┌──────────┴──────────┐
↓ ↓
Storage RAM
"Keep data" "Work on data"
│ │
└──────────┬──────────┘
↓
CPU
43. Practical Commands
Start learning these:
Memory summary
free -h
Live system view
top
Memory-heavy processes
ps aux --sort=-%mem | head
CPU-heavy processes
ps aux --sort=-%cpu | head
Kernel memory information
cat /proc/meminfo
System uptime/load
uptime
44. What Does uptime Tell You?
For example:
uptime
might show:
up 10 days, 3:15
load average: 0.20, 0.18, 0.15
This introduces another important concept:
Load Average
We will study it properly later.
It is related to how much work is runnable or waiting for certain resources over time.
It is not simply CPU percentage.
45. What You Should Remember
The key chain is:
Program
↓
Process
↓
Virtual memory
↓
Pages
↓
Physical RAM
↓
CPU
And for your server:
Nginx
↓
PHP-FPM
↓
WordPress
↓
MySQL
↓
RAM
All of these compete for system resources.
46. Quick Check
Is a program the same as a process?
No.
Program = stored code
Process = running instance
Is RAM persistent?
Normally no.
Is SSD persistent?
Yes, normally.
What is virtual memory?
An abstraction that gives processes virtual address spaces, with mappings managed by the OS and hardware.
What is a page?
A fixed-size unit used in virtual-memory management.
What is swap?
Storage used as part of the system’s virtual-memory management.
Is swap as fast as RAM?
No.
What can happen under extreme memory pressure?
The system can invoke OOM handling and may terminate processes.
Next Lesson — 021
Linux Storage and Disk I/O
Before we move into networking, we need to understand the other major resource your hosting server depends on:
RAM
↓
Storage
↓
Disk
↓
Partitions
↓
Filesystems
↓
Mount points
↓
SSD
↓
I/O
↓
I/O wait
↓
Disk usage
↓
Storage performance
Then we will connect it directly to your:
/storage/websites/
architecture and learn how Linux knows which physical/virtual storage device contains your websites.
Leave a Reply