在C题使用的内存 [英] Problem usage memory in C

查看:110
本文介绍了在C题使用的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助:)
操作系统:Linux

凡在睡眠(1000);,此时顶(显示Linux的任务)给我写了7.7%MEM使用。
Valgrind的:未找到内存泄漏。

我明白了,写正确,所有的malloc结果为NULL。
但为什么要在这个时候休眠我的计划不是记忆下降?缺少的是什么?

对不起我的英文不好,谢谢

 
〜#tmp_soft
适用于:它是免费的?没有
它是免费的?是
对于0
适用于:它是免费的?没有
它是免费的?是
1
END:它是免费的?是
结束

〜#top返回
  PID USER PR NI VIRT RES SHR S%CPU%MEM TIME + COMMAND
23060根20 0155米153米448秒0 0 7.7:01.07 tmp_soft

完整的源:tmp_soft.c

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&unistd.h中GT;结构cache_db_s
{
 INT table_update;
 结构cache_db_s * p_next;
};无效free_cache_db(结构cache_db_s ** cache_db)
{
 结构cache_db_s * cache_db_t;
 而(* cache_db!= NULL)
 {
  cache_db_t = * cache_db;
  * cache_db =(* cache_db) - GT; p_next;
  免费(cache_db_t);
  cache_db_t = NULL;
 }
 的printf(它是免费的?%S \\ n,* cache_db == NULL是:不?);
}无效make_cache_db(结构cache_db_s ** cache_db)
{
 结构cache_db_s * cache_db_t = NULL;
 INT N =千万; 的for(int i = 0; i = N时,我++)
 {
  如果((cache_db_t =的malloc(sizeof的(结构cache_db_s)))== NULL){
   的printf(错误:malloc的1 - > cache_db_s(没有空闲内存)\\ n);
   打破;
  }
  memset的(cache_db_t,0,sizeof的(结构cache_db_s));  cache_db_t-> table_update = 1; // TMP  cache_db_t-> p_next = * cache_db;
  * cache_db = cache_db_t;
  cache_db_t = NULL;
 }
}INT主(INT ARGC,字符** argv的)
{
 结构cache_db_s * cache_db = NULL; 为(中间体二= 0;ⅱ2;ⅱ++){
  make_cache_db(安培; cache_db);
  的printf(为:它是免费的?%S \\ n,cache_db == NULL是:不?);
  free_cache_db(安培; cache_db);
  的printf(%d位\\ n,第二章);
 } 的printf(END:它是免费的?%S \\ n,cache_db == NULL是:不?);
 的printf(END \\ n);
 睡眠(1000);
 返回0;
}


解决方案

如果你想建立你的程序是否有内存泄漏,那么不是对于工作的工具( valrind 是)。

显示了OS所看到的内存使用情况。即使你叫免费,也不能保证该释放的内存会得到返回到操作系统。通常情况下,它不会。尽管如此,内存不成为意义上的自由,你的程序可以使用它的后续分配。

修改如果你的的libc 支持的话,你可以尝试用<实验一href=\"http://www.gnu.org/s/libc/manual/html_node/Malloc-Tunable-Parameters.html#Malloc-Tunable-Parameters\"相对=nofollow> M_TRIM_THRESHOLD 。即使你走这条路,这将是棘手的(单块二手坐在靠近堆的顶部将prevent它下面的所有可用内存被释放到OS)。

Please help :) OS : Linux

Where in " sleep(1000);", at this time "top (display Linux tasks)" wrote me 7.7 %MEM use. valgrind : not found memory leak.

I understand, wrote correctly and all malloc result is NULL. But Why in this time "sleep" my program NOT decreased memory ? What missing ?

Sorry for my bad english, Thanks


~ # tmp_soft
For : Is it free??  no
Is it free??  yes
For 0 
For : Is it free??  no
Is it free??  yes
For 1 
END : Is it free??  yes
END 

~ #top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                
23060 root      20   0  155m 153m  448 S    0  7.7   0:01.07 tmp_soft    

Full source : tmp_soft.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

struct cache_db_s
{
 int       table_update;
 struct    cache_db_s * p_next;
};

void free_cache_db (struct cache_db_s ** cache_db)
{
 struct cache_db_s * cache_db_t;
 while (*cache_db != NULL)
 {
  cache_db_t = *cache_db;
  *cache_db = (*cache_db)->p_next;
  free(cache_db_t);
  cache_db_t = NULL;
 }
 printf("Is it free??  %s\n",*cache_db==NULL?"yes":"no");
}

void make_cache_db (struct cache_db_s ** cache_db)
{
 struct cache_db_s * cache_db_t = NULL;
 int n = 10000000;

 for (int i=0; i = n; i++)
 {
  if ((cache_db_t=malloc(sizeof(struct cache_db_s)))==NULL) {
   printf("Error : malloc 1 -> cache_db_s (no free memory) \n");
   break;
  }
  memset(cache_db_t, 0, sizeof(struct cache_db_s));

  cache_db_t->table_update = 1; // tmp 

  cache_db_t->p_next = *cache_db;
  *cache_db = cache_db_t;
  cache_db_t = NULL;
 }
}

int main(int argc, char **argv)
{
 struct cache_db_s * cache_db = NULL;

 for (int ii=0; ii  2; ii++) {
  make_cache_db(&cache_db);
  printf("For : Is it free??  %s\n",cache_db==NULL?"yes":"no");
  free_cache_db(&cache_db);
  printf("For %d \n", ii);
 }

 printf("END : Is it free??  %s\n",cache_db==NULL?"yes":"no");
 printf("END \n");
 sleep(1000);
 return 0;
}

解决方案

If you're trying to establish whether your program has a memory leak, then top isn't the right tool for the job (valrind is).

top shows memory usage as seen by the OS. Even if you call free, there is no guarantee that the freed memory would get returned to the OS. Typically, it wouldn't. Nonetheless, the memory does become "free" in the sense that your process can use it for subsequent allocations.

edit If your libc supports it, you could try experimenting with M_TRIM_THRESHOLD. Even if you do follow this path, it's going to be tricky (a single used block sitting close to the top of the heap would prevent all free memory below it from being released to the OS).

这篇关于在C题使用的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆