相关疑难解决方法(0)

malloc实现是否会将free-ed内存返回给系统?

我有一个长期存在的应用程序,频繁的内存分配 - 释放.任何malloc实现都会将释放的内存返回给系统吗?

在这方面,什么是以下行为:

  • ptmalloc 1,2(默认为glibc)或3
  • dlmalloc
  • tcmalloc(谷歌线程malloc)
  • solaris 10-11默认malloc和mtmalloc
  • FreeBSD 8默认malloc(jemalloc)
  • Hoard malloc?

更新

如果我有一个应用程序,其白天和夜晚的内存消耗可能非常不同(例如),我可以强制任何malloc将系统释放的内存吗?

如果没有这样的返回,释放的内存将被多次换出,但这样的内存只包含垃圾.

malloc free mmap glibc tcmalloc

54
推荐指数
4
解决办法
2万
查看次数

为什么std :: queue在弹出元素后不缩小内存?

我写了一个使用std :: queue的小程序

queue<double> the_queue;

for(int i = 0; i < 100; i++)
{
    for(int j = 0; j < 5000; j++)
    {
        double d = 1.0 * (rand() % 1000);
        the_queue.push(d);
    }
}

printf("Done pushing\n");

while(!the_queue.empty())
{
    the_queue.pop();
}

printf("Done popping\n");
Run Code Online (Sandbox Code Playgroud)

我将2个断点放在printf("Done pushing\n");并且printf("Done popping\n");在命中断点时检查程序的内存使用情况(在任务管理器中显示).在Done pushing,内存使用量约为34 MB,但Done popping内存使用量仍为~34 MB.这让我感到惊讶!

为什么是这样?有没有办法克服这个问题?

c++ queue

8
推荐指数
2
解决办法
2479
查看次数

malloc_trim(0) 发布 Thread Arenas 的 Fastbins?

在过去一周左右的时间里,我一直在调查应用程序中内存使用量随时间累积的问题。我把它缩小到复制一个的行

std::vector< std::vector< std::vector< std::map< uint, map< uint, std::bitset< N> > > > > >

在工作线程中(我意识到这是一种组织内存的荒谬方式)。工作线程会定期被销毁、重新创建,并在线程启动时复制该内存结构。被复制的原始数据通过主线程的引用传递给工作线程。

使用 malloc_stat 和 malloc_info,我可以看到当工作线程被销毁时,它使用的 arena/heap 将用于该结构的内存保留在其 fastbins 的空闲列表中。这是有道理的,因为有许多小于 64 字节的单独分配。

问题是,当工作线程被重新创建时,它会创建一个新的 arena/heap 而不是重用前一个,这样来自先前 arenas/heap 的 fastbins 永远不会被重用。最终,系统会在重用之前的堆/arena 来重用他们持有的 fastbin 之前耗尽内存。

有点意外,我发现在我的主线程中调用 malloc_trim(0),在加入工作线程后,会导致线程 arenas/heap 中的 fastbins 被释放。据我所知,这种行为没有记录。有人有解释吗?

这是我用来查看此行为的一些测试代码:

// includes
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <mcheck.h>
#include <malloc.h>
#include <map>
#include <bitset>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>

// Number of bits per bitset.
const int sizeOfBitsets …
Run Code Online (Sandbox Code Playgroud)

c++ malloc multithreading glibc

5
推荐指数
1
解决办法
1588
查看次数

调用free()后内存未释放

我有一个简短的程序,通过向其添加节点来生成链接列表,然后释放链接列表分配的内存.

Valgrind不报告任何内存泄漏错误,但该过程继续保持分配的内存.

在将sizeof(structure_name)分配的内存更改为固定数512后,我才能修复错误.(请参阅注释代码)

这是一个错误还是正常的操作?这是代码:

#include <execinfo.h>
#include <stdlib.h>
#include <stdio.h>


typedef struct llist_node {
  int ibody;
  struct llist_node * next;
  struct llist_node * previous;
  struct llist * list;
}llist_node;

typedef struct  llist {
  struct llist_node * head;
  struct llist_node * tail;
  int id;
  int count;
}llist;

llist_node * new_lnode (void) {
  llist_node * nnode = (llist_node *) malloc ( 512 );
  //  llist_node * nnode = (llist_node *) malloc ( sizeof(llist_node) );
  nnode->next = NULL;
  nnode->previous = NULL;
  nnode->list = …
Run Code Online (Sandbox Code Playgroud)

memory malloc free memory-leaks

2
推荐指数
1
解决办法
3435
查看次数

free()没有释放嵌入式linux中的内存.

我在嵌入式Linux中使用malloc()分配了内存(大约10 MB).并检查了空闲内存它是67080 kB但是即使在使用free()释放它之后它仍然是相同的.只有在应用程序终止后,内存才会再次可用.free()不会使释放的内存可用于系统,如果可以,则如何使其可用.

c free embedded-linux

2
推荐指数
1
解决办法
1024
查看次数

我怎样才能保证在释放内存时,操作系统会回收内存以供其使用?

我注意到这个程序:

#include <stdio.h>

int main() {
  const size_t alloc_size = 1*1024*1024;
  for (size_t i = 0; i < 3; i++) {
    printf("1\n");
    usleep(1000*1000);
    void *p[3];
    for (size_t j = 3; j--; )
      memset(p[j] = malloc(alloc_size),0,alloc_size); // memset for de-virtualize the memory
    usleep(1000*1000);
    printf("2\n");
    free(p[i]);
    p[i] = NULL;
    usleep(1000*1000*4);
    printf("3\n");
    for (size_t j = 3; j--; )
      free(p[j]);
  }
}
Run Code Online (Sandbox Code Playgroud)

它分配3个存储器,3次,每次释放不同的存储器,根据存储器释放存储器watch free -m,这意味着free无论存储器在程序地址空间内的位置如何,OS都会为每个存储器回收存储器.我可以以某种方式得到这种效果的保证吗?或者是否已经有类似的东西(比如>64KB分配规则)?

c linux memory memory-management

2
推荐指数
1
解决办法
133
查看次数

为什么free()函数不会将内存返回给操作系统?

当我在Linux上使用顶级终端程序时,我看不到免费的结果.

我的期望是:

  1. 免费地图和清单.

  2. 我可以在顶部看到的内存使用情况(Linux函数)或者/proc/meminfo 比过去更小.

  3. 睡觉就是开始.

  4. 程序退出.

但是,当程序结束时,内存的使用量会变小.

你会解释自由功能的逻辑吗?

以下是我的代码.

for(mapIter = bufMap->begin(); mapIter != bufMap -> end();mapIter++)
{
    list<buff> *buffList = mapIter->second;
    list<buff>::iterator listIter;
    for(listIter = buffList->begin(); listIter != buffList->end();listIter++)
    {
        free(listIter->argu1);
        free(listIter->argu2);
        free(listIter->argu3);
    }
    delete buffList;
}
delete bufMap;

printf("Free Complete!\n");

sleep(10);
printf("endend\n");
Run Code Online (Sandbox Code Playgroud)

谢谢.

c++ linux redhat

0
推荐指数
1
解决办法
336
查看次数