作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用以下功能。每当我调用它时,它都会增加我的内存使用量。我已经检查了所有泄漏。甚至,我在函数结束时立即释放对象。在这里,我提供我的代码以供引用。请提供指南。
//函数开始处的内存正在使用的内存(以字节为单位):38936576
//函数结束时的内存(池排空后)正在使用的内存(以字节为单位):39272448
//函数
-(void)parsing:(NSMutableData *)respose
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
report_memory();
NSString *responseString = [[NSString alloc] initWithData:respose encoding:NSUTF8StringEncoding];
SBJSON *parser = [[SBJSON alloc] init];
//NSLog(@"statuses>>>>>");
statuses = [parser objectWithString:responseString
error:nil];
[parser release];
//report_memory();
refreshCounter = YES;
__block NSArray *segment =[[NSArray alloc]initWithArray:[statuses valueForKey:@"Segments"]];
int mapzoomlevel = [self getZoomLevel];
int polylinewidth = 9;
if(mapzoomlevel == 7) {
polylinewidth = 1.5;
}
else if(mapzoomlevel == 8) {
polylinewidth = 2.5;
}
else if(mapzoomlevel ==9) {
polylinewidth = 3;
}
else if(mapzoomlevel ==10) {
polylinewidth = 3.4;
}
else if(mapzoomlevel == 11) {
polylinewidth = 4;
}
else if(mapzoomlevel <= 13) {
polylinewidth = 4.3;
}
else if (mapzoomlevel == 14) {
polylinewidth = 5.4;
}
else if(mapzoomlevel== 15) {
polylinewidth = 8;
}
__block CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
//void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
bitmapBytesPerRow = (self.mapView.frame.size.width * 4);
bitmapByteCount = (bitmapBytesPerRow * self.mapView.frame.size.height);
colorSpace = CGColorSpaceCreateDeviceRGB();
// bitmapData = malloc( bitmapByteCount );
context = CGBitmapContextCreate (NULL,
self.mapView.frame.size.width,
self.mapView.frame.size.height,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast);
CGContextSetAllowsAntialiasing (context,YES);
CGColorSpaceRelease( colorSpace );
CGContextTranslateCTM(context, 0, self.mapView.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetLineWidth(context, polylinewidth);
CGContextSetAlpha(context, 0.6);
UIColor *color;
for(NSDictionary *route in segment) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSString *locations = [route valueForKey:@"Locations"];
double speed = [[route valueForKey:@"Speed"] doubleValue];
if (locations && ([locations length]/16 > 1)) {
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) {
NSAutoreleasePool *loc = [[NSAutoreleasePool alloc]init];
CLLocationCoordinate2D coordinates;
coordinates.latitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i, 16)]);
coordinates.longitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i+16, 16)]);
CGPoint point = [mapView convertCoordinate:coordinates toPointToView:self.mapView];
if (i == 0)
CGContextMoveToPoint(context, point.x, point.y);
else
CGContextAddLineToPoint(context, point.x, point.y);
[loc drain];
}
CGContextStrokePath(context);
}
[pool drain];
}
[segment release];
[polyImage release];
CGImageRef ref = CGBitmapContextCreateImage(context);
polyImage = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
CGContextRelease(context);
[responseString release];
__block NWAnotation *nannotation = [[NWAnotation alloc]initWithImage:polyImage
mapView:mapView
zoomLevel:ZOOM_LEVEL
aRoadFlag:aRoadFlag
nRoadFlag:nRoadFlag
othersRoadFlag:othersRoadFlag];
dispatch_queue_t queue1 = dispatch_queue_create("com.MyApp.AppTask",NULL);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_async(queue1,
^{
dispatch_async(main,
^{
@try {
//NSLog(@"%d",[queue operationCount]);
[self showpolyline:nannotation];
}
@catch (NSException *exception) {
NSLog(@"exception");
}
});
});
dispatch_release(queue1);
dispatch_release(main);
[nwAnotation release];
[nannotation release];
report_memory();
[pool drain];
}
谢谢。
最佳答案
要确定哪些对象导致内存增长,请尝试使用 Instruments 中的分配工具执行堆快照分析。查看this blog post了解更多详情。
附带说明一下,您释放 __block 变量注释的方式很危险,有时可能会导致崩溃。原因是 __block 变量不会被 dispatch_async 自动保留,因此在调用 [self showpolyline:nannotation] 时,nannotation 对象可能已经被释放。
关于iphone - 以下功能的内存占用量增加在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10027157/
我是一名优秀的程序员,十分优秀!