shallow and retained sizes


Shallow size of an object is the amount of memory allocated to store the object itself, not taking into account the referenced objects. Shallow size of a regular (non-array) object depends on the number and types of its fields. Shallow size of an array depends on the array length and the type of its elements (objects, primitive types). Shallow size of a set of objects represents the sum of shallow sizes of all objects in the set.
Retained size of an object is its shallow size plus the shallow sizes of the objects that are accessible, directly or indirectly, only from this object. In other words, the retained size represents the amount of memory that will be freed by the garbage collector when this object is collected.
To better understand the notion of the retained size, let us look at the following examples:
In order to measure the retained sizes, all objects in memory are treated as nodes of a graph where its edges represent references from objects to objects. There are also special nodes - GC root objects, which will not be collected by Garbage Collector at the time of measuring (read more about GC roots).
The pictures below show the same set of objects, but with varying internal references.
Figure 1:
Figure 2:
Let us consider obj1.
As you can see, in both pictures we have highlighted all of the objects that are directly or indirectly accessed only by obj1. If you look at Figure 1, you will see thatobj3 is not highlighted, because it is also referenced by a GC root object. On Figure 2, however, it is already included into the retained set, unlike obj5, which is still referenced by GC root.
Thus, the retained size of obj1 will represent the following respective values:
  • For Figure 1: the sum of shallow sizes of obj1obj2 and obj4
  • For Figure 2: the sum of shallow sizes of obj1obj2obj3 and obj4
Looking at obj2, however, we see that its retained size in the above cases will be:
  • For Figure 1: the sum of shallow sizes of obj2 and obj4
  • For Figure 2: the sum of shallow sizes of obj2obj3 and obj4
In general, retained size is an integral measure, which helps to understand the structure (clustering) of memory and the dependencies between object subgraphs, as well as find potential roots of those subgraphs.

你可能不知道的Shell


Shell也叫做命令行界面,它是*nix操作系统下用户和计算机的交互界面。Shell这个词是指操作系统中提供访问内核服务的程序。

这篇文章向大家介绍Shell一些非广为人知、但却实用有趣的知识,权当品尝shell主食后的甜点吧。

科普

先科普几个你可能不知道的事实:

Shell几乎是和Unix操作系统一起诞生,第一个Unix Shell是肯·汤普逊(Ken Thompson)以Multics上的Shell为模范在1971年改写而成,并命名Thompson sh。即便是后来流行的bash(shell的一种变体),它的年龄实际上比当前流行的所有的Linux kernel都大,可谓在Linux系统上是先有Shell再有Kernel。
当前绝大部分*nix和MacOS操作系统里的默认的Shell都是bash,bash由Brian Fox在1987年创造,全称Bourne Again shell ( bash)。
你或许听说除了bash之外,还有Bourne shell ( sh),Korn shell ( ksh),C shell (包括 csh and tcsh),但是你知道这个星球上一共存在着大约50多种不同的shell么?想了解他们,请参考 http://www.freebsd.org/ports/shells.html。
每个月tiobe上都会给一个编程语言的排名,来显示各种语言的流行度。排名指数综合了全球范围内使用该语言的工程师人数、教学的课程数和第三方供应商数。截止至2012年11月份,tiobe公布的编程语言排行榜里,bash的指数是0.56%排名22位。如果算上它旗下的awk 0.21%和tcl 0.146%,大概就能排到14名。注意这里还不包括bash的同源的兄弟姐妹csh、ksh等,算上它们,shell家族有望接近前十。值得一提的是一直以来shell的排名就很稳定,不像某些“暴发户”语言,比如objective-c,这些语言的流行完全是因为当前Apple系的崛起,但这种热潮极有可能来得快去得更快。




全球最大的源代码仓库Github里,shell相关的项目数占到了8%,跻身前5和Java相当,可见在实战工程里,shell可谓宝刀不老。图片来源,参见这里


一些强大的命令

再分享一些可能你不知道的shell用法和脚本,简单&强大!

在阅读以下部分前,强烈建议读者打开一个shell实验,这些都不是shell教科书里的大路货哦:)

!$
!$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串。如:你可能会这样:
$mkdir mydir
$mv mydir yourdir
$cd yourdir
可以改成:
$mkdir mydir
$mv !$ yourdir
$cd !$
sudo !!
以root的身份执行上一条命令 。
场景举例:比如Ubuntu里用apt-get安装软件包的时候是需要root身份的,我们经常会忘记在apt-get前加sudo。每次不得不加上sudo再重新键入这行命令,这时可以很方便的用sudo !!完事。
(陈皓注:在shell下,有时候你会输入很长的命令,你可以使用!xxx来重复最近的一次命令,比如,你以前输入过,vi /where/the/file/is, 下次你可以使用 !vi 重得上次最近一次的vi命令。)
cd –
回到上一次的目录 。
场景举例:当前目录为/home/a,用cd ../b切换到/home/b。这时可以通过反复执行cd –命令在/home/a和/home/b之间来回方便的切换。
(陈皓注:cd ~ 是回到自己的Home目录,cd ~user,是进入某个用户的Home目录)
‘ALT+.’ or ‘ .’
热建alt+. 或 esc+. 可以把上次命令行的参数给重复出来。
^old^new
替换前一条命令里的部分字符串。
场景:echo "wanderful",其实是想输出echo "wonderful"。只需要^a^o就行了,对很长的命令的错误拼写有很大的帮助。(陈皓注:也可以使用 !!:gs/old/new)
du -s * | sort -n | tail
列出当前目录里最大的10个文件。
:w !sudo tee %
在vi中保存一个只有root可以写的文件
date -d@1234567890
时间截转时间
> file.txt
创建一个空文件,比touch短。
mtr coolshell.cn
mtr命令比traceroute要好。
在命令行前加空格,该命令不会进入history里。
echo “ls -l” | at midnight
在某个时间运行某个命令。
curl -u user:pass -d status=”Tweeting from the shell” http://twitter.com/statuses/update.xml
命令行的方式更新twitter。
curl -u username –silent “https://mail.google.com/mail/feed/atom” | perl -ne ‘print “\t” if //; print “$2\n” if /<(title|name)>(.*)<\/\1>/;’
检查你的gmail未读邮件
ps aux | sort -nk +4 | tail
列出头十个最耗内存的进程
man ascii
显示ascii码表。
场景:忘记ascii码表的时候还需要google么?尤其在天朝网络如此“顺畅”的情况下,就更麻烦在GWF多应用一次规则了,直接用本地的man ascii吧。
ctrl-x e
快速启动你的默认编辑器(由变量$EDITOR设置)。
netstat –tlnp
列出本机进程监听的端口号。(陈皓注:netstat -anop 可以显示侦听在这个端口号的进程)
tail -f /path/to/file.log | sed '/^Finished: SUCCESS$/ q'
当file.log里出现Finished: SUCCESS时候就退出tail,这个命令用于实时监控并过滤log是否出现了某条记录。
ssh user@server bash < /path/to/local/script.sh
在远程机器上运行一段脚本。这条命令最大的好处就是不用把脚本拷到远程机器上。
ssh user@host cat /path/to/remotefile | diff /path/to/localfile -
比较一个远程文件和一个本地文件
net rpc shutdown -I ipAddressOfWindowsPC -U username%password
远程关闭一台Windows的机器
screen -d -m -S some_name ping my_router
后台运行一段不终止的程序,并可以随时查看它的状态。-d -m参数启动“分离”模式,-S指定了一个session的标识。可以通过-R命令来重新“挂载”一个标识的session。更多细节请参考screen用法 man screen。
wget --random-wait -r -p -e robots=off -U mozilla http://www.example.com
下载整个www.example.com网站。(注:别太过分,大部分网站都有防爬功能了:))
curl ifconfig.me
当你的机器在内网的时候,可以通过这个命令查看外网的IP。
convert input.png -gravity NorthWest -background transparent -extent 720×200  output.png
改一下图片的大小尺寸
lsof –i
实时查看本机网络服务的活动状态。
vim scp://username@host//path/to/somefile
vim一个远程文件
python -m SimpleHTTPServer
一句话实现一个HTTP服务,把当前目录设为HTTP服务目录,可以通过http://localhost:8000访问 这也许是这个星球上最简单的HTTP服务器的实现了。
history | awk '{CMD[$2]++;count++;} END { for (a in CMD )print CMD[a] " " CMD[a]/count*100 "% " a }' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10
(陈皓注:有点复杂了,history|awk ‘{print $2}’|awk ‘BEGIN {FS=”|”} {print $1}’|sort|uniq -c|sort -rn|head -10)
这行脚本能输出你最常用的十条命令,由此甚至可以洞察你是一个什么类型的程序员。
tr -c “[:digit:]” ” ” < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR=”1;32″ grep –color “[^ ]“
想看看Marix的屏幕效果吗?(不是很像,但也很Cool!)
看不懂行代码?没关系,系统的学习一下*nix shell脚本吧,力荐《Linux命令行与Shell脚本编程大全》。

最后还是那句Shell的至理名言:(陈皓注:下面的那个马克杯很不错啊,404null.com挺有意思的)

ps -elL | awk '$2 == "D"'

ps -elL | awk '$2 == "D"'

如何将函数的实际参数转化为真正的数组?


函数的arguments在JavaScript里只是一个类似数组的对象,和NodeList类似,拥有length属性,但没有push、pop和shift等数组中的方法,并不是真正意义上的数组,值得庆幸的是,我们可以通过数组的slice方法将arguments对象转换成真正的数组:



上述两种方式虽然实现的目的相同,但是两者在性能上有一些细微的差异。测试结果表明,在arguments.length较小的时候,方式2在性能上稍占优势,但是当arguments.length较大的时候,方式1就略胜一筹。

除此之外,还有第三种最传统最老土的方法:


在具体的应用中,还需结合实际情况,灵活选择最合适的方式,下边的实例展示了转换前后的差异。




Javascript 绝句


像诗一样的 Javascript 代码。

1. 取整同时转成数值型:

'10.567890'|0
//结果: 10
'10.567890'^0
//结果: 10
-2.23456789|0
//结果: -2
~~-2.23456789
//结果: -2
2. 日期转数值:

var d = +new Date(); //1295698416792
3. 类数组对象转数组:

var arr = [].slice.call(arguments)
4. 漂亮的随机码:

Math.random().toString(16).substring(2); //14位
Math.random().toString(36).substring(2); //11位
5. 合并数组:

var a = [1,2,3];
var b = [4,5,6];
Array.prototype.push.apply(a, b);
uneval(a); //[1,2,3,4,5,6]
6. 用0补全位数:

function prefixInteger(num, length) {
return (num / Math.pow(10, length)).toFixed(length).substr(2);
}
7. 交换值:

a= [b, b=a][0];
8. 将一个数组插入另一个数组的指定位置:

var a = [1,2,3,7,8,9];
var b = [4,5,6];
var insertIndex = 3;
a.splice.apply(a, Array.concat(insertIndex, 0, b));
// a: 1,2,3,4,5,6,7,8,9
9. 删除数组元素:

var a = [1,2,3,4,5];
a.splice(3,1);
10. 快速取数组最大和最小值

Math.max.apply(Math, [1,2,3]) //3
Math.min.apply(Math, [1,2,3]) //1
(出自http://ejohn.org/blog/fast-javascript-maxmin/)

11. 条件判断:

var a = b && 1;
//相当于
if (b) {
a = 1;
} else {
a = b;
}

var a = b || 1;
//相当于
if (b) {
a = b;
} else {
a = 1;
}
12. 判断IE(两种方法):

var ie = /*@cc_on !@*/false;
var ie = !-[1,];
原文地址:http://bai.lu/RVT1X

How to Check Filesystem Block Size on Linux?

How to Check Filesystem Block Size on Linux?
Example 1:
# tune2fs -l /dev/sda1 | grep -i ‘block size’
Block size: 4096
Example 2:
# dumpe2fs -h /dev/sda1 |grep “Block size:”
Block size: 4096
Example 3:
# blockdev –getbsz /dev/sda1
4096
Example 4:
# echo “abc” >test.txt
# du -h test.txt
4.0K test

用nginx做代理服务器上网

目前现状:只有1个机器能上网(web),其他机器不能
方法:能上网的做一个代理web服务器中转,其他机器连接它即可。采用nginx

Nginx配置如下:

server{
        resolver x.x.x.x;
        listen 82;
        location / {
                proxy_pass http://$http_host$request_uri;
        }
}

注意项:
1. 不能有hostname
2. 必须有resolver, 即dns,即上面的x.x.x.x,换成你们的DNS服务器ip即可
3 . $http_host和$request_uri是nginx系统变量,不要想着替换他们,保持原样就OK。

查看dns方法
cat /etc/resolv.conf

代理使用

在需要访问外网的机器上执行以下操作之一即可:
1. export http_proxy=http://yourproxyaddress:proxyport
2. gedit ~/.bashrc  
    export http_proxy=http://yourproxyaddress:proxyport
yourproxyaddress也就是你的Nginx服务器的ip了,proxyport就是上面配置中的82,可以根据自己的需要修改。


举例:

  1. worker_processes 1;
  2. master_process off;
  3. daemon off;
  4. #pid /var/run/nginx.pid;

  5. events {
  6. worker_connections 768;
  7. # multi_accept on;
  8. }

  9. http {
  10. include mime.types;
  11. default_type application/octet-stream;

  12. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  13. '$status $body_bytes_sent "$http_referer" '
  14. '"$http_user_agent" "$http_x_forwarded_for"';

  15. access_log /var/log/nginx/access.log;
  16. error_log /var/log/nginx/error.log;

  17. sendfile on;

  18. server {
  19. resolver 10.57.220.2;
  20. listen 82;
  21. access_log logs/host.access.log main;

  22. location / {
  23. proxy_pass http://$http_host$request_uri;
  24. }


  25. }
  26. }

/bin/bash: bad interpreter: Text file busy

/bin/bash: bad interpreter: Text file busy Error and Solution

Q. I'm getting an error as follows while trying to run a shell script over remote ssh session:

./myscript.sh
/bin/bash: bad interpreter: Text file busy

How do I fix this error message and run the script?

A. This error means some other process or user is accessing your file. Your script file is open and hence bash giving out this error.

24 iostat, vmstat and mpstat Examples for Linux Performance Monitoring

This article provides a total of 24 examples on iostat, vmstat, and mpstat commands.

  • iostat reports CPU, disk I/O, and NFS statistics.
  • vmstat reports virtual memory statistics.
  • mpstat reports processors statictics.

This article is part of our ongoing Linux performance monitoring series.

Please note that iostat and vmstat are part of the sar utility. You should install sysstat package as explained in our sar (sysstat) article to get iostat and vmstat working.

IOSTAT EXAMPLES

1. iostat – Basic example

Iostat without any argument displays information about the CPU usage, and I/O statistics about all the partitions on the system as shown below.

  $ iostat Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011  avg-cpu:  %user   %nice %system %iowait  %steal   %idle            5.68    0.00    0.52    2.03    0.00   91.76  Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn sda             194.72      1096.66      1598.70 2719068704 3963827344 sda1            178.20       773.45      1329.09 1917686794 3295354888 sda2             16.51       323.19       269.61  801326686  668472456 sdb             371.31       945.97      1073.33 2345452365 2661206408 sdb1            371.31       945.95      1073.33 2345396901 2661206408 sdc             408.03       207.05       972.42  513364213 2411023092 sdc1            408.03       207.03       972.42  513308749 2411023092

2. iostat – Display only cpu statistics

iostat option -c, displays only the CPU usage statistics as shown below.

  $ iostat -c Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011  avg-cpu:  %user   %nice %system %iowait  %steal   %idle            5.68    0.00    0.52    2.03    0.00   91.76

3. iostat – Display only disk I/O statistics

iostat option -d, displays only the disk I/O statistics as shown below.

  $ iostat -d Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011  Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn sda             194.71      1096.61      1598.63 2719068720 3963827704 sda1            178.20       773.41      1329.03 1917686810 3295355248 sda2             16.51       323.18       269.60  801326686  668472456 sdb             371.29       945.93      1073.28 2345452365 2661209192 sdb1            371.29       945.91      1073.28 2345396901 2661209192 sdc             408.01       207.04       972.38  513364213 2411024484 sdc1            408.01       207.02       972.38  513308749 2411024484

4. iostat – Display only network statistics

iostat option -n, displays only the device and NFS statistics as shown below.

  $ iostat -n Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)        07/09/2011  avg-cpu:  %user   %nice    %sys %iowait   %idle            4.33    0.01    1.16    0.31   94.19  Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn sda               2.83         0.35         5.39   29817402  457360056 sda1              3.32        50.18         4.57 4259963994  387641400 sda2              0.20         0.76         0.82   64685128   69718576 sdb               6.59        15.53        42.98 1318931178 3649084113 sdb1             11.80        15.53        42.98 1318713382 3649012985  Device:                  rBlk_nor/s   wBlk_nor/s   rBlk_dir/s   wBlk_dir/s   rBlk_svr/s   wBlk_svr/s 192.168.1.4:/home/data      90.67        0.00         0.00         0.00         5.33         0.00 192.168.1.4:/backup         8.74         0.00         0.00         0.00         8.74         0.00 192.168.1.8:/media          0.02         0.00         0.00         0.00         0.01         0.00

5. iostat – Display I/O data in MB/second

By default iostat, displays the device I/O statistics in Blocks. To change it to MB, use -m as shown below.

  $ iostat -m Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011  avg-cpu:  %user   %nice %system %iowait  %steal   %idle            5.68    0.00    0.52    2.03    0.00   91.76  Device:            tps    MB_read/s    MB_wrtn/s    MB_read    MB_wrtn sda             194.70         0.54         0.78    1327670    1935463 sda1            178.19         0.38         0.65     936370    1609060 sda2             16.51         0.16         0.13     391272     326402 sdb             371.27         0.46         0.52    1145240    1299425 sdb1            371.27         0.46         0.52    1145213    1299425 sdc             407.99         0.10         0.47     250666    1177259 sdc1            407.99         0.10         0.47     250639    1177259

6. iostat – Display I/O statistics only for a device

By default iostat displays I/O data for all the disks available in the system. To view statistics for a specific device (For example, /dev/sda), use the option -p as shown below.

  $ iostat -p sda Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011  avg-cpu:  %user   %nice %system %iowait  %steal   %idle            5.68    0.00    0.52    2.03    0.00   91.76  Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn sda             194.69      1096.51      1598.48 2719069928 3963829584 sda2            336.38        27.17        54.00   67365064  133905080 sda1            821.89         0.69       243.53    1720833  603892838

7. iostat – Display timestamp information

By default iostat displays only the current date. To display the current time, use the option -t as shown below.

  $ iostat -t Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011  Time: 08:57:52 AM avg-cpu:  %user   %nice %system %iowait  %steal   %idle            5.68    0.00    0.52    2.03    0.00   91.76  Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn sda             194.69      1096.49      1598.45 2719070384 3963829704 sda1            178.18       773.32      1328.88 1917688474 3295357248 sda2             16.51       323.14       269.57  801326686  668472456 sdb             371.25       945.82      1073.16 2345452741 2661228872 sdb1            371.25       945.80      1073.16 2345397277 2661228872 sdc             407.97       207.02       972.27  513364233 2411030200 sdc1            407.97       207.00       972.27  513308769 2411030200

8. iostat – Display Extended status

Use option -x, which will displays extended disk I/O statistics information as shown below.

  $ iostat -x Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011  avg-cpu:  %user   %nice %system %iowait  %steal   %idle            5.68    0.00    0.52    2.03    0.00   91.76  Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util sda              27.86    63.53 61.77 132.91  1096.46  1598.40    13.84     0.21    1.06   2.28  44.45 sda1              0.69    33.22 48.54 129.63   773.30  1328.84    11.80     1.39    7.82   2.28  40.57 sda2             27.16    30.32 13.23  3.28   323.13   269.56    35.90     0.55   32.96   3.44   5.68 sdb              39.15   215.16 202.20 169.04   945.80  1073.13     5.44     1.05    2.78   1.64  60.91 sdb1             39.15   215.16 202.20 169.04   945.77  1073.13     5.44     1.05    2.78   1.64  60.91 sdc               8.90     3.63 356.56 51.40   207.01   972.24     2.89     1.04    2.56   1.55  63.30 sdc1              8.90     3.63 356.55 51.40   206.99   972.24     2.89     1.04    2.56   1.55  63.30

To display extended information for a specific partition (For example, /dev/sda1), do the following.

  $ iostat -x sda1 Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011  avg-cpu:  %user   %nice %system %iowait  %steal   %idle            5.68    0.00    0.52    2.03    0.00   91.76  Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util sda1              0.69    33.21 48.54 129.62   773.23  1328.76    11.80     1.39    7.82   2.28  40.56

9. iostat – Execute Every x seconds (for y number of times)

To execute iostat every 2 seconds (until you press Ctl-C), do the following.

  $ iostat 2 Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011  avg-cpu:  %user   %nice %system %iowait  %steal   %idle            5.68    0.00    0.52    2.03    0.00   91.76  Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn sda             194.67      1096.39      1598.33 2719070584 3963891256 sda1            178.16       773.26      1328.79 1917688482 3295418672 sda2             16.51       323.11       269.54  801326878  668472584 sdb             371.22       945.74      1073.08 2345454041 2661251200 sdb1            371.22       945.72      1073.08 2345398577 2661251200 sdc             407.93       207.00       972.19  513366813 2411036564 sdc1            407.93       206.98       972.19  513311349 2411036564 ..

To execute every 2 seconds for a total of 3 times, do the following.

  $ iostat 2 3

10. iostat – Display LVM statistic (and version)

To display the LVM statistics use option -N as shown below.

  $ iostat -N

To display the version of iostat, use -V. This will really display the version information of sysstat, as iostat is part of sysstat package.

  $ iostat -V sysstat version 7.0.2 (C) Sebastien Godard

VMSTAT EXAMPLES

11. vmstat – Basic example

vmstat by default will display the memory usage (including swap) as shown below.

  $ vmstat procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st  0  0 305416 260688  29160 2356920    2    2     4     1    0    0  6  1 92  2  0

vmstat output contains the following fields:

  • Procs – r: Total number of processes waiting to run
  • Procs – b: Total number of busy processes
  • Memory – swpd: Used virtual memory
  • Memory – free: Free virtual memory
  • Memory – buff: Memory used as buffers
  • Memory – cache: Memory used as cache.
  • Swap – si: Memory swapped from disk (for every second)
  • Swap – so: Memory swapped to disk (for every second)
  • IO – bi: Blocks in. i.e blocks received from device (for every second)
  • IO – bo: Blocks out. i.e blocks sent to the device (for every second)
  • System – in: Interrupts per second
  • System – cs: Context switches
  • CPU – us, sy, id, wa, st: CPU user time, system time, idle time, wait time

12. vmstat – Display active and inactive memory

By default vmstat doesn't display this information. Use option -a, to display active and inactive memory information as shown below.

  $ 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 305416 253820 1052680 2688928    2    2     4     1    0    0  6  1 92  2  0 

13. vmstat – Display number of forks since last boot

This displays all the fork system calls made by the system since the last boot. This displays all fork, vfork, and clone system call counts.

  $ vmstat -f      81651975 forks

14. vmstat – Execute Every x seconds (for y number of times)

To execute every 2 seconds, do the following. You have to press Ctrl-C to stop this.

  $ vmstat 2 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st  1  0      0 537144 182736 6789320    0    0     0     0    1    1  0  0 100  0  0  0  0      0 537004 182736 6789320    0    0     0     0   50   32  0  0 100  0  0 ..

To execute every 2 seconds for 10 times, do the following. You don't need to press Ctrl-C in this case. After executing 10 times, it will stop automatically.

  $ vmstat 2 10 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st  1  0      0 537144 182736 6789320    0    0     0     0    1    1  0  0 100  0  0  0  0      0 537004 182736 6789320    0    0     0     0   50   32  0  0 100  0  0 ..

15. vmstat – Display timestamp

When you use vmstat to monitor the memory usage repeately, it would be nice to see the timestap along with every line item. Use option -t to display the time stamp as shown below.

  $ vmstat -t 1 100 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st  0  0      0 3608728 148368 3898200    0    0     0     0    1    1  0  0 100  0  0     2011-07-09 21:16:28 PDT  0  0      0 3608728 148368 3898200    0    0     0     0   60   15  0  0 100  0  0     2011-07-09 21:16:29 PDT  0  0      0 3608712 148368 3898200    0    0     0     0   32   28  0  0 100  0  0     2011-07-09 21:16:30 PDT

For me, the timestamp option worked in the following version.

  $ vmstat -V procps version 3.2.8

Note: If you use a older version of vmstat, option -t might not be available. In that case, use the method we suggested earlier to display timestamp in vmstat output.

16. vmstat – Display slab info

Use option -m, to display the slab info as shown below.

  $ vmstat -m Cache                       Num  Total   Size  Pages fib6_nodes                    5    113     32    113 ip6_dst_cache                 4     15    256     15 ndisc_cache                   1     15    256     15 RAWv6                         7     10    768      5 UDPv6                         0      0    640      6 tw_sock_TCPv6                 0      0    128     30 ...

17. vmstat – Display statistics in a table format

Instead of displays the values in the record format, you can display the output of vmstat in table format using option -s as shown below.

  $ vmstat -s       4149928  total memory       3864824  used memory       2606664  active memory       1098180  inactive memory        285104  free memory         19264  buffer memory       2326692  swap cache       4192956  total swap        274872  used swap       3918084  free swap    1032454000 non-nice user cpu ticks         14568 nice user cpu ticks      89482270 system cpu ticks   16674327143 idle cpu ticks     368965706 IO-wait cpu ticks       1180468 IRQ cpu ticks ..

18. vmstat – Display disk statistics

Use option -d to display the disk statistics as shown below. This displays the reads, writes, and I/O statistics of the disk.

  $ vmstat -d disk- ------------reads------------ ------------writes----------- -----IO------        total merged sectors      ms  total merged sectors      ms    cur    sec sda   153189971 69093708 2719150864 737822879 329617713 157559204 3965687592 4068577985      0 1102243 sdb   501426305 97099356 2345472425 731613156 419220973 533565961 2661869460 1825174087      0 1510434 sdc   884213459 22078974 513390701 452540172 127474901 8993357 2411187300 2133226954      0 1569758

19. vmstat – Increase the width of the display

The default output without increasing the width is shown below.

  $ vmstat 1 3 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st  0  0      0 3608688 148368 3898204    0    0     0     0    1    1  0  0 100  0  0  0  0      0 3608804 148368 3898204    0    0     0     0   72   30  0  0 100  0  0  0  0      0 3608804 148368 3898204    0    0     0     0   60   27  0  0 100  0  0

Use option -w to increase the width of the output columns as shown below. This give better readability.

  $ vmstat -w 1 3 procs -------------------memory------------------ ---swap-- -----io---- --system-- -----cpu-------  r  b       swpd       free       buff      cache   si   so    bi    bo   in   cs  us sy  id wa st  0  0          0    3608712     148368    3898204    0    0     0     0    1    1   0  0 100  0  0  0  0          0    3608712     148368    3898204    0    0     0     0   93   23   0  0 100  0  0  0  0          0    3608696     148368    3898204    0    0     0     0   35   34   0  0 100  0  0

20. vmstat – Display statistics for a partition

To display the disk I/O statistics of a specific disk partition use option -p as shown below.

  $ vmstat -p sdb1 sdb1          reads   read sectors  writes    requested writes            501423248 2345417917  419221612 2661885948

21. vmstat – Display in MB

By default vmstat displays the memory information in kb. To disply in MB, use the option "-S m" as shown below.

  $ vmstat -S m procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st  0  0    281    288     19   2386    0    0     4     1    0    0  6  1 92  2  0

MPSTAT EXAMPLES

22. mpstat – Display basic info

By default mpstat displays CPU statistics as shown below.

  $ mpstat Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011  10:25:32 PM  CPU   %user   %nice    %sys %iowait    %irq   %soft  %steal   %idle    intr/s 10:25:32 PM  all    5.68    0.00    0.49    2.03    0.01    0.02    0.00   91.77    146.55

23. mpstat – Display all information

Option -A, displays all the information that can be displayed by the mpstat command as shown below. This is really equalivalent to "mpstat -I ALL -u -P ALL" command.

  $ mpstat -A Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011      _x86_64_        (4 CPU)  10:26:34 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle 10:26:34 PM  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.99 10:26:34 PM    0    0.01    0.00    0.01    0.01    0.00    0.00    0.00    0.00   99.98 10:26:34 PM    1    0.00    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98 10:26:34 PM    2    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00 10:26:34 PM    3    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00  10:26:34 PM  CPU    intr/s 10:26:34 PM  all     36.51 10:26:34 PM    0      0.00 10:26:34 PM    1      0.00 10:26:34 PM    2      0.04 10:26:34 PM    3      0.00  10:26:34 PM  CPU     0/s     1/s     8/s     9/s    12/s    14/s    15/s    16/s    19/s    20/s    21/s    33/s   NMI/s   LOC/s   SPU/s   PMI/s   PND/s   RES/s   CAL/s   TLB/s   TRM/s   THR/s   MCE/s   MCP/s   ERR/s   MIS/s 10:26:34 PM    0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    7.47    0.00    0.00    0.00    0.00    0.02    0.00    0.00    0.00    0.00    0.00    0.00    0.00 10:26:34 PM    1    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    4.90    0.00    0.00    0.00    0.00    0.03    0.00    0.00    0.00    0.00    0.00    0.00    0.00 10:26:34 PM    2    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.04    0.00    0.00    0.00    0.00    0.00    3.32    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00 10:26:34 PM    3    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    4.17    0.00    0.00    0.00    0.00    0.01    0.00    0.00    0.00    0.00    0.00    0.00    0.00

24. mpstat – Display CPU statistics of individual CPU (or) Core

Option -P ALL, displays all the individual CPUs (or Cores) along with its statistics as shown below.

  $ mpstat -P ALL Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011      _x86_64_        (4 CPU)  10:28:04 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle 10:28:04 PM  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.99 10:28:04 PM    0    0.01    0.00    0.01    0.01    0.00    0.00    0.00    0.00   99.98 10:28:04 PM    1    0.00    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98 10:28:04 PM    2    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00 10:28:04 PM    3    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00 

To display statistics information of a particular CPU (or core), use option -P as shown below.

  $ mpstat -P 0 Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011      _x86_64_        (8 CPU)  10:28:53 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle 10:28:53 PM    0    0.01    0.00    0.01    0.01    0.00    0.00    0.00    0.00   99.98  $ mpstat -P 1 Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011      _x86_64_        (8 CPU)  10:28:55 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle 10:28:55 PM    1    0.00    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98

Finally, as we mentioned earlier mpstat is part of the sysstat package. When you do mpstat -V, it will really display the version number of the systat package as shown below.

  $ mpstat -V sysstat version 9.0.4 (C) Sebastien Godard (sysstat  orange.fr)