I have a part in my project, where I have to fill many arrays and dictionaries with custom classes. During the process the memory usage starts to increase of course. But at the end when I remove items from everywhere using removeAllObjects, the memory usage remains on the same level instead of decreasing to the starting value.
I made a simplified code which does not make sense of course but reproduces the original issue I am getting:
Header:
@interface ViewController : UIViewController
@property (strong, nonatomic) NSMutableArray *imageArray;
@property (strong, nonatomic) NSTimer *timer;
- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;
@end
Implementation:
@implementation ViewController
....
- (IBAction)start:(id)sender
{
[self.loadingIndicator startAnimating];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(addImages) userInfo:nil repeats:YES];
}
- (IBAction)stop:(id)sender
{
[self.loadingIndicator stopAnimating];
[self.timer invalidate];
[self.imageArray removeAllObjects];
}
- (void)addImages
{
int i = 0;
while (i < 500)
{
UIImage *image = [UIImage imageNamed:@"test.png"];
[self.imageArray addObject:image];
i++;
}
}
...
@end
Issue:
When I call start, after 1 min the memory usage increases from 3.9 MB to 58 MB. It is fine, but when I call stop, where removelAllObjects is called, it is still remains the same value, 58 MB.
What am I doing wrong? I am using ARC!