免费()时使用的内存使用量不会降低 [英] Memory usage doesn't decrease when free() used

查看:98
本文介绍了免费()时使用的内存使用量不会降低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写独立于操作系统的无锁队列,到目前为止,它的伟大工程,但有小问题,内存管理。我不知道如果GCC问题还是我的。问题:内存增加时元素被添加到列表中,但是,当元件被从列表(免费(elementPointer))除去存储器使用不改变。

I am writing OS independent lockless queue, so far it works great, but there is small problem with memory managment. I am not sure if its gcc problem or mine. Problem: Memory increases when element is added to list, but when element is removed from list (free(elementPointer);) memory usage don't change.

但是当我使用pthreads的,N生产者和M消费者(1< N< 20,1< M< 20)内存使用率约为〜10MB所有的时间(试图添加和删除〜10KK元素时),所以看起来像免费工作。

BUT when I use pthreads, N producers and M consumers (1<N<20, 1<M<20) memory usage is about ~10mb all the time (when trying to add and remove ~10kk elements), so looks like free is working.

和有趣的是,在VS 2010(同一code,没有线程)免费做工精细,内存被释放(看任务管理器)。

And funny thing is that in VS 2010 (same code, no threads) free works fine, memory is released (watched task manager).

我做了测试,增加1KK元素,添加毕竟,一个删除了一个所有元素(无螺纹)。

I made test, added 1kk elements, after adding all, removed one by one all elements (no threads).

Linux的 - 0.08秒

Linux - 0.08 seconds

视窗〜57秒。

Linux的(不免费) - 0.07秒

Linux(without free) - 0.07 seconds

视窗(不免费) - 0.9秒。

Windows(without free) - 0.9 seconds

所以,问题是,为什么内存没有在Linux下C释放的时候没有线程使用吗?
如果需要的话我可以张贴code。

So, the question is, why memory isn't freed in Linux C when no threads are used ? I can post code if necessary .

GCC版本:4.4.3

GCC version: 4.4.3

推荐答案

在许多操作系统,免费()不使其可用于操作系统的内存再次,但只是新呼叫的malloc()。这就是为什么你没有看到内存使用率下降外,但是当你用穿线增加新的分配数量,内存被重新使用,所以总使用通过屋顶不走了。

On many operating systems, free() doesn't make the memory available for the OS again, but "only" for new calls to malloc(). This is why you don't see the memory usage go down externally, but when you increase the number of new allocations by threading, the memory is re-used so total usage doesn't go through the roof.

这篇关于免费()时使用的内存使用量不会降低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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