Freeing memory, ect.

Place to discuss anything, almost. No politics, religion, Microsoft, or anything else that I (the nazi censor) deem inappropriate.

Freeing memory, ect.

Postby X11 » Wed Feb 05, 2003 4:24 am

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;
}
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby X11 » Fri Feb 14, 2003 9:00 am

all the sudden this wont compile.

hmmmmmm
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Fri Feb 14, 2003 12:56 pm

Code: Select all
#include <stdlib.h>
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Fri Feb 14, 2003 6:33 pm

why did i not notice that
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby X11 » Fri Feb 14, 2003 7:55 pm

hahahahaha
you forgot to include <stdio.h>

hahahahaha
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Fri Feb 14, 2003 8:05 pm

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.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Fri Feb 14, 2003 9:27 pm

hmmmm it wouldnt compile on my system without it, oh well.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Fri Feb 14, 2003 10:34 pm

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.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA


Return to The Lounge

Who is online

Users browsing this forum: No registered users and 1 guest

cron