How to clear abandoned memory that doesn't point to own code?

我是研究僧i 提交于 2019-12-02 02:02:50
Michał Kreft

I think you need to look explicitly for retain cycles. When in hierarchy you have a parent object object that has related object and both have strong type of proprties, they will never get released from memory.

Quick example:

@interface Parent : NSObject
@property (strong) Child *child;
@end
@interface Child : NSObject
@property (strong) Parent *parent;
@end

Also by default properties are strong, so it is the same if you do not declare it at all.

The way it should be:

@interface Parent : NSObject
@property (strong) Child *child;
@end
@interface Child : NSObject
@property (weak) Parent *parent;
@end

I also found information that Instruments can show you retain cycles (and it looks nice). More details here How to activate Cycles reporting in Instruments under ARC? However I don't know if it works with ARC, comments may suggest it does not. As a cumbersome way I can recommend commenting out some code you feel may be responsible and then check the picture.


So that's for the retain cycles. Another thing you should look for is when you allocate memory that ARC is not able to return. Those calls are looking like C functions and by convention have a word Create in the name. Each time you make such a pointer then you should also clean after yourself. Just to give you some examples:

  • CGColorCreate - CGColorRelease
  • CGColorSpaceCreateWithName - CGColorSpaceRelease
  • CGBitmapContextCreate - CGContextRelease

As you can see each function has it's corresponding releasing function which usually you should be able to find in documentation.

It seems that changing Build Settings\Build Options\Debug Information Format from DWARF to DWARF with dSYM File will fix the problem.

Or you are testing with Zombies turned on (edit scheme - diagnostics). If zombies are turned on, they are never deleted and thus memory always grows.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!