OpenVZ查看真实内存使用值

Posted by 冰河 at 15:22 Add comments 14,926 Views
052012

转载一篇不错的文章

OpenVZ的内存模型和Xen或者一般的Linux Kernel不同,导致free命令看不到真实内存使用的值。
OpenVZ上free看到的是虚拟环境(Virtual Environment, VE)中所有程序向系统声明的可能要求内存的最大值,比如C程序:

  1. int *p = malloc(1024*1024*sizeof(int));

向系统声明需要4M内存,但内存实际上并不是在程序声明需要内存时就分配,而是在实际使用时分配,即上面的程序不会让linux内核立即分配内存。
但是OpenVZ提供的/proc/user_beancounters却可以反映出VE实际使用了多少内存,而且,可以反映出有多少是在母鸡的swap里面的,从一定程度上可以判断母鸡是否超售了。
/proc/user_beancounters里面的physpages对应的held值是VE实际使用的物理内存(以4k为单位),oomguarpages则是VE实际使用的物理内存+swap,两者相减,就是VE使用的swap。
HostingFu专门写了一段程序来把结果打印的漂亮一点:http://hostingfu.com/article/vzf … ge-inside-openvz-ve
想在OpenVZ上实验一下的可以执行(需要系统有gcc)

  1. $ wget -O – https://raw.githubusercontent.com/lowendbox/vzfree/master/vzfree.c 2>/dev/null | gcc -o vzfree -x c -
  2. $ sudo ./vzfree

输出结果形如:

[root@ovz home]# /root/vzfree/vzfree
Total              Used        Free
Kernel:     2048.00M      3.33M   2044.67M
Allocate:    256.00M    173.09M     82.91M (128M Guaranteed)
Commit:      128.00M    137.27M     -9.27M (77.4% of Allocated)
Swap:                     0.00M            (0.0% of Committed)

输出中Kernel对应的是VE中所有程序占用的内核内存,Commit是VE使用的物理内存+swap,Swap对应VE使用的swap。

参考链接:http://www.hostloc.com/thread-53552-1-1.html

相关日志

One Response to “OpenVZ查看真实内存使用值”

  1. 回来陪我买Iphone

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Protected by WP Anti Spam
© 2009 - 2024 冰河的博客