设为首页 加入收藏

TOP

MySQL 调优基础(二) Linux内存管理(一)
2015-11-21 01:38:37 来源: 作者: 【 】 浏览:6
Tags:MySQL 基础 Linux 内存 管理
进程的运行,必须使用内存。下图是Linux中进程中的内存的分布图:
其中最重要的 heap segment 和 stack segment。其它内存段基本是大小固定的。注意stack是向低地址增长的,和heap相反。另外进程的内存地址从0开始,是因为使用的是虚拟内存。所以存在虚拟内存到物理内存的映射。目前服务器一般都是64位的,32位的已经极少了,32为对内存有极大限制。
?
1. Linux 虚拟内存
?
Linux是通过虚拟内存的方式来管理内存的。虚拟内存和物理内存之间存在映射关系。当进程在CPU上运行时,虚拟内存就会映射到物理内存,供CPU来访问。
?
applications do not allocate physical memory, but request a memory map of a certain size at the Linux kernel and in exchange receive a map in virtual memory. As you can see, virtual memory does not necessarily have to be mapped into physical memory. If your application allocates a large amount of memory, some of it might be mapped to the swap file on the disk subsystem.
?
图示 进程虚拟内存 = 进程物理内存 + 进程swap(page out):
上图是top命令的截图,可以看到:mysqld 使用的虚拟内存为 735M,而常驻物理内存为 430M,所以其余的305M被swap out了(实际上是延迟分配)。
?
VIRT:The total amount of virtual memory used by the task. It includes all code, data and shared libraries plus pages that have been swapped out.
?
RES: Resident size (kb)。The non-swapped physical memory a task is using(常驻内存).
?
Linux handles the memory resource far more efficiently. The default configuration of the virtual memory manager allocates all available free?
memory space as disk cache. Hence it is not unusual to see productive Linux systems that boast gigabytes of memory but only have 20 MB of that memory free. In the same context, Linux also handles swap space very efficiently. Swap space being used does not indicate a memory bottleneck but proves how efficiently Linux handles system resources.?
?
There is no need to be alarmed if you find the swap partition filled to 50%. The fact that swap space is being used does not indicate a memory bottleneck; instead it proves how efficiently Linux handles system resources.
?
可见,系统显示空闲的内存很少,并不表示真的存在内存瓶颈;swap分区被使用了,也不代表存在内存瓶颈。
?
内存的分配:
?
Linux 管理内存是通过内存page为单位的,一般一个page为4K。Linux通过一个维持一个free内存的列表来管理和分配内存,并且维持内存的连续,防止内存碎片的产生。该系统被称为buddy system。内存的分配和管理全依靠buddy system.
?
内存的回收(page frame reclaiming):
?
当空闲内存不足时,就涉及到内存的回收。内存的回收有两种方式:回收用于缓存磁盘文件的 page cache(disk cache);swap out/page out 其它非活跃进程的内存;而且优先回收用于文件缓存的内存(disk cache):
?
When kswapd reclaims pages, it would rather shrink the page cache than page out (or swap out) the pages owned by processes.
?
然后会扫描内存的 active list 和 inactive list,根据LRU原则将active的page移到inactive,然后将inactive list中的page swap out.
?
active list 和 inactive list,可以用vmstat -a 查看到:
?
[root@localhost ~]# vmstat -a
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
?r ?b ? swpd ? free ?inact active ? si ? so ? ?bi ? ?bo ? in ? cs us sy id wa st
?0 ?0 0 462024 ?72680 471416 ? ?0 ? ?0 ? ?756 ?182 ?107 ?1 13 85 ?0 ?0
kswapd: kernel swap daemon
?
内存的两大主要用处:
?
The pages are used mainly for two purposes: page and process address space. The page cache is pages mapped to a file on disk. The?
cache pages that belong to a process address space (called anonymous memory because it is not mapped to any files, and it has no name) are used for heap and stack.
?
一、disk cache(page cache, file cache);
?
二、进程使用(anonymous memory、heap 和 stack)
?
kswapd 处理swap in 和swap out; 而 pdflush 处理disk cache到磁盘的刷新。
?
2. 如何尽量避免swap对mysql的影响
?
控制系统kswapd在内核中有一个专门的参数:
?
[root@localhost ~]# cat /proc/sys/vm/swappiness
60
我们把 vm.swappiness = 0 设置好,就可以在内存不足时,尽量避免系统发生swap,而尽量去 flush disk cache. 但是最新的Linux内核修改了对vm
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MySQL设置数据库表为只读 下一篇MySQL 调优基础(四) Linux 磁盘IO

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: