1

Here is my code. Memory has been constantly increasing,when the viewDidLoad method is called.I guess that local variable data is not released.But why?

- (void)viewDidLoad {
   [super viewDidLoad];

   NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wakeup.pcm" ofType:nil]];
NSMutableData *mData = [NSMutableData dataWithData:data];

  int readLength = 0;
  while (readLength < mData.length) {
     if (mData.length - readLength > EVERBUFFERLEN) {
        NSData *data = [mData subdataWithRange:NSMakeRange(readLength, EVERBUFFERLEN)];
        readLength += EVERBUFFERLEN;
        data = nil;
    }
}

enter image description here

alpine
  • 927
  • 6
  • 17
  • Put that while loop in an `@autoreleasepool` have a look at this answer for more reference http://stackoverflow.com/questions/9086913/objective-c-why-is-autorelease-autoreleasepool-still-needed-with-arc – sbarow Jul 31 '15 at 09:42
  • @Mozilla http://stackoverflow.com/questions/10121345/autoreleasepool-in-loop-or-loop-in-autoreleasepool – sbarow Jul 31 '15 at 09:44
  • 1
    Why are you putting the thread to sleep? – trojanfoe Jul 31 '15 at 09:48
  • @sbarow How should this help? Obviously the memory foot print increases during multiple executions of `-viewDidLoad`. – Amin Negm-Awad Jul 31 '15 at 10:04
  • Umm, not such a good idea to sleep the main thread (on which `viewDidLoad` is called). – Arkku Jul 31 '15 at 10:05
  • @AminNegm-Awad well if thats the case, the code snippet is not the problem then, but rather why the `ViewController` is not being released no? – sbarow Jul 31 '15 at 10:13
  • @sbarow Why do you think that it is not released? – Amin Negm-Awad Jul 31 '15 at 10:15
  • @AminNegm-Awad more asking the question from what you said, if memory keeps climbing and `viewDidLoad` is being called multiple times we can assume that multiple `ViewControllers` are being created right? Seeing as though memory isn't going down, those `ViewControllers` aren't being released? – sbarow Jul 31 '15 at 10:17
  • No, you cannot assume this, because $whatever can cause a higher memory usage. The system memory usage does not mean that there are leaks. – Amin Negm-Awad Jul 31 '15 at 10:28

1 Answers1

0

Such a "overall memory meter" does not really help finding leaks. I. e. it can be that -dataWithContentsOfFile: requests memory down from the system, but it is not freed after usage. Reasons for that can be that allocating memory from the system is slow and holding unused memory "for the app" can be a cheaper way as long as the memory pressure isn't high.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50