gpt4 book ai didi

仅包含指针的对象的 c++ 未使用内存,如何释放?

转载 作者:行者123 更新时间:2023-11-30 03:10:24 26 4
gpt4 key购买 nike

我刚刚编写了一些处理大型对象的线程代码。即使我清理了一个对象的所有内容(不删除它),它也会继续占用 GB 的 RAM。仅供引用,我在较小的环境中重现了这个问题。

此代码创建一个结构和一个包含指向该结构的指针 vector 的对象。然后我用一些指向结构(用 new 创建)的指针填充对象。我想对象应该只拥有指针的大小,而不是所有指向结构的大小,但是当我运行代码时,对象的大小使用了 300mb。

当我删除 vector 的所有成员然后清理 vector 时,占用的内存(但现在未使用)仍然很高。释放此内存的唯一方法似乎是删除包含 vector 的整个对象。如果它只是一个指针 vector ,为什么 vector 会占用这么多内存?如何在不必删除并重新创建对象的情况下释放它?

函数 -> void f_create_heapCleaner_string 创建一个字符串然后将其删除。有时堆会被这个技巧清理干净。

#include <string>
#include <malloc.h>

#include <vector>
#include <iostream>
using namespace std;

struct struct_test_struct {
string s_internal_data;
};

struct struct_test_struct* BASEDATA_struct_test_struct;

class objhandle_TestStruct__class {
public:
vector<struct struct_test_struct *> v_pointer_TestStruct;

unsigned int ui_v_size;

objhandle_TestStruct__class() {
ui_v_size = 3000;
}

void f_create_heapCleaner_string() {
string * s_tmp = new string();
(*s_tmp).assign(1000000, '*');
(*s_tmp) = "";
delete s_tmp;
}

void f_create_vector(unsigned int ui_size_str) {
cout << " f_create_vector() start " << endl;
malloc_stats();
for (unsigned int ui = 0; ui < ui_v_size; ui++) {
struct struct_test_struct * tmp_newstruct = new struct_test_struct();
(*tmp_newstruct).s_internal_data.assign((ui_size_str + ui), '*');
v_pointer_TestStruct.push_back(tmp_newstruct);
}
cout << " f_create_vector() end " << endl;
malloc_stats();
}

void f_delete_vector_content() {
cout << " f_delete_vector_content() start " << endl;
malloc_stats();
for (unsigned int ui = 0; ui < ui_v_size; ui++) {
delete v_pointer_TestStruct[ui];
}
f_create_heapCleaner_string();
cout << " f_delete_vector_content() end " << endl;
malloc_stats();
}

void f_clear_vector() {
cout << " f_clear_vector() start " << endl;
malloc_stats();
v_pointer_TestStruct.clear();
f_create_heapCleaner_string();
cout << " f_clear_vector() end " << endl;
malloc_stats();
}

void f_RUN_FULL_TEST(unsigned int ui_size_str) {
cout << " .... start test with string size of = " << ui_size_str << endl;
f_create_vector(ui_size_str);
f_delete_vector_content();
f_clear_vector();

}
};

int main(int argc, char**argv) {
objhandle_TestStruct__class * ptr_objhandle_TestStruct__class = new objhandle_TestStruct__class();
(*ptr_objhandle_TestStruct__class).f_RUN_FULL_TEST(100000);
(*ptr_objhandle_TestStruct__class).f_RUN_FULL_TEST(10000);
cout << " DELETE OBJECT start " << endl;
malloc_stats();
delete ptr_objhandle_TestStruct__class;
cout << " DELETE OBJECT finished " << endl;
malloc_stats();

return 0;
}

--- 编译:g++ -o 测试.cc

然后./一个

输出:

    .... start test with string size of = 100000
f_create_vector() start
Arena 0:
system bytes = 135168
in use bytes = 48
Total (incl. mmap):
system bytes = 135168
in use bytes = 48
max mmap regions = 0
max mmap bytes = 0
f_create_vector() end
Arena 0:
system bytes = 309997568
in use bytes = 309972064
Total (incl. mmap):
system bytes = 309997568
in use bytes = 309972064
max mmap regions = 0
max mmap bytes = 0
f_delete_vector_content() start
Arena 0:
system bytes = 309997568
in use bytes = 309972064
Total (incl. mmap):
system bytes = 309997568
in use bytes = 309972064
max mmap regions = 0
max mmap bytes = 0
f_delete_vector_content() end
Arena 0:
system bytes = 309997568
in use bytes = 32832
Total (incl. mmap):
system bytes = 309997568
in use bytes = 32832
max mmap regions = 0
max mmap bytes = 0
f_clear_vector() start
Arena 0:
system bytes = 309997568
in use bytes = 32832
Total (incl. mmap):
system bytes = 309997568
in use bytes = 32832
max mmap regions = 0
max mmap bytes = 0
f_clear_vector() end
Arena 0:
system bytes = 309997568
in use bytes = 32832
Total (incl. mmap):
system bytes = 309997568
in use bytes = 32832
max mmap regions = 0
max mmap bytes = 0
.... start test with string size of = 10000
f_create_vector() start
Arena 0:
system bytes = 309997568
in use bytes = 32832
Total (incl. mmap):
system bytes = 309997568
in use bytes = 32832
max mmap regions = 0
max mmap bytes = 0
f_create_vector() end
Arena 0:
system bytes = 309997568
in use bytes = 40094656
Total (incl. mmap):
system bytes = 309997568
in use bytes = 40094656
max mmap regions = 0
max mmap bytes = 0
f_delete_vector_content() start
Arena 0:
system bytes = 309997568
in use bytes = 40094656
Total (incl. mmap):
system bytes = 309997568
in use bytes = 40094656
max mmap regions = 0
max mmap bytes = 0
f_delete_vector_content() end
Arena 0:
system bytes = 250077184
in use bytes = 32832
Total (incl. mmap):
system bytes = 250077184
in use bytes = 32832
max mmap regions = 0
max mmap bytes = 0
f_clear_vector() start
Arena 0:
system bytes = 250077184
in use bytes = 32832
Total (incl. mmap):
system bytes = 250077184
in use bytes = 32832
max mmap regions = 0
max mmap bytes = 0
f_clear_vector() end
Arena 0:
system bytes = 250077184
in use bytes = 32832
Total (incl. mmap):
system bytes = 250077184
in use bytes = 32832
max mmap regions = 0
max mmap bytes = 0
DELETE OBJECT start
Arena 0:
system bytes = 250077184
in use bytes = 32832
Total (incl. mmap):
system bytes = 250077184
in use bytes = 32832
max mmap regions = 0
max mmap bytes = 0
DELETE OBJECT finished
Arena 0:
system bytes = 135168
in use bytes = 0
Total (incl. mmap):
system bytes = 135168
in use bytes = 0
max mmap regions = 0
max mmap bytes = 0

谢谢,弗朗切斯科

----------------编辑在对象和结构之间使用和对象容器,这在删除时释放内存.. 结构 struct_test_struct { 字符串 s_internal_data; };

class objstruct_test_struct_OWNER {
public:
vector<struct struct_test_struct *> v_pointer_TestStruct;
};
class objhandle_TestStruct__class {
public:

class objstruct_test_struct_OWNER * ptr_OBJ;

unsigned int ui_v_size;

objhandle_TestStruct__class() {
ui_v_size = 3000;
}
.........
void f_create_vector(unsigned int ui_size_str) {
.....
ptr_OBJ = new objstruct_test_struct_OWNER();
cout << " f_create_vector() start " << endl;
malloc_stats();
for (unsigned int ui = 0; ui < ui_v_size; ui++) {
struct struct_test_struct * tmp_newstruct = new struct_test_struct();
(*tmp_newstruct).s_internal_data.assign((ui_size_str + ui), '*');
(*ptr_OBJ).v_pointer_TestStruct.push_back(tmp_newstruct);
}
.........
void f_clear_vector() {
.........
delete ptr_OBJ;
.........

这样程序就可以运行了,这是输出

 .... start test with string size of = 100000
f_create_vector() start
Arena 0:
system bytes = 135168
in use bytes = 64
Total (incl. mmap):
system bytes = 135168
in use bytes = 64
max mmap regions = 0
max mmap bytes = 0
f_create_vector() end
Arena 0:
system bytes = 309997568
in use bytes = 309972080
Total (incl. mmap):
system bytes = 309997568
in use bytes = 309972080
max mmap regions = 0
max mmap bytes = 0
f_delete_vector_content() start
Arena 0:
system bytes = 309997568
in use bytes = 309972080
Total (incl. mmap):
system bytes = 309997568
in use bytes = 309972080
max mmap regions = 0
max mmap bytes = 0
f_delete_vector_content() end
Arena 0:
system bytes = 309997568
in use bytes = 32848
Total (incl. mmap):
system bytes = 309997568
in use bytes = 32848
max mmap regions = 0
max mmap bytes = 0
f_clear_vector() start
Arena 0:
system bytes = 309997568
in use bytes = 32848
Total (incl. mmap):
system bytes = 309997568
in use bytes = 32848
max mmap regions = 0
max mmap bytes = 0
f_clear_vector() end
Arena 0:
system bytes = 135168
in use bytes = 32
Total (incl. mmap):
system bytes = 135168
in use bytes = 32
max mmap regions = 1
max mmap bytes = 1007616
.... start test with string size of = 10000
f_create_vector() start
Arena 0:
system bytes = 135168
in use bytes = 64
Total (incl. mmap):
system bytes = 135168
in use bytes = 64
max mmap regions = 1
max mmap bytes = 1007616
f_create_vector() end
Arena 0:
system bytes = 40161280
in use bytes = 40094816
Total (incl. mmap):
system bytes = 40161280
in use bytes = 40094816
max mmap regions = 1
max mmap bytes = 1007616
f_delete_vector_content() start
Arena 0:
system bytes = 40161280
in use bytes = 40094816
Total (incl. mmap):
system bytes = 40161280
in use bytes = 40094816
max mmap regions = 1
max mmap bytes = 1007616
f_delete_vector_content() end
Arena 0:
system bytes = 40161280
in use bytes = 32848
Total (incl. mmap):
system bytes = 40161280
in use bytes = 32848
max mmap regions = 1
max mmap bytes = 1007616
f_clear_vector() start
Arena 0:
system bytes = 40161280
in use bytes = 32848
Total (incl. mmap):
system bytes = 40161280
in use bytes = 32848
max mmap regions = 1
max mmap bytes = 1007616
f_clear_vector() end
Arena 0:
system bytes = 1138688
in use bytes = 32
Total (incl. mmap):
system bytes = 1138688
in use bytes = 32
max mmap regions = 1
max mmap bytes = 1007616
DELETE OBJECT start
Arena 0:
system bytes = 1138688
in use bytes = 32
Total (incl. mmap):
system bytes = 1138688
in use bytes = 32
max mmap regions = 1
max mmap bytes = 1007616
DELETE OBJECT finished
Arena 0:
system bytes = 1138688
in use bytes = 0
Total (incl. mmap):
system bytes = 1138688
in use bytes = 0
max mmap regions = 1
max mmap bytes = 1007616

我只在这里展示的问题是,对象似乎为自己保留分配的内存,直到不被删除,所以似乎为我的设备释放内存的唯一方法是将数据放入另一个子对象并删除它..

-------------------- 最后..我发现当程序保留一部分内存映射以供将来使用时,众所周知,这会被重新使用,这没关系。我的多线程程序的问题是 malloc 创建这些“Arenas”,当 2 malloc在同一时刻在一个大对象 malloc 中被调用,创建另一个“Arena”,为其保留一个新的内存映射。在我的程序中,我终于结束了没有空闲 ram,4 个“Arenas”每个映射了超过 3gb 的 ram ,但实际上每次使用不到 100mb!所以问题是内存映射(~不可能手动释放)和对内存的线程访问将这个未使用的 ram 乘以这些“Arenas”,所以我在访问这些对象时为所有线程创建了一个 mutex_lock,所以它们被保存在没有“浪费”内存(映射但未使用)到多个竞技场的相同竞技场..

我希望我已经解释了一些我的问题和解决方案..希望这可以帮助别人 ;)再次感谢你,弗朗切斯科

-----我还在测试.​​.我也看到了这个 http://google-perftools.googlecode.com/svn/trunk/doc/tcmalloc.html他们确切地说出我的问题是什么……“在 ptmalloc2 中,内存永远不能从一个领域移动到另一个领域。这会导致大量空间浪费。”并创建一个不同的内存分配器,这应该会有所帮助……

最佳答案

如果没有 malloc_stats 的源代码,就很难确切地知道您在报告什么。

然而,需要考虑的一件事是,如果您正在报告系统页面,即使您释放了 C/C++ 使用的内存(使用 delete 或 free),这些页面也不会被释放。原因是当您使用 new/malloc 分配内存时,C 运行时库将请求一段系统内存,然后 segmentation 该内存块以满足您的请求。当你分配一大块内存时也是如此。 C RTL 将分配一大块系统内存,将该内存添加到其已知的可用 C RTL 内存块列表中,然后返回一个指向一大块内存的指针。

这里的关键是,对于大多数标准分配器(不是全部,甚至可能不是您正在使用的分配器),当您释放内存时,该内存不会返回给操作系统,而是保留在列表中用于 future 新闻和 mallocs 的可用内存。

因此,如果您以系统页面的形式显示可用内存,那么在您使用空闲内存时您不会看到该数字下降。

关于仅包含指针的对象的 c++ 未使用内存,如何释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3230098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com