Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Memory allocation textbook case

0 views
Skip to first unread message

Balteo

unread,
Jun 29, 2002, 4:25:53 PM6/29/02
to
Hello,

I am relatively new to C programming and I am experimenting memory
allocation functions. I am trying to
write a program that uses up an excessive amount of RAM (100 Mo) and to
observe its memory consumption from
the shell. Even though I allocate (1024 * 1024 * 100) bytes, the program
does not seem to be using that
much memory.

My program is as follows (it is called memoryD.c and the resulting
executable memoryD):
**************************************************
#include <stdio.h>
#include <stdlib.h>

main(){
char * adr;
adr = malloc(1024 * 1024 * 100);
if (adr == NULL){ printf("null\n");}
sleep(30);
free (adr);
}
**************************************************


The following shell command indicates only "0.1" in memory consumption
whereas it should be using more than
50% of the RAM:
**************************************************
ps -eo args,%mem | grep memory
**************************************************

So why isn't my program using the memory it should?

Thanks in advance,

--
Balteo

Stig Brautaset

unread,
Jun 29, 2002, 10:36:11 AM6/29/02
to
Balteo <bal...@wanadoo.fr> wrote:
> Hello,
>
> I am relatively new to C programming and I am experimenting memory
> allocation functions. I am trying to
> write a program that uses up an excessive amount of RAM (100 Mo) and to
> observe its memory consumption from
> the shell. Even though I allocate (1024 * 1024 * 100) bytes, the program
> does not seem to be using that
> much memory.
>
> My program is as follows (it is called memoryD.c and the resulting
> executable memoryD):
> **************************************************
> #include <stdio.h>
> #include <stdlib.h>
>
> main(){
> char * adr;
> adr = malloc(1024 * 1024 * 100);
> if (adr == NULL){ printf("null\n");}
> sleep(30);

Don't use sleep(), it is not standard C. Consider using getchar()
instead, then you can just hit enter when you are done doing your
observations.

> free (adr);
> }
> **************************************************
>
>
> The following shell command indicates only "0.1" in memory consumption
> whereas it should be using more than
> 50% of the RAM:
> **************************************************
> ps -eo args,%mem | grep memory
> **************************************************
>
> So why isn't my program using the memory it should?

Shell and platform-specific questions are strictly off-topic in
comp.lang.c.

However, you indicate that you're using Linux, so go then and read the
malloc() manpage. The last paragraph says this:

Linux follows an optimistic memory allocation strategy. This
means that when malloc() returns non-NULL there is no
guarantee that the memory really is available. In case it turns
out that the system is out of memory, one or more processes
will be killed by the infamous OOM killer.

Try to memset the mallocated area (or parts of it) and you should get a
different result.

Stig

--
brautaset.org

Mike Wahler

unread,
Jun 29, 2002, 1:49:29 PM6/29/02
to

Balteo <bal...@wanadoo.fr> wrote in message
news:afkeai$iuv$2...@blob.linuxfr.org...

Perhaps your OS is delaying the actual provision of memory
to your program until its actually used. Try writing some
values to the memory as well, and check again. Perhaps
using 'calloc()' instead of 'malloc()' would be sufficient.
('calloc()' does the same as 'malloc()' but also zero-initializes
the allocated memory.)


-Mike

>
> --
> Balteo


0 new messages