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.
3 Answers
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]];
2 Comments
Lord Zsolt
Not really on topic with this question, but why you use new instead of alloc init?
trojanfoe
@LordZsolt To save typing and it looks neater; otherwise the two styles are identical.
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
NSArray, ARC should manage it nicely.