Linux Memory
Memory Hierarchy
Memory is the component within the computer which allows short-term data access.
Von Neumann architecture or Princeton architecture:
Harvard architecture:
The Harvard architecture is a computer architecture with separate storage and signal pathways for instructions and data, while in von Neumann architecture, instructions and data share a common bus, so that an instruction fetch and a data operation cannot occur at the same time.
There are 2 main types of memory hierarchy: primary memory and secondary memory.
Primary memory(Internal Memory): a segment of computer memory that can be accessed directly by the processor. Comprising of Main Memory, Cache Memory & CPU registers.
Secondary Memory(External Memory): peripheral storage devices which are accessible by the processor via I/O Module. Comprising of Magnetic Disk, Optical Disk, Magnetic Tape
Primary memory includes RAM and ROM.
RAM, Random Access Memory, is a volatile memory as the data is lost when the power is turned off.
ROM, Read-Only Memory, stores crucial information to operate the system, like boot. It is classified into 4 types MROM, PROM, EPROM, and EEPROM.
Linux Memory Management
Physical memory: The physical system memory is divided into pages.
Virtual memory: abstracts the details of physical memory from the application software. Each and every memory access uses a virtual address.
Page tables: Each physical memory page can be mapped as one or more virtual page, which is described by page tables. Namely, page tables, which are organized hierarchically, translate a virtual address used by programs to Physical memory address.
TLB: Translation Lookaside Buffer, the cache of mentioned translation above.
Page cache: Whenever a file is read, the data is put into the page cache to avoid expensive disk access on the subsequent reads. Similarly, when one writes to a file, the data is placed in the page cache and eventually gets into the backing storage device.
Zswap: Zswap is a lightweight compressed cache for swap pages. It takes pages that are in the process of being swapped out and attempts to compress them into a dynamically allocated RAM-based memory pool. (a new feature as of v3.11)
Clear RAM Memory
Check memory usage:
free -m # use MB
cat /proc/meminfo
vmstat -s #lays out the memory usage statistics
top
htop
Hardware information about the installed RAM: sudo dmidecode -t 17
or sudo dmidecode -t memory
Clear RAM without killing app or service
Clear PageCache only: sync; echo 1 > /proc/sys/vm/drop_caches
Clear dentries and inodes: sync; echo 2 > /proc/sys/vm/drop_caches
Clear pagecache, dentries, and inodes: sync; echo 3 > /proc/sys/vm/drop_caches
CLear Swap Space
swapoff -a && swapon -a
Ref
difference between memory and storage Memory architecture Memory Hierarchy Design and its Characteristics Random Access Memory (RAM) and Read Only Memory (ROM) Memory Management 5 commands to check memory usage on Linux How to Clear RAM Memory Cache, Buffer and Swap Space on Linux Random Access Memory (RAM) and Read Only Memory (ROM)