STL容器内存问题 [英] STL containers memory issue
问题描述
我在linux(Fedora 10和CentOS 5)中使用gcc 4.3.2和使用STL容器实现自己的图形库,然后我发现了一些内存问题。当我建立我的图表,我使用足够的内存很多,以查看顶部或另一个内存使用工具。我确信我解除分配的内存(我一次又一次检查代码,我使用valgrind检查内存泄漏),但内存仍在使用(我可以在顶部或 cat / proc / meminfo ),当我再次创建图形时,它不会增加内存使用,显然重用分配的内存。
I'm implementing my own graph library in linux (Fedora 10 and CentOS 5) with gcc 4.3.2 and using STL containers then I found some problems with memory. When I build my graph, I use a lot of memory enough to be view in top or another memory usage tool. I'm sure that I'm deallocating that memory (I reviewed the code again and again and I used valgrind to check for memory leak), but the memory remains in use (I can view this in top or cat /proc/meminfo) and when I create the graph once again, it does not increase memory usage, apparently reusing the allocated memory.
经过几天的调试,我创建了一个非常简单的代码,具有相同的问题。
After several days of debugging, I created a very simple code that has the same problem.
#include <iostream>
#include <list>
// Object that occupies 128KB.
// Data is not important.
class MyObject
{
public:
int * a;
int * b;
int * c;
int * d;
MyObject( )
{
a = new int[ 8192 ];
b = new int[ 8192 ];
c = new int[ 8192 ];
d = new int[ 8192 ];
}
MyObject( const MyObject & m )
{
a = new int[ 8192 ];
b = new int[ 8192 ];
c = new int[ 8192 ];
d = new int[ 8192 ];
}
~MyObject( )
{
delete [] a;
delete [] b;
delete [] c;
delete [] d;
}
void operator=( const MyObject &m )
{
//Do nothing.
}
};
typedef std::list< MyObject > list_t;
#define MB_TO_ALLOC 1000 // Size in MB that the program must alloc.
#define SLEEP_TIME 5 // Time in seconds that the program must wait until go to another step.
// It's used to give sufficient time for tools update the memory usage
int main( )
{
std::cout << "Alloc..." << std::endl;
list_t * list = new list_t( );
// Number of objects for alloc MB_TO_ALLOC amount of memory
int nObjects = MB_TO_ALLOC * 1024 / 128;
for( int i = 0; i < nObjects; ++i )
list->push_back( MyObject( ) );
std::cout << SLEEP_TIME << "s to Dealloc..." << std::endl;
// Wait some time for a tool (like top) to update the memory usage
sleep( SLEEP_TIME );
std::cout << "Dealloc..." << std::endl;
delete list;
std::cout << SLEEP_TIME << "s to Alloc..." << std::endl;
// Wait some time for a tool (like top) to update the memory usage
sleep( SLEEP_TIME );
//Repeats the procedure for evaluating the reuse of memory
std::cout << "Alloc..." << std::endl;
list = new list_t( );
for( int i = 0; i < nObjects; ++i )
list->push_back( MyObject( ) );
std::cout << SLEEP_TIME << "s to Dealloc..." << std::endl;
sleep( SLEEP_TIME );
delete list;
}
我尝试使用简单数组或我自己的列表类,但在这些情况下,内存被正常释放。
I tried to use simple array or my own list class, but in these cases, the memory is deallocated normally.
有人知道发生了什么吗?如何防止此内存被保留?
Does anyone know what's going on? How to prevent this memory to be "reserved"?
谢谢!
-
布鲁诺Caponi
-- Bruno Caponi
推荐答案
gcc STL有自己的内存管理层,抓取大块内存,有一个env变量可以设置为使用原始新调用
gcc STL has its own memory managment layer that grabs big chunks of memory and doesn't give them back ; there is a env variable you can set to make it use raw new calls
GLIBCPP_FORCE_NEW=1
我认为这也使它免费。这个env var通常在使用valgrind时使用,这样valgrind不会认为事情泄露了
I assume this makes it free it too. This env var is typically used when using valgrind so that valgrind doesnt think things leaked
这篇关于STL容器内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!