Reputation: 71
I recently started working on optimizing the memory usage in my Swift application. When I started using the Leaks Instrument, I got over 100 "Malloc" leaks with no descriptions. I've looked around, but cannot find an explanation.
I'm running iOS 12.0 and Xcode 10.2
I went as far as commenting out all of the functions that were being called in my ViewDidLoad, and I'm still getting around 50 Malloc leaks. I researched what causes memory leaks, and there's nothing in my code to suggest a leak, but I'm fairly new to memory management.
It's important for my app to not have leaks, so any help would be appreciated!
Upvotes: 4
Views: 5354
Reputation: 13375
Let's say you have a very simple code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int mem(long int mem) {
char *buffer = malloc(sizeof(char) * mem);
if(buffer == NULL) {
return 1;
} else {
return 0;
}
}
int main(int argc, const char * argv[]) {
long int mem_to_alloc = 2;
for(int i=0; i<30; i++, mem_to_alloc *= 2) {
printf("Allocation: %d Allocating: %ld\n", i, mem_to_alloc);
if( mem(mem_to_alloc) == 1 )
break;
sleep(1);
}
return 0;
}
First of all, make sure to set proper configuration in Build Scheme
Once in Instruments
choose Leaks
After you have run your code, you should see suspicious allocations with the calls stack (Stack Trace
) on the right.
Upvotes: 3