gpt4 book ai didi

linux - Linux给malloc()分配了多少内存?

转载 作者:太空狗 更新时间:2023-10-29 12:29:50 30 4
gpt4 key购买 nike

这是一道Linux系统题,不是编码题。当我使用“top”检查程序的内存使用情况时,它报告的值是内存分析器 Valgrind 的 Massif 给出的实际堆分配的 3-4 倍。这是一个大程序,相差数百兆。 Valgrind 手册只给出了部分解释:

(Massif) does not directly measure memory allocated with lower-level system calls such as mmap, mremap, and brk.

Heap allocation functions such as malloc are built on top of these system calls. For example, when needed, an allocator will typically call mmap to allocate a large chunk of memory, and then hand over pieces of that memory chunk to the client program in response to calls to malloc et al. Massif directly measures only these higher-level malloc et al calls, not the lower-level system calls.

很好,但是我真的从系统中拿走了多少内存?我需要能够在一台机器上运行这个程序的尽可能多的实例,所以我需要知道还有多少内存可用。页面对齐等无法解释报告的内存使用量中数百兆字节的差异。

此外,什么决定了底层 mmap() 调用的 block 大小?根据顶部,我一次看到 64MB 的 block ,这看起来大得离奇。

最佳答案

任何 malloc 实现都将针对具有大量内存需求的应用程序进行优化,因为低需求的应用程序无论如何都可以正常运行,而且虚拟内存很便宜。

例如,您会发现 malloc 实现将一个内存块用于最多 1024 个最多 16 字节的 malloc,另一个内存块最多使用 1024 个最多 32 字节的 malloc,等等。对于一些 mallocs,这是低效的,但仍然很便宜。有了无数的 malloc,它使 malloc 非常高效。

所以说“4 倍”可能完全没有意义。告诉我们比您想象的多了多少兆字节。

关于linux - Linux给malloc()分配了多少内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30786792/

30 4 0
文章推荐: html - 从另一种风格继承另一种风格
文章推荐: linux - 解压后更改目录
文章推荐: python - 在 QT 中运行应用程序时防止 Python 内核崩溃
文章推荐: javascript - 如何设置
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com