allocating 99% RAM

A

allanallansson

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
 
O

olaf_giezenaar

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
 
S

santosh

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

allanallansson

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!
 
A

allanallansson

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
 
S

santosh

(e-mail address removed) wrote:

Please don't top post.
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?
 
I

Ian Collins

(e-mail address removed) wrote:

Please don't top post.
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>
 
S

santosh

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

allanallansson

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

(e-mail address removed) wrote:

Please don't top post.
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?
 
S

santosh

It seems there has been a breakout of incurable top-posting here
recently.

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

Juuso Hukkanen

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.

Hi, This might resemble what Santosh suggested earlier.
Heavily commented C99 code

<OT>I made this sometime ago for t3d-progamming language (to serve as
function: t3d_measure_MEMORY_n_AVIALABLE). </OT>


/**
* measure_memory() - function
* Function returns the maximum amount of memory
* system can reserve using malloc.
* measure_memory() returns negative if it fails.
*
* Public domain by Juuso Hukkanen (2006)
*/


#ifndef _measure_memory
#define _measure_memory


#endif

#if defined(__cplusplus) && __cplusplus
extern "C" {
#endif


#include <stdlib.h>
#include <stdio.h>

#define TRY_MAX 1000 /* only to help to exit in case of paranormal
looping */

/*#define TEST_MAIN */ /* if testing: uncomment "#define
TEST_MAIN" */

size_t measure_memory(void);
void *mallocator(size_t size);

size_t measure_memory(void)
{
long long *taulu = NULL;
size_t low_fail =0; /* lowest mem amount detected unavailable */
size_t min =0;
size_t too_much;
int rounds = 0;
int first_fail =0;

while(1)
{
if(min == too_much)
{
return(min);
}
taulu = mallocator(too_much);

if (!taulu) /* if could not allocate too_much
bytes */
{
if(first_fail ==0)
{
first_fail =1; /* init recording of lowest
failed */
low_fail = too_much;
}
if((too_much< low_fail)) /* if tried less than previously
proven */
{ /* unavailable -->new
minimumimpossibe */
low_fail = too_much;
}
too_much -= (too_much - min) / 2;
if (too_much-min <=1)
{
return(min);
}
}
if(taulu) /* was able to allocate too_much
bytes */
{
min = too_much;
if(first_fail ==1) /* if first unavailable found */
{
too_much = too_much *2;
}
else /* a bit faster advantagement at
start */
{ /* until first unavailable amount
found */
too_much = too_much *16;
}
if((low_fail < too_much) && (first_fail ==1))
{
too_much = low_fail-1;
}
}
free(taulu);
rounds++;
if (rounds > TRY_MAX) /* someting strange has occured */
break; /* -->return with error */
}
return(-1); /* TODO: u_error: error of unexplained type */
}
/* a safe-malloc routine */
void *mallocator(size_t max)
{
void *p;
p=(void *)malloc(max);
if(p == NULL)
{
free(p);
return(NULL);
}
return(p);
}

#ifdef TEST_MAIN

int main(void)
{
int g=0;
size_t x =0;
for (g=0; g < 100; g++)
{
x = measure_memory();
printf("\n%d: %ld bytes of memory available",g,x);
}
return(0);
}

#endif /* TEST_MAIN */


Juuso Hukkanen
www.tele3d.com
(to reply by e-mail set addresses month and year to correct)
"t3d programming language" and the structure of t3d function prototype
are trademarks of Juuso Hukkanen. (Currently discussing the
transfer of those to a major charity organization).
 
A

Andrew Poelstra

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

Stop top-posting. The group will stop helping you if you persist.

If you're using Linux, just open up the kernel source and search for some
swap code. If you're using Windows, any attempt to measure this will result
in some terrible failure (most probably a system crash). If you're using an
embedded system, you almost certainly have no swap to test. I have no idea
about other OSes (other than that Macs seem pretty closed, and you probably
won't even be able to find the swap file(s)).

To implement someone else's idea elsethread:

#include <stdio.h>

int main (void)
{
size_t ctr = 0;
void *temp = malloc (0x100000);

while (temp)
{
temp = malloc (0x100000) /* This should be 1Mb, but my large hex math is */
ctr++; /* rusty. */
}
printf ("Allocated %dMb successfully.", ctr);

return EXIT_FAILURE;
}
 
S

santosh

Andrew said:
Stop top-posting. The group will stop helping you if you persist.

If you're using Linux, just open up the kernel source and search for some
swap code. If you're using Windows, any attempt to measure this will result
in some terrible failure (most probably a system crash). If you're using an
embedded system, you almost certainly have no swap to test. I have no idea
about other OSes (other than that Macs seem pretty closed, and you probably
won't even be able to find the swap file(s)).

AFAIK, Mac OS is built upon Darwin, a variant of FreeBSD. So it ought
to have a swap partition like most other UNIX like operating systems.
To implement someone else's idea elsethread:

#include <stdio.h>

int main (void)
{
size_t ctr = 0;
void *temp = malloc (0x100000);

while (temp)
{
temp = malloc (0x100000) /* This should be 1Mb, but my large hex math is */
ctr++; /* rusty. */
}
printf ("Allocated %dMb successfully.", ctr);

return EXIT_FAILURE;
}

No matter how you do it, allocating 99% of physical memory, which is
what the OP wants, under modern virtual memory based operating systems
is practically impossible. In general, a process can only deal with
virtual memory, and the OS will honour allocation requests far more
than the physical memory size, and map pages to page frames as needed.
Beyond a point of course, especially if a process tries to write to all
it's memory, it's sent a signal and killed.

There are means to lock pages in memory etc., but they aren't
accessible with standard C at all.
 
W

Walter Roberson

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

<OT>
My recent experience on a 64-bit Linux cluster indicates that Linux
reacts fairly typically [compared to other systems]

- if a new memory allocation request would exceed the maximum system
allocatable memory, then the request will be denied

- this will not directly terminate the process that made the memory
request, but carefully written programs that checked the allocation
results will usually terminate themselves, and programs not written
with sufficient allocation checking will typically misbehave and
then crash with a segmentation fault.

- either way, strange things often happen, as even the programs that
check the allocation are often in the middle of something when the
memory exhaustion is reached -- and many common utilities do not
output any kind of "memory limit" message as they terminate

- allocations that are backed by the swap file result in memory paging.
Either the Linux paging system is not particularily good or else
my typical application (Maple) as very bad locality of reference,
as the swapping slows down the system enourmously

- what is a bit different about our Linux cluster (at least compared to
the SGI IRIX systems I am more accustomed to) is that while heavy
paging is going on, Linux often drops input characters
(though it might be that what it is dropping is network traffic).
This often gives me the impression that the process has died
or the system has died. (Yes, I am certain that the characters
are dropped, not merely buffered for later processing.)
On IRIX each network process would have its own input buffer, so
for small amounts of input, the delay until echo might be quite
perceivable but the characters would not be dropped completely
[true, there are system network buffers that could get clogged, but
the IRIX scheduler would temporarily give priority to the processes
with network input waiting, thus allowing the system buffers to drain
into the process buffers.]

- the side-effect of the above tendancy of our Linux cluster to drop
characters while swapping, is that using ^Z to suspend a process might
not work, as the ^Z might well happen to be dropped. If you continue
to try eventually you might catch the process able to accept characters
and then the ^Z will work as expected. The ^Z is not disabled or
ignored -- as per the above, if the system is heavily paging,
the ^Z might just not arrive to be processed.

- suspending a large heavily swapping linux process suspends the
swapping-in for that process, but does not make any more total system
memory available. Further memory requests (e.g., for running 'top'
to figure out what's happening on the system) may have to be
backed by the swap file instead of by primary memory -- but those
new requests would often have much greater locality of reference
and Linux would typically just swap out more of the suspended process
in order to regain physical memory, so the effect is often perceived
as being that the system speeds right up again when the large
process is suspended. But if total virtual memory is nearly full,
then there might not be enough virtual memory to allocate in
order to satisify even moderate requests, with results as discussed
at the begining.

</OT>
 
W

Walter Roberson

santosh said:
No matter how you do it, allocating 99% of physical memory, which is
what the OP wants, under modern virtual memory based operating systems
is practically impossible. In general, a process can only deal with
virtual memory, and the OS will honour allocation requests far more
than the physical memory size, and map pages to page frames as needed.
Beyond a point of course, especially if a process tries to write to all
it's memory, it's sent a signal and killed.

<OT>
I don't know the details for Linux; SGI IRIX allows one to allocate
physical swap space (backed by a file or raw partition), and allows
one to allocate virtual swap space (not backed by anything).

If no IRIX virtual swap space has been allocated, then an IRIX memory
allocation request is only granted if there is adequate physical memory or
swap space. *All* of the memory so granted can be written to, with
no possibility of being sent a signal later when attempting to write
to memory for which an allocation was granted.

An interesting side effect of the above is that typical fork()/exec()
requests may fail, because in theory after the fork() the memory space
will be duplicated; the system does not assume that the memory will
all be given back "soon" in an exec().


If IRIX virtual swap space has been allocated, then an IRIX memory
allocation request is granted if there is sufficient physical memory,
backing swap, or the allocation would not fill up the system-wide
virtual memory. The result -does- have the indeterminacy that -some-
IRIX process may be sent a signal if the total actual writes would
fill up physical memory plus backing swap; the killed process will
not [in IRIX] necessarily be the one whose write would have overextended
the actual resources. Often it is the biggest process that IRIX decides
to kill, but not always; the decision algorithm was never publically
documented.


Thus, on "modern virtual memory systems", it may be the decision of the
systems administrator as to whether the system behaviour is as you
describe. A system can be a "virtual memory system" [i.e., addresses
as seen by processes need not have any visible relationship to physical
addresses], and yet be configured not to have the "kill off something
when memory fills up" behaviour you describe.
 
F

forkazoo

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

I think I understand what you are trying to do. I don't think it is a
very good idea, but if I were to try it, it'd probably look something
like this (I'm not near a compiler, so take this only as a rough
completely untested guide...) :


struct node {
int waste[SOME_NUMBER];
node *next;
};

int main() {
node start;
node tmp;
node *current = &start;
int count;
int i;
start.next = malloc (sizeof(node));

while (current != 0) {
current = current->next;
current = malloc (sizeof(node));
count++;
}

while(true) {
current = &start;
for (i=0; i<count; i++) {
current = current->next;
tmp = *current;
current->waste[0] = count;
}
}
return 0;
}


Basically, the idea is that you won't be able to allocate all the
memory in one go. At least, you aren't guaranteed to be able to. So,
allocate in small chunks at a time. Eventually, the system will decide
you have had all the memory you deserve, and the malloc will fail.
Now, if you simply allocate it and then don't do anything with it, the
system may just swap it out, and leave it is a big chunk of the swap,
rather than actually using much memory. To get around this, you have
to do stuff with it. I have this sample running thorugh a linked list,
trying to copy each node onto tmp, and then write to the array that is
mostly just there to use up space. This will probably peg the CPU,
which isn't something you mentioned wanting to do. But, using the
memory is the only way I know to ask the system to keep it actually in
RAM. There may be some system-specific options you can use. Ask in an
OS-specific newsgroup for more info.

Also, if I haven't done something stupid, in which case this just won't
compile, or will crash... It isn't supposed to ever exit. Good luck
with that. (And, don't try to run it on anybody else's machine!)
 
S

santosh

.... snip ...
I think I understand what you are trying to do. I don't think it is a
very good idea, but if I were to try it, it'd probably look something
like this (I'm not near a compiler, so take this only as a rough
completely untested guide...) :


struct node {
int waste[SOME_NUMBER];
node *next;
};

int main() {
node start;
node tmp;
node *current = &start;

You need:
struct node XXX;

.... snip ...
But, using the memory is the only way I know to ask the system to keep it actually in RAM.

The operating system might still swap out pages that aren't being
_currently_ accessed. Of course under heavy stress, this will cause
thrashing.
There may be some system-specific options you can use. Ask in an OS-specific
newsgroup for more info.

Yes, there are non-standard functions to 'lock' pages in memory,
(meaning that a corresponding physical page frame will always be
present), but even with these, most OSes don't gaurentee that you'll
exactly get what you've asked for.
 
K

Keith Thompson

santosh said:
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.

Correction: it will ail as soon as the OS has given your program as
much memory as it wants to. An implementation can place limits on how
much memory a program can allocate; those limits are not necessarily
directly related to the total amount of physical, or even virtual,
memory available.

<OT>See "limit" and/or "ulimit".</OT>

And to the OP: read <http://www.caliburn.nl/topposting.html>.
 
D

Default User

santosh said:
It seems there has been a breakout of incurable top-posting here
recently.

That's because Google finally fixed the no-quote business. So now we
have to train them to not top-post. When you do complain to them, don't
just say, "don't top-post". Most of them are newbies and have no idea
what that means. Explain that replies belong following or interspersed
with trimmed quotes.



Brian
 
K

Keith Thompson

Default User said:
That's because Google finally fixed the no-quote business. So now we
have to train them to not top-post. When you do complain to them, don't
just say, "don't top-post". Most of them are newbies and have no idea
what that means. Explain that replies belong following or interspersed
with trimmed quotes.

Or just cite <http://www.caliburn.nl/topposting.html>. (Google users
are already using a web browser; they can follow a link as easily as
they can jump to the next message.)
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top