malloc_size
返回指针所指向对象字节数。但是这种方法不会考虑到对象成员变量指针所指向对象所占用的内存。跟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.
单个对象占用的内存大小
ARC 模式下:
#import <malloc/malloc.h>
NSLog(@"Size of %@: %zd", NSStringFromClass([myObject class]), malloc_size((__bridge const void *) myObject));
__bridge 用来关键字进行对象转换。
没有用ARC :
#import <malloc/malloc.h>
NSLog(@"size of myObject: %zd", malloc_size(myObject));
集合中的所有对象占用的内存大小
比如数组:
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])));