473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

why its not consuming memory

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.

May 23 '07 #1
7 1633
Ravi <ra*********@gmail.comwrote:
#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.
May 23 '07 #2

"Ravi" <ra*********@gmail.comha scritto nel messaggio
news:11**********************@n15g2000prd.googlegr 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.
May 24 '07 #3
"Ravi" <ra*********@gmail.comwrote in message
news:11**********************@n15g2000prd.googlegr 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

May 24 '07 #4
On 23 May 2007 11:26:59 -0700, Ravi <ra*********@gmail.comwrote:
>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
May 25 '07 #5
In article <fo********************************@4ax.com>,
JimS <so***@not.comwrote:
>On 23 May 2007 11:26:59 -0700, Ravi <ra*********@gmail.comwrote:
>>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
May 25 '07 #6
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <fo********************************@4ax.com>,
JimS <so***@not.comwrote:
>>On 23 May 2007 11:26:59 -0700, Ravi <ra*********@gmail.comwrote:
>>>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) ks***@mib.org <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"
May 25 '07 #7
JimS wrote On 05/24/07 20:02,:
On 23 May 2007 11:26:59 -0700, Ravi <ra*********@gmail.comwrote:

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

--
Er*********@sun.com
May 25 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Aravind | last post by:
Hi, I am trying to create a image object out of a bitmap file like Image img = new Bitmap(@"c-\CurDisplayImage.bmp"); ,I found the memory of the process is increased twice as the bitmap size ,...
7
by: Roberto Perez via .NET 247 | last post by:
Hello all and thank you in advance. We have several old applications in Cobol, Centura, etc that needs to be converted to .NET In order to save time our plan is to create web services and call...
5
by: Scott Emick | last post by:
I have rather small project that is a store/dealer locator but it is consuming about 143 MB in terms of the virtual memory I believe. Working set is 46 MB and Private Bytes 32 MB. The project...
6
by: kbs | last post by:
Hi, I'm looking for some good examples that illustrate how to code a web service that exposes a custom collection so that the properties of the collection are accessible on the client without...
7
by: W. Jordan | last post by:
Hello, I am using Response.WriteFile (filename) to write a file that on the server to my web client. I discover that the while a client request a big file, for example, a 14m-bytes one, the...
2
by: saran | last post by:
I am having a problem with MySQL consuming a lot of memory and eventually throwing an Out of Memory error and restarting itself. The symptoms are that swap usage continues to rise until some...
0
by: plmanikandan | last post by:
I need to develop a application to consume a webservice. For consuming web service i tried a example in Consuming a Web Service in C# in http://www.csharphelp.com.But i am uanble to use that...
0
by: Venkatesha | last post by:
Hi All, I wrote a sample windows application in C#, which contains a data class which has 2 float values and 3 bool values. public class DataPoints { float xval; float yval; bool typ1;...
1
lifeisgreat20009
by: lifeisgreat20009 | last post by:
There are around 861 files in the Internet Explorer Folder with the name series as follows reg00002 . . . . . .reg01010
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.