SlideShare a Scribd company logo
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetcloud Training Program
ChinaNetCloud Training
Linux Memory Basics
By ChinaNetCloud
Pioneers in OaaS – Operations-as-a-Service
October, 2013
www.ChinaNetCloud.com
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 2
Introduction
●
Linux memory is complex and interesting
●
Class summarizes:
●
Types of memory
●
How it's used
●
How to troubleshoot memory issues
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 3
Overview, Purpose & Use
●
Linux memory is one of the most important
areas for engineers to understand
●
Used by everything
●
Often not well-understood
●
Often the cause of problems
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 4
Introduction
●
Memory is simple, in theory
●
Complex in real use
●
Plenty of strange things, too
●
Important to understand
●
Important to monitor
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 5
Memory Types I
●
Physical RAM – Fixed amount, real RAM
●
Virtual Memory – Virtual, can swap
●
Shared – Between processes
●
Oracle & Postgres use this
●
Slab – Kernel memory
●
/proc/slabinfo & slabtop utility
●
Includes big RAM users
– tcpmem & inode cache
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 6
Memory Types II
●
Page Cache – File system cache
●
Dirty
●
Changed file system data waiting to write to disks
●
Important for high write systems
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 7
/proc/meminfo
Very useful but complex
●
These are items not found by 'free' or 'top'
●
MemTotal: Total usable ram (RAM minus kernel binary code)
●
MemFree: Total free memory, same as free's 'free' output
●
SwapCached: Memory swapped out, then back in, but still
also in the swapfile
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 8
/proc/meminfo
●
Active – Recently used, not reclaimed unless necessary
●
Inactive – Less recently used, likely to be reclaimed
●
Dirty – File / Page Cache waiting to be written to disk
●
Writeback – Memory which is now being written to the disk
●
Mapped – Memory Mapped files, such as libraries
●
Includes Mongo, Varnish, and many others
●
Slab – Kernel memory, usually 256-512MB
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 9
Memory Page Size
●
4KB page size
●
Important as many stats are in pages
– Be careful of units ! KB vs. Pages
●
sysctl items like tcpmem are mixed
●
Do not confuse with disk sector size of 0.5KB
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 10
Free -m
●
Total – Physical in machine
●
Used – by Apps plus Cache, Buffers and maybe Shared
●
Free – Totally unused RAM, not important, usually very small
●
Shared – by different processes like Oracle / Postgres
●
Buffers – Raw block cache to/from disks, not impotrant
●
Cached (Page Cache) – Used by disk files cached in RAM
●
Swap – Total, Used, Free – Used should be small
●
-/+ buffers/cache – Important numbers, inside box is key
●
PAY ATTENTION to the BOX number – it's all that matters
total used free shared buffers cached
Mem: 3961 3901 60 0 121 1232
-/+ buffers/cache: 2546 1414
Swap: 4683 756 3927
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 11
Top, ps Memory Output
●
VIRT – Virtual size
●
Includes mapped libraries, often very large
●
Not too useful – IGNORE THIS
●
RSS – Resident Set Size
●
Most important !
●
Real RAM used by applications, not including swap
●
SHR – Shared, such as Oracle / Postgres
●
Be careful to see processes not threads
●
Threads will share RSS, see htop in thread mode
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 12
Swap
●
Simple, but not simple
●
Managed by kswapd
●
Actual swap used calc
●
SwapTotal – SwapCached – SwapFree
●
Goal is zero swap on servers
●
Never let a system actively swap
●
But some systems will have small swap
●
Common to see 100-200MB, more on big system
●
NUMA defaults can cause swapping
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 13
Problem with Swap
●
Swapping FREEZES the swapping process
●
So, a 32MB byte swap of MySQL RAM
●
Freezes ALL of MySQL for many seconds
●
Very bad for applications
●
Bad for any single process system
●
MySQL, Nginx, HAProxy, etc. (not Apache)
●
Also uses valuable IO, slowing DBs, etc.
●
Goal is to NEVER swap real applications
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 14
Swappiness
●
Tells kernel which is more important
●
File system Cache vs. App Memory
●
Default is 60 – Means cache more important
●
Stupid for servers – will swap even with free RAM
●
Set to 1, always (used to be 0, now use 1)
●
Won't swap until all RAM used by applications
●
Note some swap anyway (see next slide)
●
Set by sysctl
●
Check in /proc/sys/vm/swappiness
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 15
Swap still happens
●
Linux likes to swap a little
●
Even with free RAM and swappiness = 0
●
Some kernel data wants to swap
●
Can pre-swap SwapCached data
●
Still a mystery, but it's okay
●
Very large systems (64GB+ might swap 1-2GB)
●
Turning off can cause kswapd to go crazy
●
Watch vmstat swapin/out, make sure very small
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 16
NUMA - Can cause swap
●
NUMA – Non-Uniform Memory Access
●
Each CPU has its own RAM
●
Slower to use 'other' CPU's RAM
●
Standard now on all Intel servers
●
Causes RAM inbalance on big RAM processes
●
Like MySQL, MongoDB, Java
●
Install & use numa-utils package, numactl app
●
Set all big processes to full interleave:
●
numactrl –interleave all mysqld . . .
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 17
Dirty Memory & Ratios
●
Causes strange problems & performance issues
●
See /proc/meminfo
●
Dirty is the changed FileSystem cache data
●
Must be written to disk
●
Can cause big problems if too high
●
Two ratios control
●
/proc/sys/vm/dirty_background_ratio / _bytes
●
/proc/sys/vm/dirty_ratio / _bytes
●
Freezes all writing processes when dirty > dirty_ratio
●
Calculation not clear, but keep dirty data < 20% of RAM
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 18
OOM Killer
●
Kills process when no more RAM or Swap
●
Chooses process to die via a score
●
Biggest RAM user (MySQL) often dies first
●
See /proc/<pid>/oom_score
●
Always a message in dmesg - /var/log/kernel
●
Can adjust score to control (advanced)
●
Best practice to monitor swap and log
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2013 ChinaNetCloud 19
Summary
●
Memory important to understand
●
Especially basics & definitions
●
Understand swap & swappiness
●
Watch NUMA on big systems
●
Memory is cheap – buy more !
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
Copyright 2015 ChinaNetCloud 20
About ChinaNetCloud
ChinaNetCloudRunning the World's Internet Servers 管理全球服务器
ChinaNetCloud
Sales@ChinaNetCloud.com
www.ChinaNetCloud.com
Beijing Office:
Lee World Business Building #305
57 Happiness Village Road,
Chaoyang District
Beijing, 100027 China
Silicon Valley Office:
California Avenue
Palo Alto, 94123 USA
Shanghai Headquarters:
X2 Space 1-601, 1238 Xietu Lu
Shanghai, 200032 China
T: +86-21-6422-1946 F: +86-21-
6422-4911

More Related Content

What's hot (20)

PPT
Recent advances in the Linux kernel resource management
OpenVZ
 
PDF
MySQL always-up with Galera Cluster
FromDual GmbH
 
PPTX
Webinar: Keeping Your MongoDB Data Safe
MongoDB
 
PDF
Redis : Database, cache, pub/sub and more at Jelly button games
Redis Labs
 
ODP
Sdc challenges-2012
Gluster.org
 
PDF
DB Latency Using DRAM + PMem in App Direct & Memory Modes
ScyllaDB
 
PDF
kranonit S06E01 Игорь Цинько: High load
Krivoy Rog IT Community
 
PDF
Redis as a Main Database, Scaling and HA
Dave Nielsen
 
ODP
Tiering barcelona
Gluster.org
 
ODP
Memcache d
Abhishek Tomar
 
PDF
MariaDB Server Performance Tuning & Optimization
MariaDB plc
 
PDF
Building AuroraObjects- Ceph Day Frankfurt
Ceph Community
 
PDF
MySQL topology healing at OLA.
Mydbops
 
PDF
OpenNebula Conf 2014 | Lightning talk: OpenNebula at Etnetera by Jan Horacek
NETWAYS
 
PDF
Scalr: Setting Up Automated Scaling
Hakka Labs
 
PDF
Caffe + H2O - By Cyprien noel
Sri Ambati
 
PDF
Zingme practice for building scalable website with PHP
Chau Thanh
 
PDF
Zing Me Real Time Web Chat Architect
Chau Thanh
 
PDF
Avoiding Data Hotspots at Scale
ScyllaDB
 
PDF
Shared Database Concurrency
Aivars Kalvans
 
Recent advances in the Linux kernel resource management
OpenVZ
 
MySQL always-up with Galera Cluster
FromDual GmbH
 
Webinar: Keeping Your MongoDB Data Safe
MongoDB
 
Redis : Database, cache, pub/sub and more at Jelly button games
Redis Labs
 
Sdc challenges-2012
Gluster.org
 
DB Latency Using DRAM + PMem in App Direct & Memory Modes
ScyllaDB
 
kranonit S06E01 Игорь Цинько: High load
Krivoy Rog IT Community
 
Redis as a Main Database, Scaling and HA
Dave Nielsen
 
Tiering barcelona
Gluster.org
 
Memcache d
Abhishek Tomar
 
MariaDB Server Performance Tuning & Optimization
MariaDB plc
 
Building AuroraObjects- Ceph Day Frankfurt
Ceph Community
 
MySQL topology healing at OLA.
Mydbops
 
OpenNebula Conf 2014 | Lightning talk: OpenNebula at Etnetera by Jan Horacek
NETWAYS
 
Scalr: Setting Up Automated Scaling
Hakka Labs
 
Caffe + H2O - By Cyprien noel
Sri Ambati
 
Zingme practice for building scalable website with PHP
Chau Thanh
 
Zing Me Real Time Web Chat Architect
Chau Thanh
 
Avoiding Data Hotspots at Scale
ScyllaDB
 
Shared Database Concurrency
Aivars Kalvans
 

Viewers also liked (20)

PDF
Processes
Anil Kumar Pugalia
 
PDF
Linux Memory Management
Anil Kumar Pugalia
 
PPT
Linux memorymanagement
pradeepelinux
 
PPTX
Linux Memory Management
Suvendu Kumar Dash
 
PDF
(120513) #fitalk an introduction to linux memory forensics
INSIGHT FORENSIC
 
PPTX
Linux memory-management-kamal
Kamal Maiti
 
PDF
Understanding of linux kernel memory model
SeongJae Park
 
PPT
Linux memory consumption
haish
 
PDF
Process' Virtual Address Space in GNU/Linux
Varun Mahajan
 
PPTX
Christo kutrovsky oracle, memory & linux
Kyle Hailey
 
PDF
Threads
Anil Kumar Pugalia
 
PPTX
Linux Memory Management
Ni Zo-Ma
 
PDF
Embedded Storage Management
Anil Kumar Pugalia
 
PDF
Architecture Porting
Anil Kumar Pugalia
 
PDF
Embedded I/O Management
Anil Kumar Pugalia
 
PDF
Embedded Applications
Anil Kumar Pugalia
 
PDF
Mobile Hacking using Linux Drivers
Anil Kumar Pugalia
 
PDF
RPM Building
Anil Kumar Pugalia
 
PDF
Embedded Software Design
Anil Kumar Pugalia
 
PDF
Synchronization
Anil Kumar Pugalia
 
Linux Memory Management
Anil Kumar Pugalia
 
Linux memorymanagement
pradeepelinux
 
Linux Memory Management
Suvendu Kumar Dash
 
(120513) #fitalk an introduction to linux memory forensics
INSIGHT FORENSIC
 
Linux memory-management-kamal
Kamal Maiti
 
Understanding of linux kernel memory model
SeongJae Park
 
Linux memory consumption
haish
 
Process' Virtual Address Space in GNU/Linux
Varun Mahajan
 
Christo kutrovsky oracle, memory & linux
Kyle Hailey
 
Linux Memory Management
Ni Zo-Ma
 
Embedded Storage Management
Anil Kumar Pugalia
 
Architecture Porting
Anil Kumar Pugalia
 
Embedded I/O Management
Anil Kumar Pugalia
 
Embedded Applications
Anil Kumar Pugalia
 
Mobile Hacking using Linux Drivers
Anil Kumar Pugalia
 
RPM Building
Anil Kumar Pugalia
 
Embedded Software Design
Anil Kumar Pugalia
 
Synchronization
Anil Kumar Pugalia
 
Ad

Similar to Linux Memory Basics for SysAdmins - ChinaNetCloud Training (20)

PDF
Screaming Fast Wpmu
djcp
 
PDF
MySQL and MariaDB Backups
Federico Razzoli
 
PDF
Linux NUMA & Databases: Perils and Opportunities
Raghavendra Prabhu
 
PDF
How Can OpenNebula Fit Your Needs: A European Project Feedback
NETWAYS
 
PDF
OpenNebulaConf 2013 - How Can OpenNebula Fit Your Needs: A European Project F...
OpenNebula Project
 
PDF
Embedded Linux Basics
Marc Leeman
 
PDF
UKOUG 2011: Practical MySQL Tuning
FromDual GmbH
 
PDF
The Dark Side Of Go -- Go runtime related problems in TiDB in production
PingCAP
 
PDF
Mongo nyc nyt + mongodb
Deep Kapadia
 
PDF
Scaling up and accelerating Drupal 8 with NoSQL
OSInet
 
PDF
How can OpenNebula fit your needs - OpenNebulaConf 2013
Maxence Dunnewind
 
PDF
GCMA: Guaranteed Contiguous Memory Allocator
SeongJae Park
 
ODP
Nagios Conference 2014 - Andy Brist - Nagios XI Failover and HA Solutions
Nagios
 
PPTX
PL22 - Backup and Restore Performance.pptx
Vinicius M Grippa
 
PDF
Linux Huge Pages
Geraldo Netto
 
PDF
Software Design for Persistent Memory Systems
C4Media
 
PPTX
Redis Developers Day 2014 - Redis Labs Talks
Redis Labs
 
ODP
Optimizing Linux Servers
Davor Guttierrez
 
PDF
Deploying Containers and Managing Them
Docker, Inc.
 
PDF
Backing up Wikipedia Databases
Jaime Crespo
 
Screaming Fast Wpmu
djcp
 
MySQL and MariaDB Backups
Federico Razzoli
 
Linux NUMA & Databases: Perils and Opportunities
Raghavendra Prabhu
 
How Can OpenNebula Fit Your Needs: A European Project Feedback
NETWAYS
 
OpenNebulaConf 2013 - How Can OpenNebula Fit Your Needs: A European Project F...
OpenNebula Project
 
Embedded Linux Basics
Marc Leeman
 
UKOUG 2011: Practical MySQL Tuning
FromDual GmbH
 
The Dark Side Of Go -- Go runtime related problems in TiDB in production
PingCAP
 
Mongo nyc nyt + mongodb
Deep Kapadia
 
Scaling up and accelerating Drupal 8 with NoSQL
OSInet
 
How can OpenNebula fit your needs - OpenNebulaConf 2013
Maxence Dunnewind
 
GCMA: Guaranteed Contiguous Memory Allocator
SeongJae Park
 
Nagios Conference 2014 - Andy Brist - Nagios XI Failover and HA Solutions
Nagios
 
PL22 - Backup and Restore Performance.pptx
Vinicius M Grippa
 
Linux Huge Pages
Geraldo Netto
 
Software Design for Persistent Memory Systems
C4Media
 
Redis Developers Day 2014 - Redis Labs Talks
Redis Labs
 
Optimizing Linux Servers
Davor Guttierrez
 
Deploying Containers and Managing Them
Docker, Inc.
 
Backing up Wikipedia Databases
Jaime Crespo
 
Ad

More from ChinaNetCloud (20)

PPTX
AWS ELB Tips & Best Practices
ChinaNetCloud
 
PPTX
OpsStack--Integrated Operation Platform
ChinaNetCloud
 
PPTX
ChinaNetCloud Online Lecture:Something About Tshark
ChinaNetCloud
 
PPTX
ChinaNetCloud Online Lecture: Fight Against External Attacks From Different L...
ChinaNetCloud
 
PPTX
Steve Mushero on Entrepreneurship - 创业 - 崔牛会
ChinaNetCloud
 
PPTX
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
ChinaNetCloud
 
PPTX
云中漫步 颠覆创新_创业邦春季创新峰会主题演讲 Cloud Innovation in China
ChinaNetCloud
 
PDF
运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享
ChinaNetCloud
 
PPTX
AWS Summit OaaS Talk by ChinaNetCloud
ChinaNetCloud
 
PDF
Running Internet Systems in China - The Details You Need to Succeed in Chines...
ChinaNetCloud
 
PDF
Making Internet Operations Easier
ChinaNetCloud
 
PPTX
Internet Cloud Operations - ChinaNetcloud & AWS Event Beijing
ChinaNetCloud
 
PPTX
Big Data Security (ChinaNetCloud - Guiyang Conference)
ChinaNetCloud
 
PPTX
Internet System Security Overview
ChinaNetCloud
 
PPTX
Why Work at ChinaNetCloud
ChinaNetCloud
 
PPTX
Cloud Operations Challenges - Talk by ChinaNetCloud at Joint Cisco event
ChinaNetCloud
 
PPTX
Automatically Managing Internet Operations In The Cloud - 云计算平台的自动化运维
ChinaNetCloud
 
PPTX
ChinaNetCloud - Aliyun Joint Event on Cloud Operations
ChinaNetCloud
 
PDF
Clouds in China
ChinaNetCloud
 
PPTX
ChinaNetCloud - Public Clouds in China Overview
ChinaNetCloud
 
AWS ELB Tips & Best Practices
ChinaNetCloud
 
OpsStack--Integrated Operation Platform
ChinaNetCloud
 
ChinaNetCloud Online Lecture:Something About Tshark
ChinaNetCloud
 
ChinaNetCloud Online Lecture: Fight Against External Attacks From Different L...
ChinaNetCloud
 
Steve Mushero on Entrepreneurship - 创业 - 崔牛会
ChinaNetCloud
 
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
ChinaNetCloud
 
云中漫步 颠覆创新_创业邦春季创新峰会主题演讲 Cloud Innovation in China
ChinaNetCloud
 
运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享
ChinaNetCloud
 
AWS Summit OaaS Talk by ChinaNetCloud
ChinaNetCloud
 
Running Internet Systems in China - The Details You Need to Succeed in Chines...
ChinaNetCloud
 
Making Internet Operations Easier
ChinaNetCloud
 
Internet Cloud Operations - ChinaNetcloud & AWS Event Beijing
ChinaNetCloud
 
Big Data Security (ChinaNetCloud - Guiyang Conference)
ChinaNetCloud
 
Internet System Security Overview
ChinaNetCloud
 
Why Work at ChinaNetCloud
ChinaNetCloud
 
Cloud Operations Challenges - Talk by ChinaNetCloud at Joint Cisco event
ChinaNetCloud
 
Automatically Managing Internet Operations In The Cloud - 云计算平台的自动化运维
ChinaNetCloud
 
ChinaNetCloud - Aliyun Joint Event on Cloud Operations
ChinaNetCloud
 
Clouds in China
ChinaNetCloud
 
ChinaNetCloud - Public Clouds in China Overview
ChinaNetCloud
 

Recently uploaded (20)

PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 

Linux Memory Basics for SysAdmins - ChinaNetCloud Training

  • 1. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetcloud Training Program ChinaNetCloud Training Linux Memory Basics By ChinaNetCloud Pioneers in OaaS – Operations-as-a-Service October, 2013 www.ChinaNetCloud.com
  • 2. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 2 Introduction ● Linux memory is complex and interesting ● Class summarizes: ● Types of memory ● How it's used ● How to troubleshoot memory issues
  • 3. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 3 Overview, Purpose & Use ● Linux memory is one of the most important areas for engineers to understand ● Used by everything ● Often not well-understood ● Often the cause of problems
  • 4. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 4 Introduction ● Memory is simple, in theory ● Complex in real use ● Plenty of strange things, too ● Important to understand ● Important to monitor
  • 5. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 5 Memory Types I ● Physical RAM – Fixed amount, real RAM ● Virtual Memory – Virtual, can swap ● Shared – Between processes ● Oracle & Postgres use this ● Slab – Kernel memory ● /proc/slabinfo & slabtop utility ● Includes big RAM users – tcpmem & inode cache
  • 6. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 6 Memory Types II ● Page Cache – File system cache ● Dirty ● Changed file system data waiting to write to disks ● Important for high write systems
  • 7. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 7 /proc/meminfo Very useful but complex ● These are items not found by 'free' or 'top' ● MemTotal: Total usable ram (RAM minus kernel binary code) ● MemFree: Total free memory, same as free's 'free' output ● SwapCached: Memory swapped out, then back in, but still also in the swapfile
  • 8. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 8 /proc/meminfo ● Active – Recently used, not reclaimed unless necessary ● Inactive – Less recently used, likely to be reclaimed ● Dirty – File / Page Cache waiting to be written to disk ● Writeback – Memory which is now being written to the disk ● Mapped – Memory Mapped files, such as libraries ● Includes Mongo, Varnish, and many others ● Slab – Kernel memory, usually 256-512MB
  • 9. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 9 Memory Page Size ● 4KB page size ● Important as many stats are in pages – Be careful of units ! KB vs. Pages ● sysctl items like tcpmem are mixed ● Do not confuse with disk sector size of 0.5KB
  • 10. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 10 Free -m ● Total – Physical in machine ● Used – by Apps plus Cache, Buffers and maybe Shared ● Free – Totally unused RAM, not important, usually very small ● Shared – by different processes like Oracle / Postgres ● Buffers – Raw block cache to/from disks, not impotrant ● Cached (Page Cache) – Used by disk files cached in RAM ● Swap – Total, Used, Free – Used should be small ● -/+ buffers/cache – Important numbers, inside box is key ● PAY ATTENTION to the BOX number – it's all that matters total used free shared buffers cached Mem: 3961 3901 60 0 121 1232 -/+ buffers/cache: 2546 1414 Swap: 4683 756 3927
  • 11. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 11 Top, ps Memory Output ● VIRT – Virtual size ● Includes mapped libraries, often very large ● Not too useful – IGNORE THIS ● RSS – Resident Set Size ● Most important ! ● Real RAM used by applications, not including swap ● SHR – Shared, such as Oracle / Postgres ● Be careful to see processes not threads ● Threads will share RSS, see htop in thread mode
  • 12. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 12 Swap ● Simple, but not simple ● Managed by kswapd ● Actual swap used calc ● SwapTotal – SwapCached – SwapFree ● Goal is zero swap on servers ● Never let a system actively swap ● But some systems will have small swap ● Common to see 100-200MB, more on big system ● NUMA defaults can cause swapping
  • 13. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 13 Problem with Swap ● Swapping FREEZES the swapping process ● So, a 32MB byte swap of MySQL RAM ● Freezes ALL of MySQL for many seconds ● Very bad for applications ● Bad for any single process system ● MySQL, Nginx, HAProxy, etc. (not Apache) ● Also uses valuable IO, slowing DBs, etc. ● Goal is to NEVER swap real applications
  • 14. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 14 Swappiness ● Tells kernel which is more important ● File system Cache vs. App Memory ● Default is 60 – Means cache more important ● Stupid for servers – will swap even with free RAM ● Set to 1, always (used to be 0, now use 1) ● Won't swap until all RAM used by applications ● Note some swap anyway (see next slide) ● Set by sysctl ● Check in /proc/sys/vm/swappiness
  • 15. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 15 Swap still happens ● Linux likes to swap a little ● Even with free RAM and swappiness = 0 ● Some kernel data wants to swap ● Can pre-swap SwapCached data ● Still a mystery, but it's okay ● Very large systems (64GB+ might swap 1-2GB) ● Turning off can cause kswapd to go crazy ● Watch vmstat swapin/out, make sure very small
  • 16. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 16 NUMA - Can cause swap ● NUMA – Non-Uniform Memory Access ● Each CPU has its own RAM ● Slower to use 'other' CPU's RAM ● Standard now on all Intel servers ● Causes RAM inbalance on big RAM processes ● Like MySQL, MongoDB, Java ● Install & use numa-utils package, numactl app ● Set all big processes to full interleave: ● numactrl –interleave all mysqld . . .
  • 17. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 17 Dirty Memory & Ratios ● Causes strange problems & performance issues ● See /proc/meminfo ● Dirty is the changed FileSystem cache data ● Must be written to disk ● Can cause big problems if too high ● Two ratios control ● /proc/sys/vm/dirty_background_ratio / _bytes ● /proc/sys/vm/dirty_ratio / _bytes ● Freezes all writing processes when dirty > dirty_ratio ● Calculation not clear, but keep dirty data < 20% of RAM
  • 18. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 18 OOM Killer ● Kills process when no more RAM or Swap ● Chooses process to die via a score ● Biggest RAM user (MySQL) often dies first ● See /proc/<pid>/oom_score ● Always a message in dmesg - /var/log/kernel ● Can adjust score to control (advanced) ● Best practice to monitor swap and log
  • 19. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2013 ChinaNetCloud 19 Summary ● Memory important to understand ● Especially basics & definitions ● Understand swap & swappiness ● Watch NUMA on big systems ● Memory is cheap – buy more !
  • 20. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 Copyright 2015 ChinaNetCloud 20 About ChinaNetCloud
  • 21. ChinaNetCloudRunning the World's Internet Servers 管理全球服务器 ChinaNetCloud [email protected] www.ChinaNetCloud.com Beijing Office: Lee World Business Building #305 57 Happiness Village Road, Chaoyang District Beijing, 100027 China Silicon Valley Office: California Avenue Palo Alto, 94123 USA Shanghai Headquarters: X2 Space 1-601, 1238 Xietu Lu Shanghai, 200032 China T: +86-21-6422-1946 F: +86-21- 6422-4911