Page 1 of 1

Freeing memory, ect.

PostPosted: Wed Feb 05, 2003 4:24 am
by X11
A while ago these code was posted onto the MES to a program which allocates as much memory as possible and then exits. I find that if i let it run when lots of programs are open, it tends to make everything use less memory in the long run. Im not quite sure, but before i ran it, i had about 80% used memory (not swap) and about 5% swap. Now its 45% used memory, 18% used swap. Well it looks like it put the unused stuff in swapspace.

Anyways here is the code.
Code: Select all
/* This program is great, like that X11 guy you like. */
int main(int argc, char *argv[])
{
        char *Memory;
        int MegsObtained= 0;
        int KsObtained= 0;

        while(1)
        {
                for (KsObtained=0; KsObtained < 1024; KsObtained++)
                {
                        Memory= (char *)malloc(1024);
                        if (!Memory) return EXIT_FAILURE;
                        sprintf(Memory, "Hey there!");
                } /* End for */

                MegsObtained++;
                printf("Now allocated %d Megs\n", MegsObtained);
        } /* End while */
        return EXIT_SUCCESS;
}

PostPosted: Fri Feb 14, 2003 9:00 am
by X11
all the sudden this wont compile.

hmmmmmm

PostPosted: Fri Feb 14, 2003 12:56 pm
by Void Main
Code: Select all
#include <stdlib.h>

PostPosted: Fri Feb 14, 2003 6:33 pm
by X11
why did i not notice that

PostPosted: Fri Feb 14, 2003 7:55 pm
by X11
hahahahaha
you forgot to include <stdio.h>

hahahahaha

PostPosted: Fri Feb 14, 2003 8:05 pm
by Void Main
Who forgot? I didn't forget. It's not necessary in the code that you posted. Compile your code with and without the "#include <stdio.h>" and you go ahead and lie to me about the binaries being different.

PostPosted: Fri Feb 14, 2003 9:27 pm
by X11
hmmmm it wouldnt compile on my system without it, oh well.

PostPosted: Fri Feb 14, 2003 10:34 pm
by Void Main
I originally included both stdio.h and stdlib.h because that is just something I normally do for any C program I write. I wanted to be cute though and narrow it down to just the necessary include file. With GCC 3.2 it seems enough to include just stdlib.h, maybe with 2.9x you need stdio.h. Which version do you have?

Of course if you replaced "EXIT_FAILURE" with "1" and "EXIT_SUCCESS" with "0" then you wouldn't have to include any header and still have the exact same resulting binary.