简体   繁体   English

如何从我的程序中获取我的堆的当前大小?

[英]How do I obtain the current size of my heap from within my program?

I'm writing a C program running on Linux (with a less-than-10-yro kernel in case it matters).我正在编写一个在 Linux 上运行的 C 程序(如果重要的话,还有不到 10 年的 kernel)。

From within that program, I want to determine what the overall size of my process' heap at some point.在该程序中,我想在某个时候确定我的进程堆的总体大小。

I know I can do this in a round-about way by reading /proc/mypidhere/maps and parsing that - but I want to do it more directly and without messing withe files and strings.我知道我可以通过读取/proc/mypidhere/maps并对其进行解析来以一种迂回的方式做到这一点——但我想更直接地做到这一点,而不会弄乱文件和字符串。

Notes:笔记:

  • I don't need the limit value, I need the current size of the heap.我不需要限制值,我需要堆的当前大小。
  • I realize mmap() 'ed regions can also be part of the heap.我意识到mmap()的区域也可以是堆的一部分。 I am interested both in answers which address the this fact, and in answers which ignore it.我对解决这个事实的答案和忽略它的答案都感兴趣。

On Linux with glibc , you can use malloc_info() to get heap usage statisics:在带有glibc的 Linux 上,您可以使用malloc_info()获取堆使用统计信息:

SYNOPSIS概要

 #include <malloc.h> int malloc_info(int options, FILE *stream);

DESCRIPTION描述

The malloc_info() function exports an XML string that describes the current state of the memory-allocation implementation in the caller. malloc_info() function 导出一个 XML 字符串,该字符串描述调用者中内存分配实现的当前 state。 The string is printed on the file stream stream.该字符串打印在文件 stream stream 上。 The exported string includes information about all arenas (see malloc(3)).导出的字符串包含有关所有 arenas 的信息(请参阅 malloc(3))。

As currently implemented, options must be zero.按照目前的实施,选项必须为零。

That produces an XML document that you have to parse.这会产生一个您必须解析的 XML 文档。 But you might be able to use mallinfo() to get heap usage statistics (but see the BUGS section ):但是您也许可以使用mallinfo()来获取堆使用统计信息(但请参阅BUGS部分):

SYNOPSIS概要

 #include <malloc.h> struct mallinfo mallinfo(void);

DESCRIPTION描述

The mallinfo() function returns a copy of a structure containing information about memory allocations performed by malloc(3) and related functions. mallinfo() function 返回一个结构的副本,其中包含有关 malloc(3) 和相关函数执行的 memory 分配的信息。

Note that not all allocations are visible to mallinfo();请注意,并非所有分配都对 mallinfo() 可见; see BUGS and consider using malloc_info(3) instead.请参阅 BUGS 并考虑改用 malloc_info(3)。

The returned structure is defined as follows:返回的结构体定义如下:

 struct mallinfo { int arena; /* Non-mmapped space allocated (bytes) */ int ordblks; /* Number of free chunks */ int smblks; /* Number of free fastbin blocks */ int hblks; /* Number of mmapped regions */ int hblkhd; /* Space allocated in mmapped regions (bytes) */ int usmblks; /* See below */ int fsmblks; /* Space in freed fastbin blocks (bytes) */ int uordblks; /* Total allocated space (bytes) */ int fordblks; /* Total free space (bytes) */ int keepcost; /* Top-most, releasable space (bytes) */ };

However,然而,

BUGS错误

Information is returned for only the main memory allocation area.仅返回主 memory 分配区域的信息。 Allocations in other arenas are excluded.不包括其他领域的分配。 See malloc_stats(3) and malloc_info(3) for alternatives that include information about other arenas.请参阅 malloc_stats(3) 和malloc_info(3)以获取包含有关其他领域的信息的替代方案。

The fields of the mallinfo structure are typed as int. mallinfo 结构的字段类型为 int。 However, because some internal bookkeeping values may be of type long, the reported values may wrap around zero and thus be inaccurate.但是,由于某些内部簿记值可能是 long 类型,因此报告的值可能会环绕零,因此不准确。

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

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