Fix memory accumulation in screenshot and OCR operations #1028
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Memory usage grows from ~200MB to 900MB+ after repeated screenshot/OCR operations on macOS 14. CGImage objects and Vision framework temporaries accumulate in the default autorelease pool until run loop completion.
Changes
Screenshot capture:
takeScreenshot()inautoreleasepoolto releaseCGDisplayCreateImage()results immediately (20-50MB per full-screen capture)autoreleasepoolOCR processing:
ocrImagefromEZQueryModelafter silent screenshot OCR completesautoreleasepool(legacy and modern APIs)NSImage.toCGImage()conversion inautoreleasepoolto release intermediateNSBitmapImageRepExample
Impact: Peak memory stays at 200-350MB regardless of operation count, returning to baseline within seconds.
Files: 7 changed (+80/-55 lines)
Original prompt
This section details on the original issue you should resolve
<issue_title>🐞 反馈问题:内存占用有点高哦</issue_title>
<issue_description>### 请先确认以下事项:
问题描述
重现步骤
无
期望结果
优化内存
解决方案(可选)
No response
设备信息 && 操作系统版本(可选)
No response</issue_description>
<agent_instructions>根据用户的反馈评论,帮我仔细调查这个问题,并提供修复方案。</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@tisfeng 怎么又关闭了?我还准备看看呢 🤨 @tisfeng 900多,这个内存占用确实有问题,正常应该是 200多 @tisfeng 哦,你说前面这个吗 https://github.com//issues/7 ,内存问题没办法一劳永逸,只能阶段性处理,每次加新代码,都有可能导致内存泄露,除非时时盯着 Debug 仪表盘 😩这次新版本代码变动比较多,我没注意测试,可能又哪里泄露了。。稍后查一下。
取词问题,某些应用可能会失败的,看文档
关于【输入翻译】没有清空之前内容,可以看看这个讨论 #19
我该把它写到文档里的 😤
@tisfeng 现在也添加清空输入内容的快捷键 Cmd+K @tisfeng 很奇怪,可能是 sublime 比较独特,我查一下。 @tisfeng 看了下,Sublime Text 不支持 Auxiliary,并且错误类型还与众不同 😓我准备搞个白名单,单独支持一下这类特立独行的应用吧。
看一下你电脑上 Sublime Text 的 bundle id,这个应用好像有好几个版本。</comment_new>
<comment_new>@tisfeng
发现不少用户有清空输入的需求,而且我之前也没考虑到取词失败时无法清空输入的情况,那下个版本还是加一个输入翻译是否【清空查询内容】选项吧。
你这个内存问题,升级之前也有吗? 还记得使用了多少天导致占用这么多内存吗?</comment_new>
<comment_new>@tisfeng
这就奇怪了,我还没升级系统,暂时不能确定是否是系统问题。
@Jerry23011 你升级了系统吗?麻烦你有空跟进一下这个问题,看能否找出具体原因,或者复现步骤。
@Jerry23011 > @Jerry23011 你升级了系统吗?麻烦你有空跟进一下这个问题,看能否找出具体原因,或者复现步骤。我会在今年圣诞节左右升级 macOS 14,我平时对系统稳定性要求比较高,有时间我看看能不能弄一个跑新系统的虚拟机
@oclld 这个问题是每次使用都会出现吗?你在 macOS 14 Beta 几?</comment_new>
<comment_new>@Jerry23011
装了个 14 的虚拟机,和@oclld 的结果差不多,截图的面积越大占用增加越快,只不过我到900M+就不再增大了

@tisfeng 我这边连续测试了二三十次,大概稳定在 350M 内存,并不会无限增长。 这个内存占用似乎也和用户的电脑环境有关,比如用户系统内存的大小等。截图这块确实可能存在问题,这部份功能是借用了第三方的代码,之前已经修复过一次内存泄露问题了。。这个我再看看吧 😥
okay 我看了下前面的comment,看起来是存在两种场景会有内存占用大的情况,一种是[#7 ]长时间使用,还有一种是上面提到的在Sonoma 14.0上不同位置多次截图。Let me see see</comment_new>
<comment_new>@AkaShark
确实有复现,发现是每次截图之后内存就会涨点,并且持续不下降,感觉 #7 的长时间使用大概率就是内存持续叠加导致后面达到一个比较大的占用。


@tisfeng 截图功能是存在一些问题,包括内存占用等。@AkaShark 稍后你有空的话,我们先将截图功能用 Swift 给重写了,看是自己写或者找找第三方库。</comment_new>
<comment_new>@AkaShark
okay</comment_new>
<comment_new>@tisfeng
我本来想找一个开源的 Swift 截图库,但没找到,要不我们直接用 macOS 系统的截图命令 screencapture 来实现截图功能?</comment_new>
<comment_new>@tisfeng
我尝试用 Swift 重写了截图功能 #846 ,稍后看看效果。</comment_new>
<comment_new>@tisfeng
不是这样的,上个版本我们只是重写了截图...
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.