如何查看Linux查看服务器内存命令使用

3088人阅读
mysql服务器管理优化(116)
原贴:/node/97
MySQL 服务器内存使用
- 21:09 & yejr
Every so often people ask me the question how should they estimate memory consumption by MySQL Server in given configuration. What is the formula they could use.
经常有人问我配置MySQL时该如何估算内存的消耗。那么该使用什么公式来计算呢?
The reasons to worry about memory usage are quite understandable. If you configure MySQL Server so it uses too small amount of memory it will likey perform suboptimally. If you however configure it so it consumes too much memory it may be crashing , failing to execute queries or make operation to swap seriously slowing down. On now legacy 32bit platforms you could also run out of address space so that had to be watched as well. Having said so, I do not think looking for the secret fomula to compute your possible memory usage is the right approach to this problem. The reasons are - this formula is very complex nowadays and what is even more important &theoretically possible& maximum it provides have nothing to do with real memory consumptions. In fact typical server with 8GB of memory will often run with maximum theoretical memory usage of 100GB or more. Furthermore there is no easy &overcommit factor& you can use - it really depends on application and configuration. Some applications will drive server to 10% of theoretical memory consumptions others only to 1%.
关心内存怎么使用的原因是可以理解的。如果配置MySQL服务器使用太少的内存会导致性能不是最优的;如果配置了太多的内存则会导致崩溃,无法执行查询或者导致交换操作严重变慢。在现在的32位平台下,仍有可能把所有的地址空间都用完了,因此需要监视着。 话虽如此,但我并不觉得找到什么可以计算内存使用的秘诀公式就能很好地解决这个问题。原因有 -- 如今这个公式已经很复杂了,更重要的是,通过它计算得到的值只是&理论可能&并不是真正消耗的值。事实上,有8GB内存的常规服务器经常能运行到最大的理 论值 -- 100GB甚至更高。此外,你轻易不会使用到&超额因素& -- 它实际上依赖于应用以及配置。一些应用可能需要理论内存的 10% 而有些仅需 1%。
So what could you do instead ?
First take a look at global buffers which are allocated at start and always where - these are key_buffer_size,
innodb_buffer_pool_size,
innodb_additional_memory_pool_size, innodb_log_buffer_size, query_cache_size. If you&re using MyISAM seriously you can also add the size of Operation System cache you would like MySQL to use for your table. Take this number add to it number of memory Operation System and other applications need, add might be 32MB more for MySQL Server code and various small static buffers. This is memory which you can consider used when you just start MySQL Server. The rest of memory is available for connections. For exampe with 8GB server you might have everything listed adding up to 6GB, so you have 2GB left for your threads.
那么,我们可以做什么呢?首先,来看看那些在启动时就需要分配并且总是存在的全局缓冲 -- key_buffer_size,
innodb_buffer_pool_size, innodb_additional_memory_pool_size, innodb_log_buffer_size, query_cache_size。 如果你大量地使用MyISAM表,那么你也可以增加操作系统的缓存空间使得MySQL也能用得着。把这些也都加到操作系统和应用程序所需的内存值之中,可 能需要增加32MB甚至更多的内存给MySQL服务器代码以及各种不同的小静态缓冲。这些就是你需要考虑的在MySQL服务器启动时所需的内存。其他剩下 的内存用于连接。例如有8GB内存的服务器,可能监听所有的服务就用了6GB的内存,剩下的2GB内存则留下来给线程使用。
Each thread connecting to MySQL server will needs its own buffers. About 256K is allocated at once even if thread is idle - they are used by default thread stack, net buffer etc. If transaction is started some more space can add up. Running small queries might only barely increase memory consumption for given thread, however if table will perform complex operations such as full table scans, sorts, or need temporary tables as much as read_buffer_size, sort_buffer_size, read_rnd_buffer_size,
tmp_table_size of memory might be allocated. But they are only allocated upon the need and freed once given stage of query is done. Some of them are allocated as single chunk at once others, for example tmp_table_size is rather maximum amount of memory MySQL will allocate for this operation. Note it is more complicated than once may think - multiple buffers of the same type might be allocated for exampe to handle subqueries. For some special queries memory usage might be even larger - bulk inserts may allocate bulk_insert_buffer_size bytes of memory if done to MyISAM tables.
myisam_sort_buffer_size used for ALTER TABLE, OPTIMIZE TABLE, REPAIR TABLE commands.
每个连接到MySQL服务器的线程都需要有自己的缓冲。大概需要立刻分配256K,甚至在线程空闲时 -- 它们使用默认的线程堆栈,网络缓存等。事务开始之后,则需要增加更多的空间。运行较小的查询可能仅给指定的线程增加少量的内存消耗,然而如果对数据表做复 杂的操作例如扫描、排序或者需要临时表,则需分配大约 read_buffer_size, sort_buffer_size, read_rnd_buffer_size,
tmp_table_size
大小的内存空间。不过它们只是在需要的时候才分配,并且在那些操作做完之后就释放了。有的是立刻分配成单独的组块,例如 tmp_table_size 可能高达MySQL所能分配给这个操作的最大内存空间了。注意,这里需要考虑的不只有一点 -- 可能会分配多个同一种类型的缓存,例如用来处理子查询。一些特殊的查询的内存使用量可能更大 -- 如果在MyISAM表上做成批的插入时需要分配 bulk_insert_buffer_size 大小的内存。执行 ALTER TABLE, OPTIMIZE TABLE, REPAIR TABLE 命令时需要分配 myisam_sort_buffer_size 大小的内存。
For OLTP applications with simple queries memory consumption is often less than 1MB per thread with default buffers, and you really do not need to increase per thread buffers unless you have complex queries. Sorting 10 rows will be as fast with 1MB sort buffer as with 16MB (actually 16MB might be even slower but it is other story).
只有简单查询OLTP应用的内存消耗经常是使用默认缓冲的每个线程小于1MB,除非需要使用复杂的查询否则无需增加每个线程的缓冲大小。使用1MB的缓冲来对10行记录进行排序和用16MB的缓冲基本是一样快的(实际上16MB可能会更慢,不过这是其他方面的事了)。
Another approach you may take is to come up with amount of memory you want MySQL Server to consume at peak. This can be easily computed by memory needed for OS, File Cache and other applications. For 32bit envinronment you also should keep 32bit limits into account and probably limit &mysqld& size to about 2.5GB (exact number depens on a lot of factors). Now you can use &ps aux& to see VSZ - Virtual Memory allocated by MySQL process. You can also look at &Resident Memory& but I find it less helpful as it may down because of swapping - not what you would like to see. Monitor how the value changes so you know memory requirements with current settings and increase/decrease values appropriately.
另外,就是找出MySQL服务器内存消耗的峰值。这很容易就能计算出操作系统所需的内存、文件缓存以及其他应用。在32位环境下,还需要考虑到32 位的限制,限制 &mysqld& 的值大约为2.5G(实际上还要考虑到很多其他因素)。现在运行 &ps aux& 命令来查看 VSZ 的值 -- MySQL 进程分配的虚拟内存。也可以查看 &Resident Memory& 的值,不过我想它可能没多大用处,因为它会由于交换而变小 -- 这并不是你想看到的。监视着内存变化的值,就能知道是需要增加/减少当前的内存值了。
Some may say, Hey we want to have 100% guarantee our server will never run out of memory, no matter which queries or users will decide to run. Unfortunately this is as much close to impossible to be impractical. Here is why:
可能有的人想说,我们想要让服务器能保证100%不会耗尽内存,不管决定用什么样的查询、什么样的用户。很不幸,这其实很不明智也不可能,因为:
rarely considered MySQL Server
Memory Requirements
以下是很少考虑的MySQL服务器内存需求
Thread buffers can be allocated more than once for each thread.
Consider for example subqueries - each layer may need its own read_buffer,sort_buffer, tmp_table_size
每个线程可能会不止一次需要分配缓冲。 考虑到例如子查询 -- 每层都需要有自己的 read_buffer,sort_buffer, tmp_table_size 等。
Many variabes can be set per connection.
So you can&t relay on global values if developers may use their local values to run some queries.
在每个连接中很多变量都可能需要重新设置。 如果开发者想设定自己的变量值来运行某些查询就不能继续使用全局值。
There can be mutiple key caches.
Multiple key caches can be created to accomodate query executions
可能有多个索引缓存。 为了配合执行查询可能会创建多个索引缓存。
Query Parsing and optimization needs memory.
This is usually small to be ignored but certain queries can have very large memory requrement for this step, especially specially crafted ones.
解析查询和优化都需要内存。 这些内存通常比较小,可以忽略,不过如果是某些查询在这个步骤中则需要大量内存,尤其是那些设计的比较特别的查询。
Stored Procedures. Compex stored procedures may require a lot of memory
存储过程。 复杂的存储过程可能会需要大量内存。
Prepared statements and Cursors. Single connection may have many prepared statements and cursors. Their number finally can be limited but each of them still can have very large memory consumption
准备查询语句以及游标。 单次链接可能会有很多的准备好的语句以及游标。它们的数量最后可以限定,但是仍然会消耗大量的内存。
Innodb Table Cache. Innodb has its own table cache in which meta data about each table accessed from the start is stored. It is never purged and may be large if you have a lot of tables. It also means user having CREATE TABLE privilege should be able to run MySQL server out of memory
Innodb表缓存。 Innnodb表有自己的缓存,它保存了从一开始访问每个表的元数据。它们从未被清除过,如果有很多Innodb表的话,那么这个量就很大了。这也就意味着拥有 CREATE TABLE 权限的用户就可能把MySQL服务器的内存耗尽。
MyISAM buffers.
MyISAM may allocate buffer which is large enough to contain largest record in the given table which is held until table is closed.
MyISAM缓冲。 MyISAM表可能会分配一个足以装下指定表最大记录的缓冲,而且这个缓冲直到表关闭了才释放。
Federated Storage Engine.
This may have unbound memory requirements
retriving result sets from remove queries.
FEDERATED存储引擎。 This may have unbound memory requirements
retriving result sets from remove queries.
Blobs may require 3x time of memory.
This is important if you&re deaing with large Blobs (your max_allowed_packet is large) Processing of 256MB of blob may require 768MB of memory.
Blobs可能需要3倍的内存。 这在处理很大(max_allowed_packet 的值较大)的Blobs数据时很重要,如果处理256MB的数据可能需要768MB的内存。
Storage Engines.
In general storage engines may have their own per thread or global memory allocations which are not tuned as buffers. Watch for these especially now with many storage engines being released for MySQL by various parties.
存储引擎。 通常情况下,存储引擎会设置自己的每个线程的全局分配内存,它通常不能像缓存一样可以调节。现在应该通过各种方式来特别关注MySQL释放出来的存储引擎。
I do not pretend this to be complete list. On the contrary I&m quite sure I&ve missed something (drop me a note if you have something to add). But the main point is - there are a lot of memory consumers out where and trying to find peak possible usage for each is impractical - so my advice would be measure what you get in practice and how memory consumption reacts to changing various variables. For example you may find out increasing sort_buffer_size from 1MB to
and 1000 max_connections
peak memory consumption just 30MB not
3000MB as you might have counted.
我想这还不是完成的列表,相反地,我觉得还是漏掉了一些(如果你知道,请给我回复加上)。但主要的原因是 -- 找到每次内存消耗峰值是不切实际的,因此我的这些建议可以用来衡量一下你实际修改一些变量值产生的反应。例如,把 sort_buffer_size 从1MB增加到4MB并且在 max_connections 为 1000 的情况下,内存消耗增长峰值并不是你所计算的3000MB而是30MB。
中文网,打造最好的
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:3160182次
积分:33221
积分:33221
排名:第146名
原创:76篇
转载:1209篇
评论:200条
(1)(16)(1)(10)(4)(16)(79)(59)(35)(38)(78)(24)(46)(25)(14)(12)(27)(32)(12)(2)(12)(12)(66)(261)(334)(42)(2)(25)
() () () () () ()
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'free命令可选参数
-b,-k,-m,-g show output in bytes, KB, MB, or GB
-h human readable output (automatic unit scaling)
-l show detailed low and high memory statistics
-o use old format (no -/+buffers/cache line)
-t display total for RAM + swap
-s update every [delay] seconds
-c update [count] times
-a show available memory if exported by kernel (&80 characters per line)
-V display version information and exit
常用参数演示
[root@ ~]# free -b   #以Byte为单位显示&&&&&&&&&&&& total&&&&&& used&&&&&& free&&&& shared&&& buffers&&&& cachedMem:&&& & & &&&&& 57344&& 4108800-/+ buffers/cache:& & Swap:&& & [root@ ~]# free -k   #以KB为单位显示&&&&&&&&&&&& total&&&&&& used&&&&&& free&&&& shared&&& buffers&&&& cachedMem:&&&&&& 1020128&&&& 536520&&&& 483608&&&&&&&& 56&&&&& 38108&&&& 121200-/+ buffers/cache:&&&& 377212&&&& 642916Swap:&&&&& 1535992&&&& 240172&&& 1295820[root@ ~]# free -m   #以MB为单位显示&&&&&&&&&&&& total&&&&&& used&&&&&& free&&&& shared&&& buffers&&&& cachedMem:&&&&&&&&&& 996&&&&&&& 523&&&&&&& 472&&&&&&&&& 0&&&&&&&& 37&&&&&&& 118-/+ buffers/cache:&&&&&&& 368&&&&&&& 627Swap:&&&&&&&& 1499&&&&&&& 234&&&&&& 1265[root@ ~]# free -h  #人性化输出显示&&&&&&&&&&&& total&&&&&& used&&&&&& free&&&& shared&&& buffers&&&& cachedMem:&&&&&&&&& 996M&&&&&& 521M&&&&&& 474M&&&&&&& 56K&&&&&&& 35M&&&&&& 118M-/+ buffers/cache:&&&&&& 368M&&&&&& 628MSwap:&&&&&&&& 1.5G&&&&&& 234M&&&&&& 1.2G[root@ ~]# free -o  #不显示-/+buffers/cache line,不建议&&&&&&&&&&&& total&&&&&& used&&&&&& free&&&& shared&&& buffers&&&& cachedMem:&&&&&& 1020128&&&& 536892&&&& 483236&&&&&&&& 56&&&&& 38468&&&& 121284Swap:&&&&& 1535992&&&& 240172&&& 1295820[root@ ~]# free -th  #算上swap&&&&&&&&&&&& total&&&&&& used&&&&&& free&&&& shared&&& buffers&&&& cachedMem:&&&&&&&&& 996M&&&&&& 524M&&&&&& 471M&&&&&&& 56K&&&&&&& 37M&&&&&& 118M-/+ buffers/cache:&&&&&& 368M&&&&&& 627MSwap:&&&&&&&& 1.5G&&&&&& 234M&&&&&& 1.2GTotal:&&&&&&& 2.4G&&&&&& 758M&&&&&& 1.7G[root@ ~]# time free -s 1 -c 3   #表示持续每隔1s更新更新一次mem信息,更新3次&&&&&&&&&&&& total&&&&&& used&&&&&& free&&&& shared&&& buffers&&&& cachedMem:&&&&&& 1020128&&&& 537512&&&& 482616&&&&&&&& 56&&&&& 38868&&&& 121328-/+ buffers/cache:&&&& 377316&&&& 642812Swap:&&&&& 1535992&&&& 240172&&& 1295820&&&&&&&&&&&& total&&&&&& used&&&&&& free&&&& shared&&& buffers&&&& cachedMem:&&&&&& 1020128&&&& 537512&&&& 482616&&&&&&&& 56&&&&& 38868&&&& 121328-/+ buffers/cache:&&&& 377316&&&& 642812Swap:&&&&& 1535992&&&& 240172&&& 1295820&&&&&&&&&&&& total&&&&&& used&&&&&& free&&&& shared&&& buffers&&&& cachedMem:&&&&&& 1020128&&&& 537512&&&& 482616&&&&&&&& 56&&&&& 38868&&&& 121328-/+ buffers/cache:&&&& 377316&&&& 642812Swap:&&&&& 1535992&&&& 240172&&& 1295820real&& &0m2.002suser&& &0m0.000ssys&& &0m0.001s[root@ ~]#
free 命令输出结果信息说明
&&&&&&&&&&&&         total&&&&&& used&&&&&& free&&&& shared&&& buffers&&&& cachedMem:&&&&&&&&&       996M&&&&&& 525M&&&&&& 470M&&&&&&& 56K&&&&&&& 38M&&&&&& 118M-/+ buffers/cache:&&&&&&      368M&&&&&&& 627MSwap:&&&&&&&&       1.5G&&&&&& 234M&&&&&& 1.2G
total: 内存总数
used: 已经使用内存数
free: 完全空闲内存
shared: 多个进程共享的内存
buffers: 用于块设备数据缓冲,记录文件系统metadata(目录,权限,属性等)
cached: 用于文件内容的缓冲
Mem: 物理内存
-/+ buffers/cache: 基于应用角度考虑(计算已使用内存时减去buffers/cache,计算可使用内存时加上buffers/cache)的内存情况,也可理解为真实的内存使用情况.
Swap: 交换分区
当我们获取系统内存用量的时候我们应该以“-/+ buffers/cached”行的used和free作为参考.因为第一行的buffers和cached被系统作为了缓存(这里包括缓冲了metadata数据和曾经打开过的内容,是为了加快我们系统处理的速度),而这部分缓存可以根据我们的应用内存使用情况随时释放掉(也可以手动释放).
这里的话我系统可用内存实际为:可用627M,已使用368M,而不是525M和470M.
buffers/cahed手动释放测试
[root@ ~]# free -h
        996M
-/+ buffers/cache:
        369M
        <span style="color: #.5G
<span style="color: #.2G
[root@ ~]#
使用命令手动释放
[root@ ~]# echo 3 & /proc/sys/vm/drop_caches
[root@ ~]# free -h
<span style="color: #ffK
-/+ buffers/cache:
      364M
      <span style="color: #.5G
<span style="color: #.2G
[root@ ~]#
手动释放后我们看到 第一行Mem的used和free有了很大的提升,我们上述的观点得到了验证
使用find 命令让buffers增加
[root@ backup]# free -h;find .&/dev/null;free -h
-/+ buffers/cache:
<span style="color: #.5G
<span style="color: #.2G
-/+ buffers/cache:
<span style="color: #.5G
<span style="color: #.2G
[root@ backup]#
使用cat 命令让cached增加
[root@zwj python]# free -h;find /mydata/backup/python/ -type f|xargs cat&/dev/null 2&&1;free -h
-/+ buffers/cache:
<span style="color: #.5G
<span style="color: #.2G
-/+ buffers/cache:
<span style="color: #.5G
<span style="color: #.2G
阅读(...) 评论()Linux下服务器内存测试软件memtester的使用
Linux下服务器内存测试软件memtester的使用
  Linux最简单发送邮件的方法 14:16一般使用本机的mail命令, 这就需要开启本机的smtp服务. 假如网络里面有一个邮件服务器(一般公司都有的), 则可以直接使用这台现有的邮件服务器, 不用开启本机的smtp服务, 只需要使用第三方软件sendEmail. 其官方地址:
  首先要在邮件服务器上新建一个账户用来做发邮件的账户, 可以利用一现有的, 假如为, SMTP邮件服务器地址为mai, SMTP验证的用户名密码(如果该邮件服务不需要SMTP验证则无需本用户名密码)为god/iamgod
  1. 安装sendMail
  # cd /u01/software/nagios# wget # tar -zxvf sendEmail-v1.56.tar.gz && cd sendEmail-v1.56# cp sendEmail /usr/local/bin# chmod 0755 /usr/local/bin/sendEmail
  2. 确保可访问域名
  # vi /etc/nf&&&&&&&&&&& (使用Google的公共DNS服务, 其它也行)nameserver 8.8.8.8nameserver 8.8.4.4# ping -c 3 mai&&& (确认可访问smtp服务器域名)PING mai (222.232.145.18) 56(84) bytes of data.64 bytes from mai (222.232.145.18): icmp_seq=0 ttl=63 time=10.0 ms64 bytes from mai (222.232.145.18): icmp_seq=1 ttl=63 time=0.749 ms64 bytes from mai (222.232.145.18): icmp_seq=2 ttl=63 time=8.35 ms--- mai ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2001msrtt min/avg/max/mdev = 0.749/6.388/10.064/4.049 ms, pipe 2
  3. 发送邮件测试
  # sendEmail -h&&&&&&&&&&&&&&& (查看sendMail帮助)
  # sendEmail -f
-s mai -u "nagios测试" -m "nagios test 测试"
  各参数含义如下:-f 表示发送者的邮箱, 可随意设置-t 表示接收者的邮箱-s 表示SMTP服务器的域名或者IP-u 表示邮件的主题-m 表示邮件的内容-xu 表示SMTP验证的用户名, 如果SMTP服务器需要验证的话就加上该参数, 一般发给外网用户则需要-xp 表示SMTP验证的密码, 如果SMTP服务器需要验证的话就加上该参数, 一般发给外网用户则需要-a FILE [FILE ...]&&&&& file attachment(s)-cc ADDRESS [ADDR ...]&& cc& email address(es)-bcc ADDRESS [ADDR ...]&& bcc email address(es)
  4. 发送邮件高级用法
  如果不带-m参数的话,就会提示你自行输入, 输入完成后使用CTRL-D来结束. 如果还要发送给外网邮件用户, 则还需设置SMTP验证:# sendEmail -f
-s mai -u "nagios测试" -m "nagios test 测试" -xu god -xp "iamgod"
  也可以将一个文件的内容作为邮件的正文发出:# sendEmail -f
-s mai -u "nagios测试" -o message-file=/var/log/messages
  或者将一个文件的内容作为附件发出:# sendEmail -f
-s mai -u "nagios测试" -m "pls see the attachement" -a /var/log/messages
  --End--
&&&主编推荐
H3C认证Java认证Oracle认证
基础英语软考英语项目管理英语职场英语
.NETPowerBuilderWeb开发游戏开发Perl
二级模拟试题一级模拟试题一级考试经验四级考试资料
软件测试软件外包系统分析与建模敏捷开发
法律法规历年试题软考英语网络管理员系统架构设计师信息系统监理师
高级通信工程师考试大纲设备环境综合能力
路由技术网络存储无线网络网络设备
CPMP考试prince2认证项目范围管理项目配置管理项目管理案例项目经理项目干系人管理
职称考试题目
招生信息考研政治
网络安全安全设置工具使用手机安全
生物识别传感器物联网传输层物联网前沿技术物联网案例分析
Java核心技术J2ME教程
Linux系统管理Linux编程Linux安全AIX教程
Windows系统管理Windows教程Windows网络管理Windows故障
数据库开发Sybase数据库Informix数据库
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&2012年9月 Linux/Unix社区大版内专家分月排行榜第二2012年7月 Linux/Unix社区大版内专家分月排行榜第二
2012年6月 Linux/Unix社区大版内专家分月排行榜第三2009年4月 硬件/嵌入开发大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。Linux 服务器内存使用分析 - 简书
Linux 服务器内存使用分析
运行在 linux上的应用程序大部分都是需要占用内存,像 web 服务器(nginx\apache), Rails 程序 ,php-fpm 等等。如果服务器上可用的内存不够,就会出现部分程序会启动不起来,或者有些程序会被系统 kill 掉。
我们可以通过以下命令,查询出哪些程序占用的内存比较多,如果有些是无用的进程,则手动把它 kill 掉。
查看占用内存最多的程序(前10)
ps aux | sort -k4,4nr | head -n 10
查看服务器内存的使用情况
或者,如果没有可以 kill 的进程,这时候就要考虑是不是要增加服务器的内存。
另,系统内存和应用使用内存的区别:
Paste_Image.png
Paste_Image.png
存在即合理}

我要回帖

更多关于 服务器内存 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信