malloc_size
返回指針?biāo)赶驅(qū)ο笞止?jié)數(shù)杖剪。但是這種方法不會考慮到對象成員變量指針?biāo)赶驅(qū)ο笏加玫膬?nèi)存服赎。跟instrument 的 Allocation計算出來的值相同。
官方解釋:
The malloc_size() function returns the size of the memory block that backs the allocation pointed to by ptr. The memory block size is always at least as large as the allocation it backs, and may be larger.
** 單個對象占用的內(nèi)存大小 **
ARC 模式下:
#import <malloc/malloc.h>
NSLog(@"Size of %@: %zd", NSStringFromClass([myObject class]), malloc_size((__bridge const void *) myObject));
__bridge 用來關(guān)鍵字進(jìn)行對象轉(zhuǎn)換。
沒有用ARC :
#import <malloc/malloc.h>
NSLog(@"size of myObject: %zd", malloc_size(myObject));
** 集合中的所有對象占用的內(nèi)存大小 **
比如數(shù)組:
NSArray *myArray = [myDictionary allValues];
id obj = nil;
int totalSize = 0;
for(obj in myArray)
{
totalSize += malloc_size(obj);
}
其他方法
#import <malloc/malloc.h>
#import <objc/runtime.h>
NSLog(@"Object Size: %zd", malloc_good_size(class_getInstanceSize([yourObject class])));