Chinaunix首页 | 论坛 | 博客
  • 博客访问: 200786
  • 博文数量: 489
  • 博客积分: 410
  • 博客等级: 下士
  • 技术积分: 2590
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-01 22:50
文章分类

全部博文(489)

文章存档

2011年(489)

我的朋友

分类:

2011-09-04 12:32:28

原文地址:别让虚拟内存吓了你 作者:agu9899

前段时间要在我们的产品上加上提示框(notification box)在ubuntu上使用的是libnotify,一般性应用代码如下:


/*
 * @file tests/test-basic.c Unit test: basics
 *
 * @Copyright (C) 2004 Mike Hearn
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#include <libnotify/notify.h>
#include <stdio.h>
#include <unistd.h>

int main() {
    NotifyNotification *n;

    notify_init("Basics");

    n = notify_notification_new ("Summary",
                                     "Content that is very long",
                                     NULL, NULL);
        notify_notification_set_timeout (n, 3000); //3 seconds


    if (!notify_notification_show (n, NULL)) {
        fprintf(stderr, "failed to send notification\n");
        return 1;
    }

    g_object_unref(G_OBJECT(n));

    return 0;
}


在我们的实际项目中也就是类似这样使用的。其实libnotify分为两部分,一部分为对外的接口API,另一部分为应用程序notify-send.libnotify 其实只是发送消息的一个通道,消息窗体的显示,显示时长,以及窗体的消失。则是notification-daemon程序来完成的。libnotify与notification-daemon之间是使用DBUS来通信的。将这两者移植到我们的终端设备上,测试组的同事告诉我,在使用libnotify发送消息前后,notification的内存占用有很大的区别,关键是一旦notification-daemon占了大约17M的内存后就再也没有减下去。

在一个资源本身就是很紧张的手持设备上占去17M的内存空间,这显然是够恐怖的了,更要命的是占了17M的空间就没有减下去。按理说在notification窗体显示完后,窗体部分是自动退出,内存所占空间应该也要释放的。

这是用top指令查看的notification-daemon刚启动时的内存使用情况

top - 11:46:49 up 3:02, 5 users, load average: 0.10, 0.27, 0.33
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
Cpu(s): 17.4%us, 3.7%sy, 0.0%ni, 77.9%id, 0.3%wa, 0.7%hi, 0.0%si, 0.0%st
Mem: 1033520k total, 986528k used, 46992k free, 170704k buffers
Swap: 497972k total, 0k used, 497972k free, 472736k cached

  PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
14305 gzq 20 0 17380 4996 4112 S 0.0 0.5 0:00.02 notification-da

在使用notify-send test 后
再次看下notification-daemon使用情况发现:
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                            
14305 gzq       20   0 32156 9244 7540 S  0.0  0.9   0:00.28 notification-dameon

看到了吧virt 从17380飙到了32156,很紧张,不会吧,这么个小程序会占用这么多的内存?当然不是了。查看top指令参数,其中说到了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.
VIRT = SWAP + RES.
接下来,也介绍了
SWAP  --  Swapped size (kb)
          The swapped out portion of a task's total virtual memory image.

RES  --  Resident size (kb)
          The non-swapped physical memory a task has used.

res 是进程使用的、未被换出的物理内存大小。RES=CODE+DATA


还有个很重要的共享内存 SHR
 SHR  --  Shared Mem size (kb)
          The  amount of shared memory used by a task.  It simply reflects memory that could be potentially shared with other processes.

所以此时根本不能用virt来衡量一个软件运行所占用的内存。在linux 上一般查看内存的使用可以用:
free -m

             total       used       free     shared    buffers     cached
Mem:          1009        818        191          0        130        369
-/+ buffers/cache:        318        691
Swap:          486          0        486
将used的值减去buffer和cache的值就是你当前真实内存使用.

阅读(133) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~