I have been using the Instruments Leak Tester and it gives a number for Total Allocations for an app around 1-3 meg.
But, when using the task_info it is reporting much larger memory amounts like 10-20 meg.
I guess I just want to confirm that the task_info is returning some sort of total memory including stack / etc where the leak tester is just reporting Malloc / Alloc memory.
Also, why would the task_info number be increasing quite a bit during the app when the leak tester is not increasing that much....
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory in use (in bytes): %u", info.resident_size);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
Those numbers cannot be compared really. Even pages belonging to a (shared) memory mapped file (e.g. a library) will count as resident pages for the task. But they will be ignored by the Leak Tester.
The important thing to note is that there is a conceptual difference between memory available to the process (in whatever way: readonly, read/write, executable or not) and memory allocated by you, within your program. Not all available memory is connected to an actual allocation you did (e.g. a shared library) and not all memory you allocate is necessarily resident in memory (e.g. a big malloc will not reserve physical memory for you right away, but only as soon as it is used).
You can test the impact of this by mapping an anonymous region of memory (or a file) using:
#include <sys/mman.h>
// allocate anonymous region of memory (1 mb)
char *p = mmap(NULL,1024*1024,PROT_WRITE|PROT_READ,MAP_PRIVATE|MAP_ANON,0,0);
// actually access the memory, or it will not be resident
int sum=0;
for(int i=0;i<1024*1024;i++ ) sum += p[i];
You can easily change this to mmap
a file, by passing an fd to mmap
and changing MAP_ANON
to MAP_FILE
.
Also, presumably the leak tester looks from the malloc
(library) call onward until a corresponding free
, while the actual memory reservation is done just one level lower, e.g. using a mmap
(system) call just like the one above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With