Allocate big memory chunks

Allocate all my computers memory just for fun

Page 1 of 1

4 Replies - 3178 Views - Last Post: 08 April 2010 - 03:42 PM Rate Topic: -----

#1 Scorpiion  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 23-April 09

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?

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



Is This A Good Question/Topic? 0
  • +

Replies To: Allocate big memory chunks

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

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.

This post has been edited by Aphex19: 08 April 2010 - 07:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 Scorpiion  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 23-April 09

Re: Allocate big memory chunks

Posted 08 April 2010 - 08:07 AM

Might be true... I'll try that.. :)
Was This Post Helpful? 0
  • +
  • -

#4 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

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.
Was This Post Helpful? 1
  • +
  • -

#5 Scorpiion  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 23-April 09

Re: Allocate big memory chunks

Posted 08 April 2010 - 03:42 PM

Okey, that might explain some parts.. :) Good to know!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1