Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 16, 2025

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:

  • Wrap takeScreenshot() in autoreleasepool to release CGDisplayCreateImage() results immediately (20-50MB per full-screen capture)
  • Wrap overlay background image capture in autoreleasepool
  • Add explicit cleanup to release overlay state resources

OCR processing:

  • Clear ocrImage from EZQueryModel after silent screenshot OCR completes
  • Wrap Vision request execution in autoreleasepool (legacy and modern APIs)
  • Wrap NSImage.toCGImage() conversion in autoreleasepool to release intermediate NSBitmapImageRep

Example

// Before: CGImage lives until run loop completes
func takeScreenshot(rect: CGRect? = nil) -> NSImage? {
    let displayImage = CGDisplayCreateImage(displayID)  // 30MB retained
    let croppedImage = displayImage.cropping(to: rect)
    return NSImage(cgImage: croppedImage, size: .zero)
}

// After: Released immediately
func takeScreenshot(rect: CGRect? = nil) -> NSImage? {
    return autoreleasepool {
        let displayImage = CGDisplayCreateImage(displayID)
        let croppedImage = displayImage.cropping(to: rect)
        return NSImage(cgImage: croppedImage, size: .zero)
    }
}

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>### 请先确认以下事项:

  • 已仔细阅读了 README
  • issues 页面搜索过问题(包括已关闭的 issue),但未能找到解决方法
  • Easydict 已升级到最新版本

问题描述

WX20230430-210849@2x
WX20230430-211101@2x

重现步骤

期望结果

优化内存

解决方案(可选)

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 仪表盘 😩

这次新版本代码变动比较多,我没注意测试,可能又哪里泄露了。。稍后查一下。


取词问题,某些应用可能会失败的,看文档

目前支持多种鼠标快捷取词方式:双击取词、鼠标拖动取词、Shift 取词(多段落)和三击取词(段落),在某些应用中【鼠标拖动取词】和【Shift 取词】可能会失败,此时可换其他取词方式。(快捷键取词在任意应用中都可以正常工作)


关于【输入翻译】没有清空之前内容,可以看看这个讨论 #19

如果没有选中文本,Option + D 就清空输入并显示查询窗口。

我该把它写到文档里的 😤

@tisfeng 现在也添加清空输入内容的快捷键 Cmd+K @tisfeng 很奇怪,可能是 sublime 比较独特,我查一下。 @tisfeng 看了下,Sublime Text 不支持 Auxiliary,并且错误类型还与众不同 😓

我准备搞个白名单,单独支持一下这类特立独行的应用吧。

看一下你电脑上 Sublime Text 的 bundle id,这个应用好像有好几个版本。</comment_new>
<comment_new>@tisfeng
发现不少用户有清空输入的需求,而且我之前也没考虑到取词失败时无法清空输入的情况,那下个版本还是加一个输入翻译是否【清空查询内容】选项吧。

我觉得设置里面可以加个是否清空的选项</comment_new>
<comment_new>@tisfeng
可以,我用的也是这个。</comment_new>
<comment_new>@tisfeng
确实存在内存问泄露情况,但这个问题比较麻烦,我努力查一查。

你这个内存问题,升级之前也有吗? 还记得使用了多少天导致占用这么多内存吗?</comment_new>
<comment_new>@tisfeng
这就奇怪了,我还没升级系统,暂时不能确定是否是系统问题。

我朋友没有升级,是正常的。

@Jerry23011 你升级了系统吗?麻烦你有空跟进一下这个问题,看能否找出具体原因,或者复现步骤。

@Jerry23011 > @Jerry23011 你升级了系统吗?麻烦你有空跟进一下这个问题,看能否找出具体原因,或者复现步骤。

我会在今年圣诞节左右升级 macOS 14,我平时对系统稳定性要求比较高,有时间我看看能不能弄一个跑新系统的虚拟机

我最近升级到macos 14了,内存到了1.3G左右

@oclld 这个问题是每次使用都会出现吗?你在 macOS 14 Beta 几?</comment_new>
<comment_new>@Jerry23011

截图翻译的话每使用一次暴增几百M,应该存在内存泄露问题

装了个 14 的虚拟机,和@oclld 的结果差不多,截图的面积越大占用增加越快,只不过我到900M+就不再增大了
截屏2023-07-19 16 07 32

@tisfeng 我这边连续测试了二三十次,大概稳定在 350M 内存,并不会无限增长。 这个内存占用似乎也和用户的电脑环境有关,比如用户系统内存的大小等。

截图这块确实可能存在问题,这部份功能是借用了第三方的代码,之前已经修复过一次内存泄露问题了。。这个我再看看吧 😥

image @tisfeng @AkaShark 如果你方便的话,可以尝试看看这个内存泄漏问题,长时间使用会导致内存占用太多,需要解决一下。 @AkaShark > @AkaShark 如果你方便的话,可以尝试看看这个内存泄漏问题,长时间使用会导致内存占用太多,需要解决一下。

okay 我看了下前面的comment,看起来是存在两种场景会有内存占用大的情况,一种是[#7 ]长时间使用,还有一种是上面提到的在Sonoma 14.0上不同位置多次截图。Let me see see</comment_new>
<comment_new>@AkaShark

@AkaShark 如果你方便的话,可以尝试看看这个内存泄漏问题,长时间使用会导致内存占用太多,需要解决一下。

okay 我看了下前面的comment,看起来是存在两种场景会有内存占用大的情况,一种是[#7 ]长时间使用,还有一种是上面提到的在Sonoma 14.0上不同位置多次截图。Let me see see

确实有复现,发现是每次截图之后内存就会涨点,并且持续不下降,感觉 #7 的长时间使用大概率就是内存持续叠加导致后面达到一个比较大的占用。
CleanShot 2023-12-27 at 10 13 38@2x
CleanShot 2023-12-27 at 10 17 42@2x

@tisfeng 截图功能是存在一些问题,包括内存占用等。

@AkaShark 稍后你有空的话,我们先将截图功能用 Swift 给重写了,看是自己写或者找找第三方库。</comment_new>
<comment_new>@AkaShark

截图功能是存在一些问题,包括内存占用等。

@AkaShark 稍后你有空的话,我们先将截图功能用 Swift 给重写了,看是自己写或者找找第三方库。

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.

Copilot AI and others added 3 commits November 16, 2025 12:03
Co-authored-by: tisfeng <25194972+tisfeng@users.noreply.github.com>
Co-authored-by: tisfeng <25194972+tisfeng@users.noreply.github.com>
Co-authored-by: tisfeng <25194972+tisfeng@users.noreply.github.com>
Copilot AI changed the title [WIP] Optimize memory usage based on user feedback Fix memory accumulation in screenshot and OCR operations Nov 16, 2025
Copilot AI requested a review from tisfeng November 16, 2025 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐞 反馈问题:内存占用有点高哦

2 participants