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

ぃ、小莉子 提交于 2019-12-20 03:05:11

问题


It seems that my app is abandoning memory since the persistent memory of recorded heapshots don't fall to zero and the heap continue to grow when the same set of operations are continuously repeated:

To find out the problems, many people suggest Bill's site that is not useful for me since I am using ARC, whereas he points to reference issues. I then followed Apple docs and watched some videos relating to abandoned memory from the WWDCs. They all say that instruments will help to point to the line of code that might cause the problems. So I reviewed every single object but didn't see anything relating to my code:

I get stuck with it now, if no one can help, I might submit the app and pray that they won't reject my app. :D

Thanks for reading.


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/13580883/how-to-clear-abandoned-memory-that-doesnt-point-to-own-code

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