swift - Swift 中未释放内存
问题描述
我在swift中有一个简单的命令行工具,它正在执行图像操作,但内存从未释放。我已经提取了代码来重现这个问题。下面的代码只是加载 50 次 tif 图像并解压缩它,我假设 Swift 应该在每个循环结束时释放我的变量,但它不会并且最终占用超过 4 GB 的内存:
let imageUrlPath = "folder/file.tif"
for _ in 0...50
{
let sourceImg = NSImage(byReferencingFile: imageUrlPath)
let sourceBitmapRep = NSBitmapImageRep(cgImage: sourceImg!.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
let sourceImageData = sourceBitmapRep.representation(using: .tiff, properties: [:])
}
RunLoop.main.run() // Total memory taken is 4.3 GB
如何强制释放内存?
解决方案
推荐阅读
- python - 在某个时间范围内只运行最后一个 python 函数调用?
- python - 使用 Python 的 eBird API
- php - Can't Upload Image Using CodeIgniter Error Showing My Filename not column on database
- gradle - 如何在 Gradle 项目中重新生成丢失的源路径?
- javascript - 如何扩展悬停导航抽屉并在较小的屏幕上使用应用栏图标?
- webpack - Webpack 抛出错误:将项目切换到 Windows 后找不到模块“./locale”
- c - printf 或 scanf 上的分段错误
- c++ - 是否将地图大小作为值插入地图未定义的行为?
- c# - 选择新建导致“LINQ to Entities 中仅支持无参数构造函数和初始化程序”
- ios - 如何快速从日期中获取时间