Here is my code: Header:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
UIImageView *imageView;
NSMutableArray *arrayWithImages;
}
- (IBAction)startAnimation:(id)sender;
- (IBAction)cleanMemory:(id)sender;
@end
Implementation:
#import "ViewController.h"
@implementation ViewController
......
- (IBAction)startAnimation:(id)sender {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
arrayWithImages = [[NSMutableArray alloc] initWithObjects:
[UIImage imageNamed:@"pic1"],
[UIImage imageNamed:@"pic2"],
[UIImage imageNamed:@"pic3"],
[UIImage imageNamed:@"pic4"],
[UIImage imageNamed:@"pic5"],
[UIImage imageNamed:@"pic6"],
[UIImage imageNamed:@"pic7"],
[UIImage imageNamed:@"pic8"],nil];
imageView.animationImages = arrayWithImages;
imageView.animationDuration = 3;
imageView.animationRepeatCount = 1;
[self.view addSubview:imageView];
[imageView startAnimating];
}
- (IBAction)cleanMemory:(id)sender {
[arrayWithImages removeAllObjects];
[arrayWithImages release];
arrayWithImages= nil;
[imageView removeFromSuperview];
[imageView release];
imageView = nil;
}
@end
I have ViewController and its view with two buttons. First button with startAnimation action, which creates UIImageView , NSMutableArray and starts animation on it. Second button with cleanMemory action , which clean all what i've created in startAnimation.
When i start Profile with Activity Monitor instrument, my program have 4 mb Real Mem, when i press startAnimation button it's changes to 16 mb Real Mem and after animation i press cleanMemory button, but it has same 16 mb Real Mem... Why? I wont to clean my memory to started value( 4 mb Real Mem). Please, can you explain, where i have problems?
UIImage imageNamed:caches the images and will release the memory on it's own schedule. If you do not want the caching, that is completely control the memory then load the image directly,not withUIImage imageNamed:.From the Apple docs:
You can use
to load the image directly.
From the Apple docs: