帮助改进我个人针对 Safari 高内存使用的解决方案,或者说说为什么它不好

发布于 2024-12-01 17:27:26 字数 2033 浏览 0 评论 0原文

出色地。我有点恼火,因为我在带有 8GB RAM 的 i7 MPB 上运行 Mac OS Lion,而 Safari 的性能总是随着时间的推移而下降。

因此,我注意到不仅 Safari.app 消耗了异常的内存量,而且它的一对名为“Safari Web Content”的内存消耗也异常(消耗更多)。

对此感到恼火,我决定杀死“Safari Web Contant”实例(奇怪的是,它们总是成对出现。但是,无论如何),毕竟 Safari.app 是主要应用程序,无论如何。发生了什么?您知道 Safari 中的所有选项卡何时自动刷新吗?会发生相同的效果:Safari Web Content 的内存使用量下降(当它退出时,然后从零重新启动),并且所有选项卡都会在选择时刷新。这让我相信,这些选项卡有时会发生的自动刷新只不过是一种以非常奇怪的方式释放内存的方式(葡萄牙语中称为“chuncho”)。

好吧,那又怎么样?好吧,如果这似乎(至少对我来说)是更好地处理内存消耗的“官方”方式,我想如果我也这样做就好了,对吧?

因此,我创建了一个小脚本,作为 crontab 作业每分钟运行一次,检查“Safari Web Content”内存使用率是否过高,如果是,则终止它。

就是这样:

#!/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

不要介意随机命名的文件名,也不介意我可能做过的奇怪的事情(“chuncchos”):P 该脚本基本上检查是否有任何“Safari Web Content”进程消耗超过我的 $LIMIT 百分比记忆。由于我有 8GB 的​​ ram,我认为 6% 已经太多了(请注意,以前,它有时会成功超过这 6%),所以应该杀死该进程!通过在我的 crontab 上执行该脚本,每 1 分钟进行一次验证。

结果? Safari 的性能现在好多了! :) 我不需要每隔一个半小时手动重置一次 Safari,这让我很高兴 :) (尽管,后台也发生了几乎相同的情况)

副作用? Weeell。刷新标签并不总是我们所希望的。我能想到一个明显的副作用。如果您的“Safari Web Content”进程在您正在进行某些重要操作时关闭,您将失去一切。例如,如果我在提示时内存消耗太高,并且脚本终止了我的内容进程,那么我将失去所有内容。

很高兴,我已经使用该脚本几天了,并且仍然没有丢失任何东西。

尽管这个解决方案到目前为止给我带来了更好的浏览体验,而且我仍然没有因为丢失一些文字而烦恼,但我知道这是可能的。因此,我想知道你们是否知道如何释放进程内存的特定区域。

我问这个问题是因为当我在“Safari Web Content”的 pid 上运行 vmmap 时,我注意到所使用的大部分内存是在 Malloc 区域“JavaScriptCore FastMalloc_0xaca89d40”中分配的。这样,我认为如果我可以释放内存,我可以在 Safari 上获得更好的体验,而不会丢失太多内存(当然,这取决于 Safari 如何处理这种微妙的意外内存清除,但是我有信心)。

让我们走到最后。您认为我的方法好吗?或者您能想到我所做的事情还有其他副作用吗?另外,你知道我是否可以清除该特定区域的记忆吗?我不介意是否需要 root 访问权限(至少出于测试目的)。 最后,在使用 Safari 时,您是否也开发了自己的方式来提升浏览体验?如果有,请分享! :)

费尔南多。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仲春光 2024-12-08 17:27:26

不,您不能强制另一个进程释放内存。无论如何,这几乎肯定会使其崩溃(这实际上就是您通过终止 Safari Web Content 进程所做的事情)。

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).

傲鸠 2024-12-08 17:27:26

我做了类似的事情,只是使用 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

然后,我设置了一个 cron 作业,每天早上 3 点运行。我一整天似乎都没有感到太多的浮肿。仅在运行几天后,我的“Safari Web Content”进程才开始消耗 2GB 以上的内存。

1 3 * * * /usr/bin/osascript /path/to/restart-safari.scpt

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文