简体   繁体   English

仪器:检查 memory 泄漏查询

[英]Instruments: checking for memory leaks inquiry

I was curious, when one goes ahead and executes their code for leaks checking with Instruments -- is it prudent to hit all of the parts of the app manually to ensure memory leaks are occurring in that respective area?我很好奇,当一个人继续执行他们的代码以使用 Instruments 进行泄漏检查时 - 手动点击应用程序的所有部分以确保 memory 泄漏发生在相应区域是否谨慎? For instance, I believe I have some memory leaks in my application, deep within a UINavigationController tree.例如,我相信我的应用程序中有一些 memory 泄漏,在 UINavigationController 树的深处。 Do I go ahead and run the app checking for leaks, while manually drilling down on the iPhone to get to that part of the app?我是否提前 go 并运行应用程序检查泄漏,同时在 iPhone 上手动向下钻取以到达应用程序的该部分? Is Instruments smart enough to find it on its own? Instruments 是否足够聪明,可以自己找到它? What's the proper way of going about it?正确的方法是什么?

Thanks for any insight!感谢您的任何见解!

No, Instruments just monitors the memory allocations of your code, it does not "go" anywhere, unless your app goes there.不,Instruments 只是监视代码的 memory 分配,它不会“去”任何地方,除非你的应用程序去那里。 Actually a leak is nothing more than a piece of memory to that no reference exists anymore;实际上,泄漏只不过是 memory 的一块,不再存在参考; thus it cannot be freed anymore, since how are you going to free it in the future if you cannot even reference to it any longer?因此它不能再被释放,因为如果你甚至不能再引用它,你将如何释放它?

Instruments won't find all memory leaks that way, though.不过,仪器不会以这种方式发现所有 memory 泄漏。 If you keep references to the memory, just never use them to free the memory, Instruments won't see this as a leak, because it cannot foresee if you are going to ever free it up in the future or not.如果您保留对 memory 的引用,请不要使用它们来释放 memory,Instruments 不会将此视为泄漏,因为它无法预测您是否会在将来释放它。 As you still could free it up, it's not considered a leak.由于您仍然可以释放它,因此它不被视为泄漏。 So if you have a memory issue, it might be beneficial to not just look for leaks, but also to monitor how much memory your application is "collecting" over the time.因此,如果您有 memory 问题,那么不仅要查找泄漏,而且还要监控您的应用程序在一段时间内“收集”了多少 memory,这可能是有益的。 If this permanently rises even though it shouldn't, you might still have a leak, just none where you lose the references to the memory.如果即使它不应该永久上升,您可能仍然有泄漏,只是没有丢失对 memory 的引用。

Typically, I would focus on the module suspected of causing the leaks and then widen the scope afterward.通常,我会关注可能导致泄漏的模块,然后再扩大 scope。 While I have not used Instruments on a Mac, I have used Purify and the native Windows heap tools to do memory leak tracing in Windows programs.虽然我没有在 Mac 上使用 Instruments,但我使用 Purify 和原生 Windows 堆工具在 Windows 程序中进行 memory 泄漏跟踪。

After you have identified the source of your major leak it's never a bad idea to run the program with varying test input and generally check the program for other leaks.在您确定了主要泄漏的来源之后,使用不同的测试输入运行程序并通常检查程序是否存在其他泄漏从来都不是一个坏主意。 Even small leaks with a particular set of data can lead to much larger leaks down the road.即使是特定数据集的小泄漏也可能导致更大的泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM