0

So basically I want to find the number of Gb free on my computer (just the whole Gbs, ignore the fraction Gb) using malloc() function. Here us the code that I use:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argv, char **argc) {
    unsigned long size = 0;
    void *part[1000];
    int i = 0;

    part[size] = (void *)malloc(1024*1024*1024);

    if(part[size] == 0) 
        printf("The computer has less than 1 Gb of free memory\n"); 
    else {
        while((part[size] != NULL) && (size<1000)) {
            size++;
            part[size] = (void *)malloc(1024 *1024 *1024);
        }  
        while(i <= size) {
            free(part[size]);
        }
        printf("The computer has %luGb of free memory\n", size);
    }
    return 0;
}

The result is segmentation fault(Core dumped), I don't really know why this happen and would be really appreciate if anyone can point out where I got wrong. Thanks :)

ajay
  • 9,402
  • 8
  • 44
  • 71
Thomas Dang
  • 201
  • 3
  • 4
  • 14
  • 1
    Note that this approach will not tell you how much free memory you have (at least not on Linux) due to [memory overcommitment](https://www.google.co.uk/search?q=overcommit+memory). – Oliver Charlesworth Feb 15 '14 at 09:28
  • 1
    No amount of fixing the code can make it do what you want. What is the real problem? – David Heffernan Feb 15 '14 at 09:29
  • 1
    possibly you want http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process – qwr Feb 15 '14 at 09:33

4 Answers4

2

You are getting segmentation fault because you are freeing the memory which you have already freed. Update your second while loop to

while(i <= size) {
    free(part[i]);
    i++;
}

As mentioned in the comments, this won't give you how much free memory you have on your system because the OS allocates virtual address space to the process with no guarantee that physical storage for it exists. This is called memory overcommit. Read in detail here - What is Overcommit?

Also, you should not cast the result of malloc. There's no benefit and it can lead to bugs if you forget to include the stdlib.h header file. Please read this Should I cast the result of malloc?

Community
  • 1
  • 1
ajay
  • 9,402
  • 8
  • 44
  • 71
  • I change according whaest you said but now I get Bus error (Core dumped). Any suggestion ? – Thomas Dang Feb 17 '14 at 04:46
  • It runs fine on my system (Ubuntu 13.04 64-bit) and prints `The computer has 1000GB of free memory`. Please check your code again and try to debug using gdb. – ajay Feb 17 '14 at 05:50
0

Change:

while(i<=size)
    free(part[size]);

To:

for (i=0; i<size; i++)
    free(part[i]);

BTW, I don't think that it will give you the number of free GB on your computer, but the number of free GB on the heap allocated for this process.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • Supposing that the first `while` condition fails when `size == 1000`, you should also free `part[size]`. – ajay Feb 15 '14 at 11:08
0

The loop below is infinite. There is no breaking condition from loop.

while(i<=size){
   free(part[size]);
}

Change it to

long size = 0; //change to to long from unsigned long to make sure below loop would work.


while(size >= 0){
   free(part[size]);
   size--;
}
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
0

Your code writes past the end of the array "part" if malloc succeeds 1000 times.

The way how you name variables is very confusing. size is the size of what? It isn't a size at all. It's the count of successfully malloc'ed blocks. Call it that: mallocedBlockCount. Now you know what it is, and it makes your code a lot clearer and your bugs a lot more obvious.

It would make it obvious that size isn't actually the number of successfully malloc'ed blocks but one less. And as a result, the test size < 1000 succeeds at a point where there isn't actually any space left. That bug would be obvious if your variables described what they contain and actually matched that description.

But actually, the whole scheme doesn't give you anything useful, because most OSes will let you allocate humongous amounts of memory - as long as you don't use it. Some will crash if you use too much, some will slow down to a crawl.

gnasher729
  • 51,477
  • 5
  • 75
  • 98