#include <stdio.h>
#include <stdlib.h>
int main()
{
void *memory;
int mem_to_allocate;
while( 1 ) {
printf("Memory to allocate ");
scanf("%d", &mem_to_allocate);
if( (memory = malloc(mem_to_allocate)) == NULL) {
printf("Out of memory\n");
}
else {
printf("Memory allocated! \n");
}
}
return 0;
}
Allocate big memory chunksAllocate all my computers memory just for fun
Page 1 of 1
4 Replies - 2357 Views - Last Post: 08 April 2010 - 03:42 PM
#1
Allocate big memory chunks
Posted 08 April 2010 - 04:04 AM
Hi, I tried to do a simple program that would allocate all or a lot of memory on my computer so that I could see it with "free -m" from the command line (Linux program to see how much memory is free). But I don't really see any difference when I'm running my test program, I guess I have done something wrong here? Any clues?
Replies To: Allocate big memory chunks
#2
Re: Allocate big memory chunks
Posted 08 April 2010 - 07:05 AM
I'm pretty sure that malloc() simply reserves the memory space, basically marking it as reserved memory. If you wish for the memory to be used, then you will need to actually fill it with date.
Just use a simple fixed loop that initializes all the memory to some arbitrary value, and you should see the memory being used.
Just use a simple fixed loop that initializes all the memory to some arbitrary value, and you should see the memory being used.
This post has been edited by Aphex19: 08 April 2010 - 07:06 AM
#3
Re: Allocate big memory chunks
Posted 08 April 2010 - 08:07 AM
Might be true... I'll try that..
#4
Re: Allocate big memory chunks
Posted 08 April 2010 - 09:32 AM
You will have to remember that if you allocate a large amount of memory, it will probably end up as virtual memory on disc. Both Linux and Microsoft Windows are extremely good at implementing this mechanism. The result is that you may not see an awful lot of difference in the memory usage.
#5
Re: Allocate big memory chunks
Posted 08 April 2010 - 03:42 PM
Okey, that might explain some parts..
Good to know!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|