1

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.

Nitin
  • 7,455
  • 2
  • 32
  • 51
  • What have you tried in first place? Also Search for **"Handling Memory Warning iOS"** in Google first. – Parth Bhatt Mar 14 '12 at 04:39
  • @ParthBhatt:I did and found this http://stackoverflow.com/questions/5980636/ios-low-memory-crash-but-very-low-memory-usage – Nitin Mar 14 '12 at 04:50
  • Release all the outlets in ViewDidUnload() method. – Naveen Thunga Mar 14 '12 at 04:52
  • 1
    @Naveen: I think it will be useful when you switching the view or when your view unload.It will not work with same view. – Nitin Mar 14 '12 at 04:55
  • How much memory are you using when you get the warning? A general advice here is to keep memory footprint as low as possible _all the time_. – Costique Mar 14 '12 at 05:07
  • 1
    You mentioned that your app running time is a factor in memory issues. If you're sure you have no zombies & memory leaks then you probably have some kind of buffer (trip history or something) that is growing with the time. If it's correctly allocated it won't show as a leak. Do you have this kind of data structure in your app? If yes, set it's limits and start disposing of the oldest data when it comes to this. – Rok Jarc Mar 14 '12 at 08:53
  • @rokjarc: Yes, You were right, My memory usage increase because of CGContextRef.Whenever CGContextRef is call at that time memory will increase and than it won't release.I have tried `CGContextRelease(context)` for release my context but that it shows me invalid context error.Please provide some guideline if you have any idea. – Nitin Mar 23 '12 at 09:42
  • Good, you found the source :) Hope it will work for you. – Rok Jarc Mar 23 '12 at 09:46
  • @rokjarc: Yes,I found the source but i don't know how to overcome it.Please tell me how can i deal with CGContextRef in multitreading programing. Thanks. – Nitin Mar 23 '12 at 09:57
  • 1
    I'm affraid that you're gonna have to move drawing code to main thread. I'm not an expert on this field so don't take my word for it. But as far as i know all of the UI-related code has to be on main thread. You can use other threads for data manipulation and communication etc. – Rok Jarc Mar 23 '12 at 10:02
  • @rokjarc:Thanks. I think it should work.I will change my code and will see if it's make any difference. – Nitin Mar 23 '12 at 10:07

1 Answers1

1

The first thing you need to do is measure your application to figure out where the memory growth is coming from. While you may have no leaks, you obviously have a memory growth problem. One tool that will help you identify where memory growth is coming from is the Allocations tools in Instruments. Specifically, you can use a technique called heapshot analysis to help narrow down what is causing the growth in your application. For more information, checkout this post on heapshot analysis.

Doug Richardson
  • 10,483
  • 6
  • 51
  • 77