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

PDF
NUMA and Java Databases
PDF
High-availability with Galera Cluster for MySQL
PDF
HA with Galera
PDF
MySQL 高可用性
PDF
How to Meet Your P99 Goal While Overcommitting Another Workload
PDF
NoSQL with MySQL
PPTX
Webinar: Backups and Disaster Recovery
PPTX
Backing Up Data with MMS
NUMA and Java Databases
High-availability with Galera Cluster for MySQL
HA with Galera
MySQL 高可用性
How to Meet Your P99 Goal While Overcommitting Another Workload
NoSQL with MySQL
Webinar: Backups and Disaster Recovery
Backing Up Data with MMS

What's hot (20)

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

Viewers also liked (20)

PDF
PDF
Linux Memory Management
PPT
Linux memorymanagement
PPTX
Linux Memory Management
PDF
(120513) #fitalk an introduction to linux memory forensics
PPTX
Linux memory-management-kamal
PDF
Understanding of linux kernel memory model
PPT
Linux memory consumption
PDF
Process' Virtual Address Space in GNU/Linux
PPTX
Christo kutrovsky oracle, memory & linux
PDF
PPTX
Linux Memory Management
PDF
Embedded Storage Management
PDF
Architecture Porting
PDF
Embedded I/O Management
PDF
Embedded Applications
PDF
Mobile Hacking using Linux Drivers
PDF
RPM Building
PDF
Embedded Software Design
PDF
Synchronization
Linux Memory Management
Linux memorymanagement
Linux Memory Management
(120513) #fitalk an introduction to linux memory forensics
Linux memory-management-kamal
Understanding of linux kernel memory model
Linux memory consumption
Process' Virtual Address Space in GNU/Linux
Christo kutrovsky oracle, memory & linux
Linux Memory Management
Embedded Storage Management
Architecture Porting
Embedded I/O Management
Embedded Applications
Mobile Hacking using Linux Drivers
RPM Building
Embedded Software Design
Synchronization
Ad

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

PDF
Screaming Fast Wpmu
PDF
MySQL and MariaDB Backups
PDF
Linux NUMA & Databases: Perils and Opportunities
PDF
OpenNebulaConf 2013 - How Can OpenNebula Fit Your Needs: A European Project F...
PDF
How Can OpenNebula Fit Your Needs: A European Project Feedback
PDF
Embedded Linux Basics
PDF
UKOUG 2011: Practical MySQL Tuning
PDF
The Dark Side Of Go -- Go runtime related problems in TiDB in production
PDF
Mongo nyc nyt + mongodb
PDF
Scaling up and accelerating Drupal 8 with NoSQL
PDF
How can OpenNebula fit your needs - OpenNebulaConf 2013
PDF
GCMA: Guaranteed Contiguous Memory Allocator
ODP
Nagios Conference 2014 - Andy Brist - Nagios XI Failover and HA Solutions
PPTX
PL22 - Backup and Restore Performance.pptx
PDF
Linux Huge Pages
PDF
Software Design for Persistent Memory Systems
PPTX
Redis Developers Day 2014 - Redis Labs Talks
ODP
Optimizing Linux Servers
PDF
Deploying Containers and Managing Them
PDF
Backing up Wikipedia Databases
Screaming Fast Wpmu
MySQL and MariaDB Backups
Linux NUMA & Databases: Perils and Opportunities
OpenNebulaConf 2013 - How Can OpenNebula Fit Your Needs: A European Project F...
How Can OpenNebula Fit Your Needs: A European Project Feedback
Embedded Linux Basics
UKOUG 2011: Practical MySQL Tuning
The Dark Side Of Go -- Go runtime related problems in TiDB in production
Mongo nyc nyt + mongodb
Scaling up and accelerating Drupal 8 with NoSQL
How can OpenNebula fit your needs - OpenNebulaConf 2013
GCMA: Guaranteed Contiguous Memory Allocator
Nagios Conference 2014 - Andy Brist - Nagios XI Failover and HA Solutions
PL22 - Backup and Restore Performance.pptx
Linux Huge Pages
Software Design for Persistent Memory Systems
Redis Developers Day 2014 - Redis Labs Talks
Optimizing Linux Servers
Deploying Containers and Managing Them
Backing up Wikipedia Databases

More from ChinaNetCloud (20)

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

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation theory and applications.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Chapter 3 Spatial Domain Image Processing.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
cuic standard and advanced reporting.pdf
Encapsulation theory and applications.pdf
NewMind AI Monthly Chronicles - July 2025
Understanding_Digital_Forensics_Presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Modernizing your data center with Dell and AMD
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MYSQL Presentation for SQL database connectivity
The Rise and Fall of 3GPP – Time for a Sabbatical?
Building Integrated photovoltaic BIPV_UPV.pdf
The AUB Centre for AI in Media Proposal.docx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Digital-Transformation-Roadmap-for-Companies.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

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 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