mallinfo/malloc



  • Hallo,
    ich würde gern ein paar Infos zur RAM Nutzung in meinem Programm ermitteln.
    Dazu habe ich die Funktion mallinfo() gefunden.

    The mallinfo() function returns a copy of a structure containing
           information about memory allocations performed by malloc(3) and
           related functions.  This 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;   /* Maximum total allocated space (bytes) */
                   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) */
               };
    
           The fields of the mallinfo structure contain the following
           information:
    
           arena     The total amount of memory allocated by means other than
                     mmap(2) (i.e., memory allocated on the heap).  This figure
                     includes both in-use blocks and blocks on the free list.
    
           ordblks   The number of ordinary (i.e., non-fastbin) free blocks.
    
           smblks    The number of fastbin free blocks (see mallopt(3)).
    
           hblks     The number of blocks currently allocated using mmap(2).
                     (See the discussion of M_MMAP_THRESHOLD in mallopt(3).)
    
           hblkhd    The number of bytes in blocks currently allocated using
                     mmap(2).
    
           usmblks   The "highwater mark" for allocated space—that is, the
                     maximum amount of space that was ever allocated.  This
                     field is maintained only in nonthreading environments.
    
           fsmblks   The total number of bytes in fastbin free blocks.
    
           uordblks  The total number of bytes used by in-use allocations.
    
           fordblks  The total number of bytes in free blocks.
    
           keepcost  The total amount of releasable free space at the top of the
                     heap.  This is the maximum number of bytes that could
                     ideally (i.e., ignoring page alignment restrictions, and so
                     on) be released by malloc_trim(3).
    

    Leider werde ich aus diesem Fachenglisch nicht schlau 😕
    Mich würde hauptsächlich der belegte RAM + noch zur Verfügung stehender RAM interessieren.

    mfg



  • Was ist an /* Total allocated space (bytes) */ bzw. /* Total free space (bytes) */ Fachenglisch?

    Ist denn mallinfo auf deinem System vorhanden?



  • Naja, durch die Tatsache, dass diese Werte nach dem Aufruf zuerst 1,6MB und 136KB anzeigen (Div. Dinge wurden bereits initialisiert) und nach dem erstellen von ein paar Klassen 6MB und 2,2Mb anzeigen, bin ich etwas verwirrt und traue diesem "total" nicht ganz.

    Schließlich sollten die 2 Werte addiert ja immer das gleiche Ergebnis liefern.

    mfg


  • Mod

    WSK schrieb:

    Schließlich sollten die 2 Werte addiert ja immer das gleiche Ergebnis liefern.

    Es ist ein Irrtum, anzunehmen, dass dem malloc deiner Programmlaufzeit immer gleich viel Speicher zur Verfügung stünde. Der Gesamtspeicher wird im Laufe der Zeit wachsen, wenn malloc mehr Speicherseiten (oder wie auch immer die zugrundeliegende Implementierung genau funktioniert) anfordert und bekommt. Vergleiche mit:

    arena: The total amount of memory allocated by means other than mmap(2) (i.e., memory allocated on the heap). This figure includes both in-use blocks and blocks on the free list.

    Das sollte also der Summe von freien und belegten Blöcken entsprechen.

    edit: Siehe das Beispielprogramm hier:
    http://man7.org/linux/man-pages/man3/mallinfo.3.html


Anmelden zum Antworten