Question

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.

Was it helpful?

Solution

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]];

OTHER TIPS

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.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top