AutoPurger (Auto frees inactive memory)

Discussion in 'Mac Programming' started by Mactrillionaire, Jan 16, 2011.

  1. Mactrillionaire macrumors regular

    Joined:
    Oct 16, 2010
    #1
    AutoPurger is a command line application I wrote. It is pretty basic and runs in Terminal. Suggested use is to copy it to the /Applications directory and add it as a Login Item for each user. The purpose of the application is to free inactive memory which would otherwise lead to system instability as free memory gets low. The program will free inactive memory once per hour or if it gets larger than 0.25GB (of course, you can tweak these to your liking). The only exception you might find to how this works is when you are working with RAM Disks, the ability to return inactive memory to free memory is not as predictable. In any event, you can put this into Xcode and build the command line application. It should be helpful to those who are always running low on memory or using Flash too much.

    AutoPurger
    ------------

    Code:
    /*File: main.c*/
    /*Project: AutoPurger (Command Line Application)*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <unistd.h>
    #include <xlocale.h>
    
    int main (int argc, const char * argv[]) {
    	static char apBuf[512], *apPtrStart = (char *) 0, *apPtrEnd = (char *) 0;
    	time_t cur = 0, start = 0, next = 0;
    	FILE *in = (FILE *) 0;
    	unsigned int i = 0;
    	int inactive = 0;
    	for (start = time((time_t *) 0), cur = start, next = cur + 3600; ; next = (cur < next) ? next : cur + 3600) {
    		cur = time((time_t *) 0);
    		if (cur >= next) {
    			system("/usr/bin/purge");
    			sleep(30u);
    			continue;
    		}
    		system("/usr/bin/top -stats mregion -l 1 >./AutoPurger.dat");
    		in = fopen("./AutoPurger.dat", "r");
    		if (!in) {
    			sleep(30u);
    			continue;
    		}
    		fread((void *) apBuf, 1, 500, in);
    		fclose(in);
    		in = (FILE *) 0;
    		apPtrStart = strstr(apBuf, "active, ") + 8;
    		apPtrEnd = strchr(apPtrStart, 'M');
    		*apPtrEnd = '\0';
    		inactive = atoi(apPtrStart);
    		for (i = 0; i < 512; i++) {
    			apBuf[i] = '\0';
    		}
    		if (inactive > 256) {
    			system("/usr/bin/purge");
    			sleep(30u);
    			continue;
    		}
    		sleep(30u);
    	}
    	return 0;
    }
     
  2. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #2
    What makes you think this is remotely necessary? If a system is crashing due to a lack of RAM, then freeing the inactive RAM isn't going to help. That is, I've seen the Mail.app program dive into a runaway loop where it chews up as much RAM as possible until it has 3GB, and then it crashes (assuming it's 32-bit). Once the Mail.app program crashes, the system returns to normal. If it's running in 64-bit mode, it won't crash; it'll take down the system. However, freeing up inactive memory as you're suggesting won't save the system, as the kernel will have already done that, and even if it hadn't, the program would chew up whatever RAM was subsequently freed.

    Your logic and understanding is flawed. The purge command you are using just flushes the disk cache, which in turn will slow your machine down and put a little extra strain on your hard drive, as things will be more likely to need to be read back from disk, rather than being resident in RAM already. Free RAM is wasted RAM.

    I ran the "purge" command myself, and here's what happened: iTunes stopped playing music for several seconds, and my system stopped responding until it had finished the purge and the system had a chance to reload the stuff it needed from disk.


    I can recommend running this just as highly as I recommend people repair permissions to fix all their problems, and I call that superstition and voodoo.
     
  3. holmesf macrumors 6502a

    Joined:
    Sep 30, 2001
    #3
    Your premise is flawed. Inactive memory is used for things like caching recently accessed files. It speeds up application launching and disk reads. Generally speaking, the more inactive memory you have the faster your system will run. When free memory is low inactive memory is automatically released so that inactive memory does not result in paging to disk. Inactive memory does not result in system instability.

    Reading system memory in Mac OS X

    Haven't you ever noticed that your system acts much faster after it has been running for a long time than when it first booted up? Behold the benefits of inactive memory.
     

Share This Page