简体   繁体   English

malloc消耗比进程本身少得多

[英]malloc consumption much less than process itself

I am developing hash table in C using gcc. 我正在使用gcc在C语言中开发哈希表。

for collisions I am using linked list. 对于冲突,我正在使用链表。

when I load ~1 GB file, mallinfo() show: 当我加载〜1 GB文件时,mallinfo()显示:

Total non-mmapped bytes        arena      =      1753260032
# of free chunks               ordblks    =               2
# of free fastbin blocks       smblks     =               0
# of mapped regions            hblks      =               1
Bytes in mapped regions        hblkhd     =         8003584
Max. total allocated space     usmblks    =               0
Free bytes held in fastbins    fsmblks    =               0
Total allocated space          uordblks   =      1753126384
Total free space               fordblks   =          133648
Topmost releasable block       keepcost   =          133072

if I calculate using Excel the memory consumption I get around 2 GB, which is similar to 1.7 GB shown by mallinfo(). 如果我使用Excel计算内存消耗,则大约为2 GB,这与mallinfo()显示的1.7 GB相似。

at the same time, the linux process shown in top or htop uses 5.7 GB. 同时,在top或htop中显示的linux进程使用5.7 GB。

tried jemalloc and tcmalloc, results are more or less similar. 尝试过jemalloc和tcmalloc,结果或多或少相似。 process always uses around 5.7 GB. 进程始终使用大约5.7 GB。

I know malloc must do some linked list of free memory "pieces" or something similar, but 5 GB is way more than 2 GB, even if memory blocks are aligned. 我知道malloc必须做一些空闲内存“片断”的链接列表或类似的东西,但是5 GB远远超过2 GB,即使内存块对齐了。

where the wasted memory go? 浪费的内存在哪里? :-) :-)

In this case mallinfo() gives false information. 在这种情况下,mallinfo()提供错误的信息。

When I add mallinfo() after every 1M allocations, i can clearly see how int is overflown, became negative, then overlflow again and became positive again. 当我在每1M分配之后添加mallinfo()时,我可以清楚地看到int是如何溢出,变为负值,然后再次溢出并再次变为正值。

Excel calculation seems not correct too. Excel计算似乎也不正确。 I added code for calculation of allocated memory, and result is about 4.7 GB. 我添加了用于分配内存计算的代码,结果约为4.7 GB。

There are still 1 GB difference between what top shown and real consumption, but this time it could be easily explain by allocator's internal data 顶部显示和实际消耗之间仍然存在1 GB的差异,但是这次可以通过分配器的内部数据轻松解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM