We have developed map base application. We have implemented multi-threading for display data on map view. While application is running longer(around 20 min), Application will display memory warning level-1. And after couple of minutes it will give memory warning level-2 and after level2 application will be crash or goes to sleep.
We have checked memory leaks, Zombies lot's of time. But there is no memory leak and zombie in our application.
Please advice how to handle memory warning level. Please draw some line to overcome memory level warning.
////Update... I found that memory usage increase because of
CGContextRef context = UIGraphicsGetCurrentContext();
I have used multithreading for draw poly-annotation and for that i used CGContextRef which increases my memory usage whenever i call it.
i don't know how release context because if i release it than next time it shows me
Invalid Context error i am positing my drawing code here.Please help me if anybody have any idea.
-(void) drawRect:(CGRect)rect {
NVPolylineAnnotation* annotation = self.annotation;
CGFloat POLYLINE_WIDTH = 9;
if(annotation.getZoomLevel == 7) {
POLYLINE_WIDTH = 1.5;
}
else if(annotation.getZoomLevel == 8) {
POLYLINE_WIDTH = 2.5;
}
else if(annotation.getZoomLevel ==9) {
POLYLINE_WIDTH = 3;
}
else if(annotation.getZoomLevel ==10) {
POLYLINE_WIDTH = 3.4;
}
else if(annotation.getZoomLevel == 11) {
POLYLINE_WIDTH = 4;
}
else if(annotation.getZoomLevel <= 13) {
POLYLINE_WIDTH = 4.3;
}
else if (annotation.getZoomLevel == 14) {
POLYLINE_WIDTH = 5.4;
}
else if(annotation.getZoomLevel == 15) {
POLYLINE_WIDTH = 8;
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetLineWidth(context, POLYLINE_WIDTH);
CGContextSetAlpha(context, 0.6);
for(NSDictionary *route in annotation.routes) {
NSString *locations = [route valueForKey:@"Locations"];
double speed = [[route valueForKey:@"Speed"] doubleValue];
if (locations && ([locations length]/16 > 1)) {
UIColor *color;
if (speed <= 20) {
color = [UIColor colorWithRed:222/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
}
else if (speed <= 40) {
color = [UIColor colorWithRed:253/255.0 green:91/255.0 blue:2/255.0 alpha:1.0];
}
else if (speed <= 60) {
color = [UIColor colorWithRed:253/255.0 green:145/255.0 blue:4/255.0 alpha:1.0];
}
else if (speed <=80) {
color = [UIColor colorWithRed:255/255.0 green:212/255.0 blue:4/255.0 alpha:1.0];
}
else if (speed >80) {
color = [UIColor colorWithRed:42/255.0 green:176/255.0 blue:39/255.0 alpha:1.0];
}
CGContextSetStrokeColorWithColor(context, color.CGColor);
for (int i = 0; i <= locations.length - 32; i += 32) {
CLLocationCoordinate2D coordinates;
coordinates.latitude = hexDecode([locations substringWithRange:NSMakeRange(i, 16)]);
coordinates.longitude = hexDecode([locations substringWithRange:NSMakeRange(i+16, 16)]);
CGPoint point = [_mapView convertCoordinate:coordinates toPointToView:self];
if (i == 0)
CGContextMoveToPoint(context, point.x, point.y);
else
CGContextAddLineToPoint(context, point.x, point.y);
}
[self setNeedsDisplay];
CGContextStrokePath(context);
}
}
context = NULL;
UIGraphicsEndImageContext();
}
Thanks In Advance.