why its not consuming memory

R

Ravi

I created a program just to amuse me. I consumes a lot of memory and
never gives it up:

#incluse <stdio.h>
#include <stdlib.h>
int main(void) {
void *a;
while(1)
a = malloc(99999);
}

but the output of vmstat (on GNU/Linux) is a very constant as:

procs -----------memory---------- ---swap-- -----io---- -system-- ----
cpu----
r b swpd free buff cache si so bi bo in cs us
sy id wa
1 0 0 157292 51668 201972 0 0 97 23 379 408 10
8 80 2
procs -----------memory---------- ---swap-- -----io---- -system-- ----
cpu----
r b swpd free buff cache si so bi bo in cs us
sy id wa
1 0 0 157276 51676 201972 0 0 97 23 379 407 10
9 80 2
procs -----------memory---------- ---swap-- -----io---- -system-- ----
cpu----
r b swpd free buff cache si so bi bo in cs us
sy id wa
1 0 0 157260 51684 201972 0 0 96 23 379 407 10
9 79 2
procs -----------memory---------- ---swap-- -----io---- -system-- ----
cpu----
r b swpd free buff cache si so bi bo in cs us
sy id wa
1 0 0 157276 51692 201972 0 0 96 23 379 407 10
9 79 2
procs -----------memory---------- ---swap-- -----io---- -system-- ----
cpu----
r b swpd free buff cache si so bi bo in cs us
sy id wa
1 0 0 157292 51700 201972 0 0 96 23 379 406 10
9 79 2
procs -----------memory---------- ---swap-- -----io---- -system-- ----
cpu----
r b swpd free buff cache si so bi bo in cs us
sy id wa
1 0 0 157276 51708 201972 0 0 96 23 379 406 10
9 79 2
procs -----------memory---------- ---swap-- -----io---- -system-- ----
cpu----
r b swpd free buff cache si so bi bo in cs us
sy id wa
1 0 0 157292 51716 201972 0 0 96 23 379 405 10
9 79 2

showinf free memory almost constant.
 
C

Christopher Benson-Manica

Ravi said:
#incluse <stdio.h>
#include <stdlib.h>
int main(void) {
void *a;
while(1)
a = malloc(99999);
}
but the output of vmstat (on GNU/Linux) is a very constant as:

Bet $100 that malloc() is returning NULL.
 
S

Stephen Sprunk

Ravi said:
I created a program just to amuse me. I consumes a lot of
memory and never gives it up:

#incluse <stdio.h>
#include <stdlib.h>
int main(void) {
void *a;
while(1)
a = malloc(99999);
}

but the output of vmstat (on GNU/Linux) is a very constant as: ....
showinf free memory almost constant.

Linux happens to use a "lazy" allocation mechanism; memory isn't given to
the program until it's actually used. Since your program never uses the
memory, it never affects the "free memory" stats. "free" memory on Linux
means unused memory, not unallocated memory.

(In very rare circumstances, this has an evil side effect that malloc() can
succeed but then using the memory later causes a segfault because the system
doesn't actually have any remaining memory to give at that point. Lesson:
don't malloc() memory until you're ready to use it.)

S
 
A

Army1987

Ravi said:
I created a program just to amuse me. I consumes a lot of memory and
never gives it up:

#incluse <stdio.h>
#include <stdlib.h>
int main(void) {
void *a;
while(1)
a = malloc(99999);
}

The last time I did something like that (but with a reasonable size
i.e. 200) I crashed a server without even being root. And nobody
knew where the server was physically located, so we had to ask a
professor to go reboot it. Luckily, other users weren't doing
anything more serious than surfing the web or writing simple
homework assignment programs.

The standard says:
7.20.3.3 The malloc function

Synopsis

1 #include <stdlib.h>

void *malloc(size_t size);

Description

2 The malloc function allocates space for an object whose size is specified
by size and

whose value is indeterminate.

Returns

3 The malloc function returns either a null pointer or a pointer to the
allocated space.

It doesn't even use the verbs "succeed" or "fail".

Getting back to your question, see http://c-faq.com, question 7.14.
 
J

JimS

I created a program just to amuse me. I consumes a lot of memory and
never gives it up:

#incluse <stdio.h>
#include <stdlib.h>
int main(void) {
void *a;
while(1)
a = malloc(99999);
}
showinf free memory almost constant.

Is is possible for a compiler to know that malloc has no side effects
and to optimize this program to

int main(void) {for (;;);}

under the as-if rule?

Jim
 
K

Keith Thompson

Urrr, no -- because malloc *does* have side effects!

It doesn't have any well-defined visible side effects. Since the
program never examines the value of "a" (please pick better variable
names, even for exapmles), I *think* the compiler can legally optimize
away the call to malloc().

The program would be more meaningful if it checked whether malloc()
returns a null pointer or not.

In practice, on some systems, the behavior could be considerably
different if the program attempts to access the allocated memory, due
to "lazy allocation". It can be argued (and I do argue) that lazy
allocation is non-conforming, but it is a common implementation
choice.

(In lazy allocation, malloc() allocates a chunk of address space, but
the actual memory isn't actually allocated until the program attempts
to reference it. Unfortunately, if this delayed allocation fails, it
doesn't cleanly return an error indication as malloc() itself should
have done; it causes the process, or perhaps even another process, to
be killed.)
 
E

Eric Sosman

JimS wrote On 05/24/07 20:02,:
Is is possible for a compiler to know that malloc has no side effects
and to optimize this program to

int main(void) {for (;;);}

under the as-if rule?

malloc() has no side-effects? I didn't know that ...
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top