1

I am recently having a memory crash and I am suspecting that I am failing to empty the array, This is my code for the array

- (void)viewDidLoad
{
   allImagesArray = [[NSMutableArray alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *location=@"Bottoms";
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
    collectionBottoms.delegate =self;
    collectionBottoms.dataSource=self;
    for(NSString *str in directoryContent){
        NSLog(@"i");
        NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
        NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
        if(data)
        {
            UIImage *image = [UIImage imageWithData:data];
            [allImagesArray addObject:image];
            NSLog(@"array:%@",[allImagesArray description]);

        }}
}

Ho can I free the memory by releasing the object in the array?

8
  • remove image from array when you don't need them? Commented Oct 25, 2013 at 4:08
  • Exactly...as long as they are in the array they will be alive. Commented Oct 25, 2013 at 4:10
  • I can't find a place to do that, I know you put the code [allImagesArray removeAllObjects]; NSLog(@"%@", allImagesArray);}but cannot find a good place to put it. I want to save the picture first to save to document however if I put this above code it just deletes the picture that I just took. Commented Oct 25, 2013 at 4:14
  • Then don't use that method. What's wrong with removeObject: or removeObjectAtIndex:? It would be pretty foolish to have an array where you could only remove every object, don't you think? Commented Oct 25, 2013 at 4:22
  • What should I use instead, I cannot a way to do this. Commented Oct 25, 2013 at 4:23

3 Answers 3

1

You save the image array in Plist. After save you just re initialize your array it will clear the memory in array. Then are you using ARC or not? Because ARC is more comfortable for this. Then re-initialze or removeAllObject from array in -viewWillDisappear

Sign up to request clarification or add additional context in comments.

Comments

0

Hi Just release the memory once object works is over. Here is the code for you.

- (void)viewDidLoad
{
   allImagesArray = [[NSMutableArray alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *location=@"Bottoms";
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
    collectionBottoms.delegate =self;
    collectionBottoms.dataSource=self;
    for(NSString *str in directoryContent)
    {
        NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
        NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
        if(data)
        {
            UIImage *image = [UIImage imageWithData:data];
            [allImagesArray addObject:image];
            NSLog(@"array:%@",[allImagesArray description]);
        image = nil;

        }
    finalFilePath=nil;
    data=nil;
   }

 paths= nil;
 documentsDirectory= nil;
 location= nil;
 fPath= nil;
 directoryContent = nil;
}

2 Comments

This looks like it will help and it does decrease the memory but it still builds up after taking many photo. Does this have problems with the camera code or something else. This is my whole code github.com/laryufk/Helper/tree/master for the camera is there something wrong.
You have to nil or release the object once its works get over. So you can have check your code release the memory.
0

If you are using this image array, allImagesArray, when the view is visible then put this code in viewWillAppear or viewDidAppear methods of UIViewController.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

   allImagesArray = [[NSMutableArray alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *location=@"Bottoms";
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
    collectionBottoms.delegate =self;
    collectionBottoms.dataSource=self;
    for(NSString *str in directoryContent){
        NSLog(@"i");
        NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
        NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
        if(data)
        {
            UIImage *image = [UIImage imageWithData:data];
            [allImagesArray addObject:image];
            NSLog(@"array:%@",[allImagesArray description]);

        }
    }
}

And, then release it in viewWillDisappear/viewDidDisappear methods of UIViewController.

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [allImagesArray removeAllObjects];
    //[allImagesArray release]; // (Uncomment) Call this only if you are not using ARC.
}

Alternatively, a quick, dirty but safe fix is (without knowing your usage of allImagesArray) -

- (void)viewDidLoad
{
    [super viewDidLoad];

     // Remove previously added objects.
     if (allImagesArray != nil && allImagesArray.count > 0) {
            [allImagesArray removeAllObjects];
     }
     // Allocate memory if not done earlier (i.e. first time).
     if (allImagesArray == nil) {
        allImagesArray = [[NSMutableArray alloc] init];
     }

     // Rest of your code as stated in your question
}

2 Comments

I don't get the code that is written at the bottom which line should I put this and why is it viewWillappear not viewdidload?
Clarified the code for alternative approach above. viewWillAppear is called everytime view is presented on screen and viewDidDisappear when its removed of screen. These are UIViewController methods. Read official docs for more details here - developer.apple.com/library/ios/documentation/uikit/reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.