Well. I was kind of annoyed because I am running Mac OS Lion on an i7 MPB with 8GB of RAM, and Safari always gets its performance degraded throughout time.
Hence, I have noticed that not only Safari.app consumes an abnormal amount of memory, but also its couple named "Safari Web Content" (which consumes even more).
Annoyed by this, I decided to kill that "Safari Web Contant" instances (they always appear in pairs, curiously. But, whatever), after all, Safari.app is the main application, anyways. What happened? Do you know when all of your tabs in Safari refresh automatically? The same effect occurs: memory usage of Safari Web Content drops (as it quits, and then is relaunched from point zero), and all your tabs get refreshed on selection. It makes me believe that those tabs' auto-refresh that sometimes happen is nothing more than a way to free memory in a very weird manner (what would be called "chuncho", in portuguese).
Ok, so what? Well, if that seems (at least for me) to be the 'official' way of better handling memory consumption, I guess it would be alright if I did the same, right?
Therefore, I created a small script that runs every minute as a crontab job, checking if "Safari Web Content" memory usage is too high and, if it is, kills it.
Here it is:
#!/bin/bash
filename=".tt_0e92309ei2390i209ei9203"
LIMIT=6
ps -eo pmem,pid,args | grep WebProcess.app | grep -v grep | cut -d"/" -f1 > $filename
while read line
do
mem=`echo "${line}"| awk '{print $1}'`
mem=$(echo $mem | sed -e 's/,/./g')
pid=`echo "${line}"| awk '{print $2}'`
status=$(echo "$mem > $LIMIT" | bc -l)
if [ "$status" -eq "1" ]
then
kill $pid
fi
done < $filename
rm $filename
Do not mind the random named filename, nor the weird things ("chunchos") I 开发者_如何学编程may have done :P That script basically checks if there is any "Safari Web Content" process consuming more than the $LIMIT percentage of my memory. As I have 8GB of ram, I though 6% would be already too much (Note that before, it sometimes managed to get way past those 6%), so the process should be killed! That verification is made each 1 minute, through that script's execution on my crontab.
Results? Safari's performance now is way better! :) I do not need to manually reset Safari once every one and a half hour, and that makes me very happy :) (even though, almost the same happens on the background)
Side effects? Weeell. Having your tabs refreshed is not always something wanted. There is one clear side effect I can think of. If your "Safari Web Content" process gets closed while you are in the middle of some important operation, you would lose everything. For instance, if my memory consumption got too high while I am tipping this, and the script killed my content process, I would lose everything.
Gladly, I have been using the script for some days already, and still did not lose anything.
Even though this solution gave me a better experience on browsing up to the moment, and I still did not get annoyed by having some writing lost, I know it is possible. Therefore, I wanted to know if you guys know of how to free a specific region of a process's memory.
I ask this because when I run vmmap on the pid of "Safari Web Content", I notice that the majority of the memory used is allocated in the Malloc Zone "JavaScriptCore FastMalloc_0xaca89d40". That way, I supposed that if I could get that memory dealloced, I could have a better experience on Safari, without the risk of losing too much (of course, that would depend on how Safari would handle that subtle unexpected clearance of memory, but I have faith).
Let's get to the end. Do your think my approach is good, or can you think of any other side effects of doing what I do? Further, do you know if I can clear the memory of that specific region? I do not mind if I would need to have root access (at least for testing purposes). And finally, have you also developed your own way of boosting your browsing experience, when using Safari? If so, please share! :)
Fernando.
No, you cannot force another process to deallocate memory. That'd almost certainly crash it anyway (which is effectively what you're doing by killing the Safari Web Content process).
I did something similar, just with applescript:
if appIsRunning("Safari") then
tell application "Safari"
quit
end tell
delay 5
end if
tell application "Safari"
activate
end tell
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
Then, I set up a cron job to run every morning at 3am. I don't seem to suffer too much bloat throughout the day. It's only after a few days of running that my "Safari Web Content" process starts eating 2+ gigs of memory.
1 3 * * * /usr/bin/osascript /path/to/restart-safari.scpt
精彩评论