1

I created 150 UIwebviews and stored in an NSArray called myArray .After using those webviews , I do not need those 150 webviews . Then I created another 100 UIWebViews and stored in myArray. I thought first 150 webviews will be automatically deallocated. But when i checked in Instruments , 250 web views are alive. How to release the memory manually ? I am using ARC. So I am not able to use release method.Please advise.

2
  • It surely depends at which point in the program you saw that memory still live. Unless you have a cyclic reference with any of the webviews/ NSArray, ARC should manage it nicely. Commented Apr 28, 2014 at 12:03
  • On addition to removing the web views from the array, if you had added them to a view, did you remove them from their superview? Commented Apr 28, 2014 at 12:45

3 Answers 3

1

Using [NSMutableArray removeAllObjects]:

NSMutableArray *webViews = [NSMutableArray new];

for (NSInteger i = 0; i < 150; i++)
    [webViews addObject:[self createWebView]];

[webViews removeAllObjects];

for (NSInteger i = 0; i < 100; i++)
    [webViews addObject:[self createWebView]];
Sign up to request clarification or add additional context in comments.

2 Comments

Not really on topic with this question, but why you use new instead of alloc init?
@LordZsolt To save typing and it looks neater; otherwise the two styles are identical.
1

When adding an object to an array, it gets retained - so you need to empty the array to release the objects.

Incidentally, a UIWebView is a very heavy object. You might consider using one UIWebView, and storing NSURLs in the array, loading them into the UIWebView as needed.

Comments

0

Create an autorelease pool around the code that uses the UIWebViews. Something like this:

@autoreleasepool {
    // Create and use your UIWebViews here.  Put them into an array, do whatever you want with them
}

// When you exit the @autoreleasepool block, the objects you created inside the block will be freed

Comments

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.