Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > why its not consuming memory

Reply
Thread Tools

why its not consuming memory

 
 
Ravi
Guest
Posts: n/a
 
      05-23-2007
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.

 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      05-23-2007
Ravi <(E-Mail Removed)> wrote:

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

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
Stephen Sprunk
Guest
Posts: n/a
 
      05-23-2007
"Ravi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      05-24-2007

"Ravi" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) oups.com...
>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.


 
Reply With Quote
 
JimS
Guest
Posts: n/a
 
      05-25-2007
On 23 May 2007 11:26:59 -0700, Ravi <(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      05-25-2007
In article <(E-Mail Removed)>,
JimS <(E-Mail Removed)> wrote:
>On 23 May 2007 11:26:59 -0700, Ravi <(E-Mail Removed)> wrote:


>>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);
>>}


>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?


Urrr, no -- because malloc *does* have side effects!
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-25-2007
http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
> In article <(E-Mail Removed)>,
> JimS <(E-Mail Removed)> wrote:
>>On 23 May 2007 11:26:59 -0700, Ravi <(E-Mail Removed)> wrote:
>>>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);
>>>}

>
>>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?

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

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-25-2007
JimS wrote On 05/24/07 20:02,:
> On 23 May 2007 11:26:59 -0700, Ravi <(E-Mail Removed)> wrote:
>
>
>>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?


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

--
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
Re: why its not consuming memory p_cricket_guy@yahoo.co.in C Programming 1 05-24-2007 06:19 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments