Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

c - 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).

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.

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. I am interested both in answers which address the this fact, and in answers which ignore it.
question from:https://stackoverflow.com/questions/66062282/how-do-i-obtain-the-current-size-of-my-heap-from-within-my-program

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

On Linux with glibc, you can use malloc_info() to get heap usage statisics:

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. The string is printed on the file stream stream. The exported string includes information about all arenas (see malloc(3)).

As currently implemented, options must be zero.

That produces an XML document that you have to parse. But you might be able to use mallinfo() to get heap usage statistics (but see the BUGS section):

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.

Note that not all allocations are visible to mallinfo(); see BUGS and consider using malloc_info(3) instead.

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. Allocations in other arenas are excluded. See malloc_stats(3) and malloc_info(3) for alternatives that include information about other arenas.

The fields of the mallinfo structure are typed as int. However, because some internal bookkeeping values may be of type long, the reported values may wrap around zero and thus be inaccurate.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...