2

I am in the final stages of tweaking my first iphone app for release and am trying to shave off kilobytes where I can. I have a process that syncs some data with my server on start of the app, and I noticed when I commented that out, my app is using 7MB when it is done starting. When I turn it on, it is using 18MB when it is done starting. I am now trying to determine what part of the process is eating the memory and not giving it back. I have turned off most of my sync function and am left with this and it still uses 2MB of memory and does not release it when it is done:

GDataXMLDocument *syncData = [[self getXmlWithUrl:@"http://SOMEURL"] autorelease];

This just uses my helper function to go out and load up a xml document for me to use. My helper function is as follows:

-(GDataXMLDocument*)getXmlWithUrl:(NSString*)url{
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSURLResponse *resp = nil;
    NSError *err = nil;
    NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
    return [[GDataXMLDocument alloc]initWithData:response options:0 error:&err];
}

I put a release after the syncData is created, but of course it says its already de-allocated. Any ideas on what could be causing this?

box86rowh
  • 3,415
  • 2
  • 26
  • 37
  • I've been having similar issues when parsing files, so you aren't the only one seeing these types of behaviors. +1 to the question to see if it gets more attention and possibly an answer, because I am also curious to know where that extra memory usage comes from because the code itself looks good to me – justin Oct 13 '11 at 21:12
  • It is a bit annoying as I do similar things throughout my app and would like to minimize needless memory wasting. – box86rowh Oct 13 '11 at 21:23

1 Answers1

0

Two thoughts.

  1. GDataXMLDocument is a DOM based parse. How big is your response? Typically DOM parses need 2-4x of the original document size in RAM can be even more with certain text encodings. Do you really need DOM parsing? We try and do all of our stuff using SAX so it's progressive and much more memory efficient.

  2. You're using autorelease, so memory won't be released until the autorelease pool is emptied. You could wrap the code with an auto release pool and then dispose of that. http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html

Finally, synchronous HTTP requests are generally a bad idea because the performance of the network can be so variable and you'll block the UI thread.

Dan Bennett
  • 1,450
  • 1
  • 15
  • 17