I found a piece of code, (unfortunately i can’t remember from where) and my question here is, if the following code is sure for a continued use.
--free memory
set limitMem to 1000 -- If free memory size is less than this number (MB), the action will be run.
-- with this example, if free memory size is less than 1,000MB, Task3 asks to release memory.
set freeMem to (do shell script "echo " & word 10 of (do shell script "top -l 1 | head -10 |grep PhysMem") & " | sed -e 's/M//g'") as number
if freeMem ≤ limitMem then
do shell script "du -s / &> /dev/null & sleep 10 && kill $!"
do shell script "sleep 2"
end if
I have seen this before, but then I just dismissed it, and I can’t remember either, where I saw it.
It works for me under Snow Leopard, and I think all the utilities still should work. (Clever hack for collecting up memory.) And as long as the garbage collection / memory freeing isn’t implemented in any other way as it is, then I think this still should work.
The way you use the machine, will of course inflict upon how useful it is, but I think it freed 400 Mb for me the first time I used it.
What this does is eating up memory when there is not much memory available. The machine will run out of memory and needs to store memory to the disk (read: virtual memory). The command will run for 10 seconds and depending on your hardware (read: bandwidth) a certain amount of data will be swapped from your ram to your disk. 10 seconds will be 100MB on older machine and will be 4GB on my latest machine with SSD.
You can think of this as an clever hack but it is also a dirty hack. The fact is whenever you need utilities like this you have to accept that you’re working on an weak machine or running bad written processes like Java applications. Then modern operating systems uses reserved memory as well and will be shown as used memory. This reserved memory is data in your memory that’s not being used but can be allocated really quickly in the state it was (read: earlier freed memory). For instance when I have a process and tell the program loader in the kernel to load the executable and the executable has been loaded before, it will try to use reserved memory first. This way we don’t need to read from disk but can activate the reserved memory again which saves a lot of time. Using utilities like shown above will break down these features because your machine will use reserved memory before actually swap data.
The hardware and software in modern day computers are the result of more than 40 years of engineering. Do you really think that an operating system like Unix uses unnecessary memory today? Bad written software still exists, but the tool mentioned above doesn’t solve this either. One of the great advantages of Mac OS was that you could assign the maximum allowed memory use for each application.
Long story short: There is more free memory than activity monitor or top will show you. To release reserved memory and have more “free” memory available simply use the command purge on the command line.
as an AWK-fan here a way to get the pysical memory instead of using 3 different command (head, grep and sed):
As I said, it worked for me, you mustn’t forget that we are having stuff like flash running here too, and I think that or Safari was the source of all the free memory I coalesced by running the “dirty hack”.
It is indeed a hack, but it works.
Thanks for getting to know about purge, purge frees memory in conjunction with the disk cache, and doesn’t free anynomous memory, according to the man page, so I’ll say like Winne the Pooh, both please.
I can have like 250 tabs open in my browser for a month or so, and then, when the fans starts running, then I intend to run that script, and see if it works, because those 10 seconds may be less than the time it takes to reload the tabs.
It may help or not but the extra free memory shown in the statistics after running the script is just a deception. My machine shows more free memory (less used memory) but the fact is that I’ve only slowed down my machine.
Example on an older machine (2nd generation 2.3GHz i7 MBP) to show significant differences: I start Microsoft Word 2011 for the first time and will take about 10 seconds, the second time I launch Word it will be launched in less than a second. This is because it won’t read the application from disk but from reserved but unused memory (for the record: it’s free memory when needed but used last). Now I run the script (or run command purge in terminal) and start Word again, now it takes 10 seconds to launch instead of less than a second. So the only thing I have achieved is slowing down my machine’s performance.
It won’t solve memory leaks (flash and 250 tabs in your browser example)
I actually like the approach with using du for coalescing memory blocks better than flushing caches, but the situation may vary. I don’t know if it is memory leaks, so I am willing to try. Having said that, something leaked memory once in my browser.
Thing is I am lazy, so I don’t close tabs, and I “google them” to find back… Sometimes I can lower the memory consumption by just minimizing everything, and then maximize what I have use for.
I am not saying that the “hack” will work, but I am going to try it. 12 seconds are cheap, when the situation arises.
Hi DJ Bazzie Wazzie
and thank you for the in-deep explanation. I was conscious what the script did, but unsure if the hack was save enough to be used for a prolonged period of time. Still now, my question is (and maybe i’ve overseen something reading this thread) if the code is save ?
I switched relatively late to Leon and this guy is much more Memory-hungry, than the older Os.
By the love of coding, but its a luck that computers have learned to work with 2d/3d too, and not only with numbers.
Sorry, somehow I missed your question. It’s safe to the code and won’t harm your machine. Again, you won’t free memory that’s being allocated but unused by the processes, if that would be possible you have gold in your hands .it’s only freeing memory that’s already “free” but is freed relatively short time ago. So instead of thinking you’re actually freeing up memory you will slow down some parts of your machine’s performance. So like I said, it’s more an illusion than actually freeing memory. I’m just saying so you know what happens when you get disappointing results.
Well, it makes the sound of the fans go away quicker, ok, maybe I just gain a couple of more minutes without fans, but, that is still an improvement!
It is kind of funny, the memory is actually freed, but what the snippet does, is forcing the memory manager, to get at it quicker. And since the fans goes away, then this does implicate less system activity, since the machine obviously works less, when the fans are off. So, it can’t be all that illusional.