Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > allocating 99% RAM

Reply
Thread Tools

allocating 99% RAM

 
 
allanallansson@spray.se
Guest
Posts: n/a
 
      07-03-2006
Hi i would like some guidelines for a experiment of mine. I want to
experiment with the swap and ctrl-z in linux. And for this i want to
create a c program that allocates almost all the free memory resources.

So far i have tried to use

#include <stdio.h>

int main() {
int *ip;
while(1) {
ip = calloc(sizeof(int));
ip = 312319283;
}
return 0;
}

The problem is that the memory allocation is not enough... (i hardly
think it allocates any memory at all)
I know i shoud return the allocated memory with free but as i said i
need some help with this.

Thank you in advance, Allan

 
Reply With Quote
 
 
 
 
olaf_giezenaar@hotmail.com
Guest
Posts: n/a
 
      07-03-2006
Hello

Look in the C manual

calloc needs 2 parameters

What your progam is doing is not defined!

use

int main(int argc,char *argv[])
{
while(1)
{
int *iP = malloc(sizeof(int));
iP[0] = 12344;
}
}

you will see your memory is full in no time

Greetings Olaf

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      07-03-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi i would like some guidelines for a experiment of mine. I want to
> experiment with the swap and ctrl-z in linux.


You'd probably need Linux specific libraries like glibc etc.

> And for this i want to
> create a c program that allocates almost all the free memory resources.


Why? It's rare that a program, especially a demo, needs to allocate all
available memory.

Besides, under operating system employing virtual memory, it's not
gaurunteed that 99% of RAM will be given to your program, even if it
happens to free. The OS will often honour requests for allocation far
exceeding the physical RAM and map the VM pages of your process to
physical memory as necessary.


> So far i have tried to use
>
> #include <stdio.h>
>
> int main() {
> int *ip;
> while(1) {
> ip = calloc(sizeof(int));


How do figure that this code snippet allocates almost all free memory!?
All it does is overwrite a pointer with an allocation of sizeof(int),
(often 4 bytes).

> ip = 312319283;
> }
> return 0;
> }
>
> The problem is that the memory allocation is not enough... (i hardly
> think it allocates any memory at all)


Oh yes it allocates all right. Allocates and looses memory in miniscule
installments.

> I know i shoud return the allocated memory with free but as i said i
> need some help with this.


It totally unclear what the overall objective of your program is. Until
you can shed more light on that, we can't do more than make wild
guesses.

 
Reply With Quote
 
allanallansson@spray.se
Guest
Posts: n/a
 
      07-03-2006
Thank you for your reply!

I tried your code, it works much better now except that i get
"Segementation fault". Is there some way to get around this issue in
order to allocate as much space as i need to without a program chrash?

Any ideas are appriciated!


(E-Mail Removed) wrote:
> Hello
>
> Look in the C manual
>
> calloc needs 2 parameters
>
> What your progam is doing is not defined!
>
> use
>
> int main(int argc,char *argv[])
> {
> while(1)
> {
> int *iP = malloc(sizeof(int));
> iP[0] = 12344;
> }
> }
>
> you will see your memory is full in no time
>
> Greetings Olaf


 
Reply With Quote
 
allanallansson@spray.se
Guest
Posts: n/a
 
      07-03-2006
I want my c program to allocate "all" the available ram and when it has
allocated "all" the free ram i inted to suspend that process (ctrl-z)
and then start another similar program (that allocates memory in the
same maner). by doing this i intend to study what happens to the swap
(eg. study the swap growth).

I hope this has shed some light on my goals!

Best regards, Allan


santosh wrote:
> (E-Mail Removed) wrote:
> > Hi i would like some guidelines for a experiment of mine. I want to
> > experiment with the swap and ctrl-z in linux.

>
> You'd probably need Linux specific libraries like glibc etc.
>
> > And for this i want to
> > create a c program that allocates almost all the free memory resources.

>
> Why? It's rare that a program, especially a demo, needs to allocate all
> available memory.
>
> Besides, under operating system employing virtual memory, it's not
> gaurunteed that 99% of RAM will be given to your program, even if it
> happens to free. The OS will often honour requests for allocation far
> exceeding the physical RAM and map the VM pages of your process to
> physical memory as necessary.
>
>
> > So far i have tried to use
> >
> > #include <stdio.h>
> >
> > int main() {
> > int *ip;
> > while(1) {
> > ip = calloc(sizeof(int));

>
> How do figure that this code snippet allocates almost all free memory!?
> All it does is overwrite a pointer with an allocation of sizeof(int),
> (often 4 bytes).
>
> > ip = 312319283;
> > }
> > return 0;
> > }
> >
> > The problem is that the memory allocation is not enough... (i hardly
> > think it allocates any memory at all)

>
> Oh yes it allocates all right. Allocates and looses memory in miniscule
> installments.
>
> > I know i shoud return the allocated memory with free but as i said i
> > need some help with this.

>
> It totally unclear what the overall objective of your program is. Until
> you can shed more light on that, we can't do more than make wild
> guesses.


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-03-2006
(E-Mail Removed) wrote:

Please don't top post.

> (E-Mail Removed) wrote:
> > Hello
> >
> > Look in the C manual
> >
> > calloc needs 2 parameters
> >
> > What your progam is doing is not defined!
> >
> > use
> >
> > int main(int argc,char *argv[])
> > {
> > while(1)
> > {
> > int *iP = malloc(sizeof(int));
> > iP[0] = 12344;
> > }
> > }
> >
> > you will see your memory is full in no time

>
> Thank you for your reply!
>
> I tried your code, it works much better now except that i get
> "Segementation fault".


It probably to be expected. If you keep allocating memory, eventually,
the OS will terminate your application to maintain system stability.

> Is there some way to get around this issue in
> order to allocate as much space as i need to without a program chrash?


First determine the amount of memory you'll need, since you'll have
some idea, based on your application's purpose. Then try to malloc() it
in one go, and if it fails, then all you can do is either quit
gracefully or keep trying successively smaller allocations until
malloc() succeeds.

Just allocating memory, and causing memory leak, in an infinite loop is
not going to do it.

Out of curiosity, why do you need "99%" of the RAM for? What exactly is
your program trying to accomplish?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-03-2006
(E-Mail Removed) wrote:

Please don't top post.

> santosh wrote:
>
>>It totally unclear what the overall objective of your program is. Until
>>you can shed more light on that, we can't do more than make wild
>>guesses.

>
> I want my c program to allocate "all" the available ram and when it has
> allocated "all" the free ram i inted to suspend that process (ctrl-z)
> and then start another similar program (that allocates memory in the
> same maner). by doing this i intend to study what happens to the swap
> (eg. study the swap growth).
>
> I hope this has shed some light on my goals!
>

<OT>
Well you just can't do that - the OS will give you all the virtual
memory it can, you can't restrict malloc() to physical memory only.
<OT>

--
Ian Collins.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-03-2006
(E-Mail Removed) wrote:
> I want my c program to allocate "all" the available ram and when it has
> allocated "all" the free ram i inted to suspend that process (ctrl-z)
> and then start another similar program (that allocates memory in the
> same maner). by doing this i intend to study what happens to the swap
> (eg. study the swap growth).
>
> I hope this has shed some light on my goals!


In which case a blind infinite loop will not cut it. Allocate memory in
larger chunks, (say 1 Mb), and test the return value of malloc() for
success or failure. It will fail as soon as the OS has given your
program as much memory as it can. Then the program can indicate failure
to the console, whereupon, you can suspend the process.

<OT>
I suspect that beyond a certain point, the OS will either fail to load
any more instances of your program or will terminate it soon after
starting it. In any case the OS, (especially Linux), shouldn't crash.
</OT>

 
Reply With Quote
 
allanallansson@spray.se
Guest
Posts: n/a
 
      07-03-2006
I have a compilation (place and route etc) that requires 4 gig ram to
run. I want to examine what happens to it if i suspend it (sometimes
you want to suspend it since the job can take up to 10 hrs+ and you
might have a smaller job you want to sqeeze in).

Do you have a example of how i can allocate this amount memory in the
maner as you suggested?

Thank you for your help, Allan


santosh wrote:
> (E-Mail Removed) wrote:
>
> Please don't top post.
>
> > (E-Mail Removed) wrote:
> > > Hello
> > >
> > > Look in the C manual
> > >
> > > calloc needs 2 parameters
> > >
> > > What your progam is doing is not defined!
> > >
> > > use
> > >
> > > int main(int argc,char *argv[])
> > > {
> > > while(1)
> > > {
> > > int *iP = malloc(sizeof(int));
> > > iP[0] = 12344;
> > > }
> > > }
> > >
> > > you will see your memory is full in no time

> >
> > Thank you for your reply!
> >
> > I tried your code, it works much better now except that i get
> > "Segementation fault".

>
> It probably to be expected. If you keep allocating memory, eventually,
> the OS will terminate your application to maintain system stability.
>
> > Is there some way to get around this issue in
> > order to allocate as much space as i need to without a program chrash?

>
> First determine the amount of memory you'll need, since you'll have
> some idea, based on your application's purpose. Then try to malloc() it
> in one go, and if it fails, then all you can do is either quit
> gracefully or keep trying successively smaller allocations until
> malloc() succeeds.
>
> Just allocating memory, and causing memory leak, in an infinite loop is
> not going to do it.
>
> Out of curiosity, why do you need "99%" of the RAM for? What exactly is
> your program trying to accomplish?


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-03-2006
It seems there has been a breakout of incurable top-posting here
recently.

(E-Mail Removed) wrote:
> I have a compilation (place and route etc) that requires 4 gig ram to run.


I assume a program compilation... ?

> I want to examine what happens to it if i suspend it (sometimes
> you want to suspend it since the job can take up to 10 hrs+ and you
> might have a smaller job you want to sqeeze in).


It depends on the amount of physical RAM, paging space and load on the
particular system. It's something you've to "experiment" and find out.

Besides, this has got nothing to do with the standard C langauge which
gaurentees an allocation of a single object of 65536 bytes. Anything
more is upto the specific implementation.

> Do you have a example of how i can allocate this amount memory in the
> maner as you suggested?


Well, if it's a program compilation, why would _you_ want to allocate
the memory. The compiler and OS should be able to handle it provided
sufficient amounts of physical RAM and paging space are present.

Typical C compilations are usually incremental, so even very large
programs should compile, as long as they have been constructed in a
modular manner. It might take a while though.

 
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
w3wp.exe Allocating Over a Gig of RAM Tod Birdsall ASP .Net 5 04-12-2008 03:53 AM
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
No RAM left, n e 1 no of RAM giver (freeware) ? Morph Computer Support 2 10-09-2004 02:11 AM
Looking for a VHDL or Verilog RAM Model that modles Common RAM Faults Robert Posey VHDL 0 11-26-2003 07:50 PM
Mixing SDR Ram with DDR Ram? Daz Computer Support 2 09-14-2003 03:28 PM



Advertisments