Linux 分析进程动态内存需求的方法

1、使用 mallinfo 接口:

static void display_mallinfo(void)
{
   struct mallinfo mi;

   mi = mallinfo();

   printf("Total non-mmapped bytes (arena):       %d\n", mi.arena);
   printf("# of free chunks (ordblks):            %d\n", mi.ordblks);
   printf("# of free fastbin blocks (smblks):     %d\n", mi.smblks);
   printf("# of mapped regions (hblks):           %d\n", mi.hblks);
   printf("Bytes in mapped regions (hblkhd):      %d\n", mi.hblkhd);
   printf("Max. total allocated space (usmblks):  %d\n", mi.usmblks);
   printf("Free bytes held in fastbins (fsmblks): %d\n", mi.fsmblks);
   printf("Total allocated space (uordblks):      %d\n", mi.uordblks);
   printf("Total free space (fordblks):           %d\n", mi.fordblks);
   printf("Topmost releasable block (keepcost):   %d\n", mi.keepcost);
}

2、使用sbrk(0) 调用得到当前进程的 heap 的 break point.

3、使用 strace -e trace=brk 跟踪进程的 brk 调用.

$strace -e trace=brk your_program

另外使用 -e trace=memory
                   Trace all memory mapping related system calls.

4、排除法

使用setrlimit  设置 RLIMIT_DATA 限制程序的heap 段大小,大致找到哪个值可以正常运行

RLIMIT_DATA
              The maximum size of the process's data segment (initialized data, uninitialized data, and heap).  This limit affects calls to brk(2) and sbrk(2), which fail with the error ENOMEM  upon encountering the soft limit of this resource.
 

struct rlimit memory_limit;
memory_limit.rlim_cur = memory_limit.rlim_max = 1024 * 1024 * 70;
setrlimit(RLIMIT_DATA, &memory_limit);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值