KopBob
KopBob

Reputation: 1151

How to clean memory after UIImageView animation

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?

Upvotes: 2

Views: 3205

Answers (2)

aqs
aqs

Reputation: 5682

If even after using + (UIImage *)imageWithContentsOfFile:(NSString *)path you still don't get to free memory, tyr calling imageView.animationImages = nil;

Upvotes: 1

zaph
zaph

Reputation: 112857

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 with UIImage imageNamed:.

From the Apple docs:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

You can use

+ (UIImage *)imageWithContentsOfFile:(NSString *)path

to load the image directly.

From the Apple docs:

This method does not cache the image object.

Upvotes: 2

Related Questions