如何计算我在IOS中的应用程序的内存完善情况

时间:2015-04-15 08:25:02

标签: ios objective-c memory-leaks

在应用程序中,显示高分辨率图像并有一些内存泄漏,所以我想计算每个语句的内存消耗,以找出泄漏。

有没有计算内存的方法(MB或KB)?

我需要像这样:

//这是我的方法

 + (unsigned char *) convertUIImageToBitmapRGBA8:(UIImage *) image {

//Run a method to caclate the memory(MB or KB)  --- Before
CGImageRef imageRef = image.CGImage;

// Create a bitmap context to draw the uiimage into
CGContextRef context = [self newBitmapRGBA8ContextFromImage:imageRef];

if(!context) {
    return NULL;
}

size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);

CGRect rect = CGRectMake(0, 0, width, height);

// Draw image into the context to get the raw image data
CGContextDrawImage(context, rect, imageRef);

// Get a pointer to the data    
unsigned char *bitmapData = (unsigned char *)CGBitmapContextGetData(context);

// Copy the data and release the memory (return memory allocated with new)
size_t bytesPerRow = CGBitmapContextGetBytesPerRow(context);
size_t bufferLength = bytesPerRow * height;

unsigned char *newBitmap = NULL;

if(bitmapData) {
    newBitmap = (unsigned char *)malloc(sizeof(unsigned char) * bytesPerRow * height);

    if(newBitmap) { // Copy the data
        for(int i = 0; i < bufferLength; ++i) {
            newBitmap[i] = bitmapData[i];
        }
    }

    free(bitmapData);

} else {
    NSLog(@"Error getting bitmap pixel data\n");
}

CGContextRelease(context);
//Run a method to calculate the memory(MB or KB)    --- After
return newBitmap;   

}

0 个答案:

没有答案