My App makes a recursive call every 2 seconds using NSTimer, now i have 3 NSTimers making recursive calls. when i take these timers out the app runs fine and memory usage is not increasing rapidly but still increasing slowly. Ok now that the NSTimers are out i still get Memory Usage going up and this is my find on Instruments:
Not sure what to do next to find the memory usage going up, can anyone point me to the right directions, i will show code as well if needed.
If i add my timers back this is the recursive call being made by the three timers:
-(NSString*)setupPhpCall:(NSString*)requestString :(NSString*)sciptPage{
@autoreleasepool {
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:0];
[NSURLCache setSharedURLCache:sharedCache];
NSData *myRequestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
// Create your request string with parameter name as defined in PHP file
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat: @"http://www.xxx.co.uk/%@", sciptPage]]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSHTTPURLResponse* urlResponse = nil;
NSError *error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error: &error];
// Log Response
[sharedCache removeAllCachedResponses];
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
//NSLog(@"%@",response);
return response;
}
return @"";
}
This is what the timer runs every two seconds:
-(void)recurseForumActivity:(NSTimer *)timer{
@autoreleasepool {
__weak dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
myRequestStringForum = [NSString stringWithFormat:@"lastDate=%@&threadTitle=%@&threadCountry=%@&threadCategory=%@&threadSubCategory=%@&getData=0",lastDateForumActivity,searchThreadTitle, searchThreadCountry, searchThreadCategory, searchThreadSubCategory];
responseForum = [self setupPhpCall:myRequestStringForum :@"xxx.php"];
dispatch_async(dispatch_get_main_queue(), ^{
if(responseForum.length > 0 && ![responseForum isEqualToString:@"[]"]){
labelNewForumThreads.text = [NSString stringWithFormat:@"%@ new threads...", responseForum];
if(imageviewForumAlert == NULL){
UIImage *image = [UIImage imageNamed:@"alert.png"];
imageviewForumAlert = [UIImageView new];
[viewNav1 addSubview:imageviewForumAlert];
imageviewForumAlert.translatesAutoresizingMaskIntoConstraints = NO;
imageviewForumAlert.image = image;
NSDictionary *viewsDictionary = @{@"imageviewForumAlert":imageviewForumAlert};
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-19-[imageviewForumAlert(12)]-19-|"
options:0
metrics:nil
views:viewsDictionary];
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-19-[imageviewForumAlert(12)]-19-|"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraint_H];
[self.view addConstraints:constraint_V];
}else{
imageviewForumAlert.hidden = NO;
}
/**NSDictionary *dic = [response JSONValue];
if((NSNull*)dic != [NSNull null]){
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", dic.count];
}**/
}else{
imageviewForumAlert.hidden = YES;
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", 0];
}
/**else{
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", 0];
}**/
});
});
}
}
I have modified the recursive call to this and has stopped memory increasing rapidly but still goes up slowly, i have added this to give the call some time [NSThread sleepForTimeInterval:2.0f]:
-(void)recurseForumActivity{
@autoreleasepool {
__weak dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
myRequestStringForum = [NSString stringWithFormat:@"lastDate=%@&threadTitle=%@&threadCountry=%@&threadCategory=%@&threadSubCategory=%@&getData=0",lastDateForumActivity,searchThreadTitle, searchThreadCountry, searchThreadCategory, searchThreadSubCategory];
responseForum = [self setupPhpCall:myRequestStringForum :@"getThreadRecurse.php"];
[NSThread sleepForTimeInterval:2.0f]; // waits 2 seconds then gives the response
dispatch_async(dispatch_get_main_queue(), ^{
if(responseForum.length > 0 && ![responseForum isEqualToString:@"[]"]){
labelNewForumThreads.text = [NSString stringWithFormat:@"%@ new threads...", responseForum];
if(imageviewForumAlert == NULL){
UIImage *image = [UIImage imageNamed:@"alert.png"];
imageviewForumAlert = [UIImageView new];
[viewNav1 addSubview:imageviewForumAlert];
imageviewForumAlert.translatesAutoresizingMaskIntoConstraints = NO;
imageviewForumAlert.image = image;
NSDictionary *viewsDictionary = @{@"imageviewForumAlert":imageviewForumAlert};
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-19-[imageviewForumAlert(12)]-19-|"
options:0
metrics:nil
views:viewsDictionary];
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-19-[imageviewForumAlert(12)]-19-|"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraint_H];
[self.view addConstraints:constraint_V];
}else{
imageviewForumAlert.hidden = NO;
}
/**NSDictionary *dic = [response JSONValue];
if((NSNull*)dic != [NSNull null]){
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", dic.count];
}**/
}else{
imageviewForumAlert.hidden = YES;
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", 0];
}
/**else{
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", 0];
}**/
[self recurseForumActivity]; //calls same function
});
});
}
}
Also when i run Xcode and Instruments at the same time, the memory usage shoots up, why is this?
This is what Xcode shows in its memory usage:
On average my memory usage now runs slow but goes up 300kb every 15 seconds
I found where the memory usage is going up, it's at this call after i trial and error:
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error: &error];
How do i stop this from happening?