{
    "mode": "man",
    "parameter": "memusage",
    "section": "1",
    "url": "https://www.chedong.com/phpMan.php/man/memusage/1/json",
    "generated": "2026-10-25T11:27:15Z",
    "synopsis": "memusage [option]... program [programoption]...",
    "sections": {
        "NAME": {
            "content": "memusage - profile memory usage of a program\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "memusage [option]... program [programoption]...\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "memusage  is  a bash script which profiles memory usage of the program, program.  It preloads\nthe libmemusage.so library into the caller's  environment  (via  the  LDPRELOAD  environment\nvariable; see ld.so(8)).  The libmemusage.so library traces memory allocation by intercepting\ncalls  to  malloc(3),  calloc(3),  free(3),  and  realloc(3);  optionally,  calls to mmap(2),\nmremap(2), and munmap(2) can also be intercepted.\n\nmemusage can output the collected data in textual form, or it can  use  memusagestat(1)  (see\nthe  -p  option,  below) to create a PNG file containing graphical representation of the col‐\nlected data.\n",
            "subsections": [
                {
                    "name": "Memory usage summary",
                    "content": "The \"Memory usage summary\" line output by memusage contains three fields:\n\nheap total\nSum of size arguments of all malloc(3) calls, products of  arguments  (nmemb*size)\nof  all calloc(3) calls, and sum of length arguments of all mmap(2) calls.  In the\ncase of realloc(3) and mremap(2), if the new size of an allocation is larger  than\nthe  previous  size,  the sum of all such differences (new size minus old size) is\nadded.\n\nheap peak\nMaximum of all size arguments of malloc(3), all products  of  nmemb*size  of  cal‐\nloc(3),  all  size  arguments  of  realloc(3),  length  arguments  of mmap(2), and\nnewsize arguments of mremap(2).\n\nstack peak\nBefore the first call to any monitored function, the stack pointer  address  (base\nstack  pointer)  is saved.  After each function call, the actual stack pointer ad‐\ndress is read and the difference from the base stack pointer computed.  The  maxi‐\nmum of these differences is then the stack peak.\n\nImmediately  following  this summary line, a table shows the number calls, total memory allo‐\ncated or deallocated, and number of failed calls for each intercepted  function.   For  real‐\nloc(3)  and mremap(2), the additional field \"nomove\" shows reallocations that changed the ad‐\ndress of a block, and the additional \"dec\" field shows reallocations that decreased the  size\nof  the block.  For realloc(3), the additional field \"free\" shows reallocations that caused a\nblock to be freed (i.e., the reallocated size was 0).\n\nThe \"realloc/total memory\" of the table output by memusage does not reflect cases where real‐\nloc(3) is used to reallocate a block of memory to have a smaller size than previously.   This\ncan  cause sum of all \"total memory\" cells (excluding \"free\") to be larger than the \"free/to‐\ntal memory\" cell.\n"
                },
                {
                    "name": "Histogram for block sizes",
                    "content": "The \"Histogram for block sizes\" provides a  breakdown  of  memory  allocations  into  various\nbucket sizes.\n"
                }
            ]
        },
        "OPTIONS": {
            "content": "",
            "subsections": [
                {
                    "name": "-n _ --progname=_",
                    "content": "Name of the program file to profile.\n",
                    "flag": "-n",
                    "long": "--progname",
                    "arg": "_"
                },
                {
                    "name": "-p _ --png=_",
                    "content": "Generate PNG graphic and store it in file.\n",
                    "flag": "-p",
                    "long": "--png",
                    "arg": "_"
                },
                {
                    "name": "-d _ --data=_",
                    "content": "Generate binary data file and store it in file.\n",
                    "flag": "-d",
                    "long": "--data",
                    "arg": "_"
                },
                {
                    "name": "-u --unbuffered",
                    "content": "Do not buffer output.\n",
                    "flag": "-u",
                    "long": "--unbuffered"
                },
                {
                    "name": "-b _ --buffer=_",
                    "content": "Collect size entries before writing them out.\n",
                    "flag": "-b",
                    "long": "--buffer",
                    "arg": "_"
                },
                {
                    "name": "--no-timer",
                    "content": "Disable timer-based (SIGPROF) sampling of stack pointer value.\n",
                    "long": "--no-timer"
                },
                {
                    "name": "-m --mmap",
                    "content": "Also trace mmap(2), mremap(2), and munmap(2).\n\n-?, --help\nPrint help and exit.\n",
                    "flag": "-m",
                    "long": "--mmap"
                },
                {
                    "name": "--usage",
                    "content": "Print a short usage message and exit.\n",
                    "long": "--usage"
                },
                {
                    "name": "-V --version",
                    "content": "Print version information and exit.\n\nThe following options apply only when generating graphical output:\n",
                    "flag": "-V",
                    "long": "--version"
                },
                {
                    "name": "-t --time-based",
                    "content": "Use time (rather than number of function calls) as the scale for the X axis.\n",
                    "flag": "-t",
                    "long": "--time-based"
                },
                {
                    "name": "-T --total",
                    "content": "Also draw a graph of total memory use.\n\n--title=name\nUse name as the title of the graph.\n",
                    "flag": "-T",
                    "long": "--total"
                },
                {
                    "name": "-x _ --x-size=_",
                    "content": "Make the graph size pixels wide.\n",
                    "flag": "-x",
                    "long": "--x-size",
                    "arg": "_"
                },
                {
                    "name": "-y _ --y-size=_",
                    "content": "Make the graph size pixels high.\n",
                    "flag": "-y",
                    "long": "--y-size",
                    "arg": "_"
                }
            ]
        },
        "EXIT STATUS": {
            "content": "The exit status of memusage is equal to the exit status of the profiled program.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "To report bugs, see http://www.gnu.org/software/libc/bugs.html\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "Below  is  a  simple program that reallocates a block of memory in cycles that rise to a peak\nbefore then cyclically reallocating the memory in smaller blocks that return to zero.   After\ncompiling  the program and running the following commands, a graph of the memory usage of the\nprogram can be found in the file memusage.png:\n\n$ memusage --data=memusage.dat ./a.out\n...\nMemory usage summary: heap total: 45200, heap peak: 6440, stack peak: 224\ntotal calls  total memory  failed calls\nmalloc|         1           400             0\nrealloc|        40         44800             0  (nomove:40, dec:19, free:0)\ncalloc|         0             0             0\nfree|         1           440\nHistogram for block sizes:\n192-207             1   2% ================\n...\n2192-2207            1   2% ================\n2240-2255            2   4% =================================\n2832-2847            2   4% =================================\n3440-3455            2   4% =================================\n4032-4047            2   4% =================================\n4640-4655            2   4% =================================\n5232-5247            2   4% =================================\n5840-5855            2   4% =================================\n6432-6447            1   2% ================\n$ memusagestat memusage.dat memusage.png\n",
            "subsections": [
                {
                    "name": "Program source",
                    "content": "#include <stdio.h>\n#include <stdlib.h>\n\n#define CYCLES 20\n\nint\nmain(int argc, char *argv[])\n{\nint i, j;\nsizet size;\nint *p;\n\nsize = sizeof(*p) * 100;\nprintf(\"malloc: %zu\\n\", size);\np = malloc(size);\n\nfor (i = 0; i < CYCLES; i++) {\nif (i < CYCLES / 2)\nj = i;\nelse\nj--;\n\nsize = sizeof(*p) * (j * 50 + 110);\nprintf(\"realloc: %zu\\n\", size);\np = realloc(p, size);\n\nsize = sizeof(*p) * ((j + 1) * 150 + 110);\nprintf(\"realloc: %zu\\n\", size);\np = realloc(p, size);\n}\n\nfree(p);\nexit(EXITSUCCESS);\n}\n"
                }
            ]
        },
        "SEE ALSO": {
            "content": "memusagestat(1), mtrace(1), ld.so(8)\n\nLinux man-pages 6.7                          2023-10-31                                  memusage(1)",
            "subsections": []
        }
    },
    "summary": "memusage - profile memory usage of a program",
    "flags": [
        {
            "flag": "-n",
            "long": "--progname",
            "arg": "_",
            "description": "Name of the program file to profile."
        },
        {
            "flag": "-p",
            "long": "--png",
            "arg": "_",
            "description": "Generate PNG graphic and store it in file."
        },
        {
            "flag": "-d",
            "long": "--data",
            "arg": "_",
            "description": "Generate binary data file and store it in file."
        },
        {
            "flag": "-u",
            "long": "--unbuffered",
            "arg": null,
            "description": "Do not buffer output."
        },
        {
            "flag": "-b",
            "long": "--buffer",
            "arg": "_",
            "description": "Collect size entries before writing them out."
        },
        {
            "flag": "",
            "long": "--no-timer",
            "arg": null,
            "description": "Disable timer-based (SIGPROF) sampling of stack pointer value."
        },
        {
            "flag": "-m",
            "long": "--mmap",
            "arg": null,
            "description": "Also trace mmap(2), mremap(2), and munmap(2). -?, --help Print help and exit."
        },
        {
            "flag": "",
            "long": "--usage",
            "arg": null,
            "description": "Print a short usage message and exit."
        },
        {
            "flag": "-V",
            "long": "--version",
            "arg": null,
            "description": "Print version information and exit. The following options apply only when generating graphical output:"
        },
        {
            "flag": "-t",
            "long": "--time-based",
            "arg": null,
            "description": "Use time (rather than number of function calls) as the scale for the X axis."
        },
        {
            "flag": "-T",
            "long": "--total",
            "arg": null,
            "description": "Also draw a graph of total memory use. --title=name Use name as the title of the graph."
        },
        {
            "flag": "-x",
            "long": "--x-size",
            "arg": "_",
            "description": "Make the graph size pixels wide."
        },
        {
            "flag": "-y",
            "long": "--y-size",
            "arg": "_",
            "description": "Make the graph size pixels high."
        }
    ],
    "examples": [
        "Below  is  a  simple program that reallocates a block of memory in cycles that rise to a peak",
        "before then cyclically reallocating the memory in smaller blocks that return to zero.   After",
        "compiling  the program and running the following commands, a graph of the memory usage of the",
        "program can be found in the file memusage.png:",
        "$ memusage --data=memusage.dat ./a.out",
        "...",
        "Memory usage summary: heap total: 45200, heap peak: 6440, stack peak: 224",
        "total calls  total memory  failed calls",
        "malloc|         1           400             0",
        "realloc|        40         44800             0  (nomove:40, dec:19, free:0)",
        "calloc|         0             0             0",
        "free|         1           440",
        "Histogram for block sizes:",
        "192-207             1   2% ================",
        "...",
        "2192-2207            1   2% ================",
        "2240-2255            2   4% =================================",
        "2832-2847            2   4% =================================",
        "3440-3455            2   4% =================================",
        "4032-4047            2   4% =================================",
        "4640-4655            2   4% =================================",
        "5232-5247            2   4% =================================",
        "5840-5855            2   4% =================================",
        "6432-6447            1   2% ================",
        "$ memusagestat memusage.dat memusage.png",
        "#include <stdio.h>",
        "#include <stdlib.h>",
        "#define CYCLES 20",
        "int",
        "main(int argc, char *argv[])",
        "int i, j;",
        "sizet size;",
        "int *p;",
        "size = sizeof(*p) * 100;",
        "printf(\"malloc: %zu\\n\", size);",
        "p = malloc(size);",
        "for (i = 0; i < CYCLES; i++) {",
        "if (i < CYCLES / 2)",
        "j = i;",
        "else",
        "j--;",
        "size = sizeof(*p) * (j * 50 + 110);",
        "printf(\"realloc: %zu\\n\", size);",
        "p = realloc(p, size);",
        "size = sizeof(*p) * ((j + 1) * 150 + 110);",
        "printf(\"realloc: %zu\\n\", size);",
        "p = realloc(p, size);",
        "free(p);",
        "exit(EXITSUCCESS);"
    ],
    "see_also": [
        {
            "name": "memusagestat",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/memusagestat/1/json"
        },
        {
            "name": "mtrace",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/mtrace/1/json"
        },
        {
            "name": "ld.so",
            "section": "8",
            "url": "https://www.chedong.com/phpMan.php/man/ld.so/8/json"
        }
    ],
    "tldr": {
        "source": "not_found",
        "examples": []
    }
}