Memory usage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Evangelista Sami

    #1

    Memory usage

    hello all

    i want to know in my program the amount of memory allocated by
    malloc() calls
    so i "overloaded " the malloc functions to increment a counter of
    memory used (no call to free is done).
    this counter is incremented each time a malloc is done by the number
    of requested machine words.
    The problem is that when i do a "ps aux" the amount of memory
    allocated for my program is quite different from the amount i computed
    manually.
    So my question is : what does really do a malloc call?
    for example if i do this

    ptr = malloc(sizeof(c har) * 65);

    am i sure that (assuming a machine word = 4 bytes and 1 char = 1 byte)
    the memory allocated will be of 68 bytes?

    any help would be appreciated


    Sami Evangelista
  • Case

    #2
    Re: Memory usage

    Evangelista Sami wrote:[color=blue]
    > hello all
    >
    > i want to know in my program the amount of memory allocated by
    > malloc() calls
    > so i "overloaded " the malloc functions to increment a counter of
    > memory used (no call to free is done).
    > this counter is incremented each time a malloc is done by the number
    > of requested machine words.
    > The problem is that when i do a "ps aux" the amount of memory
    > allocated for my program is quite different from the amount i computed
    > manually.
    > So my question is : what does really do a malloc call?
    > for example if i do this
    >
    > ptr = malloc(sizeof(c har) * 65);
    >
    > am i sure that (assuming a machine word = 4 bytes and 1 char = 1 byte)
    > the memory allocated will be of 68 bytes?[/color]

    The exact amount depends on the implementation.

    <OT>
    Many implementations of malloc() alocate slightly more for internal
    administration. Often this extra information is placed before the
    address you receive from malloc(). This extra information can be
    the size of the block; things that might help in the design of free().

    Furthermore, malloc() itself, for efficiency, might request memory
    from a lower layer in larger chunks. So with the 'ps' command you
    might observe bumps. Since you seem to be on UNIX, having a look at
    the manual page of sbrk() (and related calls) might be interesting.

    Things like this causes, 'ps' to shows a considereably higher memory
    usage than you expected.
    </OT>

    Case

    Comment

    • Allan Bruce

      #3
      Re: Memory usage


      "Evangelist a Sami" <evangeli@cnam. fr> wrote in message
      news:5f59677c.0 406140255.5b5cf 88c@posting.goo gle.com...[color=blue]
      > hello all
      >
      > i want to know in my program the amount of memory allocated by
      > malloc() calls
      > so i "overloaded " the malloc functions to increment a counter of
      > memory used (no call to free is done).
      > this counter is incremented each time a malloc is done by the number
      > of requested machine words.
      > The problem is that when i do a "ps aux" the amount of memory
      > allocated for my program is quite different from the amount i computed
      > manually.
      > So my question is : what does really do a malloc call?
      > for example if i do this
      >
      > ptr = malloc(sizeof(c har) * 65);
      >
      > am i sure that (assuming a machine word = 4 bytes and 1 char = 1 byte)
      > the memory allocated will be of 68 bytes?
      >
      > any help would be appreciated
      >
      >
      > Sami Evangelista[/color]

      This is completely implementation specific. I know of one system that at
      start of day 'grabs' a chunk of memory (e.g. 1kB) then as you malloc, it
      will use bits of this chunk. If you require more, it gets an exponentially
      bigger chuk from the OS and gives you some of that, so you will be seeing
      your program use a lot more memory than you expect.
      Allan


      Comment

      • Simon Massey

        #4
        Re: Memory usage

        "Evangelist a Sami" <evangeli@cnam. fr> wrote in message
        news:5f59677c.0 406140255.5b5cf 88c@posting.goo gle.com...[color=blue]
        > So my question is : what does really do a malloc call?
        > for example if i do this
        >
        > ptr = malloc(sizeof(c har) * 65);
        >
        > am i sure that (assuming a machine word = 4 bytes and 1 char = 1 byte)
        > the memory allocated will be of 68 bytes?
        >
        > any help would be appreciated[/color]

        It depends upon the way malloc() is actually implemented by your platform's
        library. All that is guaranteed is that the number of bytes you requested
        will be allocated (maybe more, maybe much more, but never less) and that the
        memory pointer returned will be aligned for any type of use on your
        platform. It is quite "normal" for malloc to allocate memory in blocks (e.g.
        multiples of 256 bytes) or increase the size you asked for by the overhead
        of the malloc management header (the pointer to the next allocated block and
        this block's size etc.) which it could store immediately before your "user"
        memory.

        To work out how your platform allocates memory, you could attempt to display
        the void * returned by three consecutive malloc(2); on may platforms these
        will not be 2 bytes different.


        Comment

        • Carlos Martin

          #5
          Re: Memory usage


          "Evangelist a Sami" <evangeli@cnam. fr> wrote in
          news:5f59677c.0 406140255.5b5cf 88c@posting.goo gle.com...[color=blue]
          > am i sure that (assuming a machine word = 4 bytes and 1 char = 1 byte)
          > the memory allocated will be of 68 bytes?[/color]

          It doesn't have to. malloc() is only required (and expected to) allocate
          _at_least_ the amount you ask it.
          The reason is that some (all really) OSes will give you chunks of memory
          rather than the exact amount asked for. This makes memory management much
          easier.
          In short. You can only be sure about the minimum memory your program will
          use.
          In large programs the difference in usage wil probably be barely noticeable
          though.

          --
          Carlos Martin
          SoKrA-BTS
          Be paranoid: Encrypt your mail.


          Comment

          Working...