title: How can we handle memory level warning in iPhone?
body: <p>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.</p>
  
  <p>We have checked memory leaks, Zombies lot's of time. But there is no memory leak and zombie in our application.</p>
  
  <p>Please advice how to handle memory warning level. Please draw some line to overcome memory level warning.</p>
  
  <p>////Update...
  I found that memory usage increase because of</p>
  
  <p><code>CGContextRef context = UIGraphicsGetCurrentContext();</code></p>
  
  <p>I have used multithreading for draw poly-annotation and for that i used CGContextRef which increases my memory usage whenever i call it.</p>
  
  <p>i don't know how release context because if i release it than next time it shows me </p>
  
  <blockquote>
    <p><strong>Invalid Context error</strong> i am positing my drawing code here.Please help me if anybody have any idea.</p>
  </blockquote>
  
  <pre><code>-(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 &lt;= 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 &amp;&amp; ([locations length]/16 &gt; 1)) {       
  
          UIColor *color;
          if (speed &lt;= 20) {
              color = [UIColor colorWithRed:222/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
          }
          else if (speed &lt;= 40) {
              color = [UIColor colorWithRed:253/255.0 green:91/255.0 blue:2/255.0 alpha:1.0];
          }
          else if (speed &lt;= 60) {
              color = [UIColor colorWithRed:253/255.0 green:145/255.0 blue:4/255.0 alpha:1.0];
          }
          else if (speed &lt;=80) {
              color = [UIColor colorWithRed:255/255.0 green:212/255.0 blue:4/255.0 alpha:1.0];
          }
          else if (speed &gt;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 &lt;= 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();    
  </code></pre>
  
  <p>}</p>
  
  <p>Thanks In Advance.</p>