简体   繁体   中英

Memory management issue

Currently, I am facing the most severe problem of all time while creating iphone applications which is memory management issue. I am reading the XML file and storing in the database using SQLITE. While reading the XML file, I create an object of the class file TestFile and allocate the space to it. So when it reads a start tag it allocates the space and when it finds the end tag, it will release the space and deallocate for next tags. //class TestFile

@interface TestFile : NSObject {

NSString *id;
NSString *number;
NSNumber *size_A;
NSNumber *size_B;
NSNumber *places;
NSString *spaces;
NSString *name;
NSString *print;
NSString *description;
NSNumber *formats;
NSString *purchases;
NSNumber *active;
UIImage *appLogo;
NSUInteger starred;
}

But for some reason, when I check the Instruments to know the memory allocation, the memory space allocated to testFile = [[TestFile alloc] init] is around 32 KB which is killing the application to load on iphone 3GS. Am I doing some thing wrong here or can I do it in some other way ?

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {

if([elementName isEqual:@"root"]) {
    self.exhArray = [NSMutableArray array] ;
}

else if([elementName isEqualToString:@"object"])
{
    testFile = [[TestFile alloc] init] ;
}
}

  - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

if(!currentElementValue) 
    currentElementValue = [[NSMutableString alloc] initWithString:string];
else
    [currentElementValue appendString:string];
}

 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

     if([elementName isEqualToString:@"root"])
    return;

     if([elementName isEqualToString:@"object"]) {
    [self.exhArray addObject:testFile];
    [testFile release];
    testFile = nil;
}
else if([elementName isEqualToString:@"key_id"])
    [testFile setValue:currentElementValue forKey:elementName];
 [currentElementValue release];
currentElementValue = nil;

}

Just a shot in the dark - the XML parsing may be ok - though I did not check too specifically on your code.

I would suggest you to check on the UIImage instance appLogo . Once instantiated, that baby will contain a decoded image (commonly 4 byte per pixel). Often enough, you may come across images that seem small enough when looking at their file-size. You should however look at their total resolution to get the effective memory needed for keeping that data in memory.

Another suggestion would be to add some logging into your parsing code to see if the expected is actually happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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