Memory usage

E

Evangelista Sami

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(char) * 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
 
C

Case

Evangelista said:
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(char) * 65);

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

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
 
A

Allan Bruce

Evangelista Sami said:
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(char) * 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

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
 
S

Simon Massey

Evangelista Sami said:
So my question is : what does really do a malloc call?
for example if i do this

ptr = malloc(sizeof(char) * 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

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.
 
C

Carlos Martin

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top