2

In my iphone app while sending some packets app is crashing when there are more number of packets and its throwing an alert on xcode saying terminating app due to memory pressure. In my code in few places I have allocating some objects in a for loop and adding those allocated objects to a queue so just after adding that i want to release those objects inside the for loop , since its ARC enabled project i cant release it , my question is will nil Help in this case? instead of release it, if we set those objects to nil,,will it release the memory (i know nil will not decrease the retain count) is setting nil will help to reduce the memory usage?

say my code is some what like the following example

NSMutableArray* arrObj = [[NSMutableArray alloc]init];
for(i=0; i<=count;i++)
{
  ClassA * Obja = [[classA alloc]initwithdata:xx];
  ClassB * Objb = [[classB alloc]initwithdata:xx];
  ClassC * Objc = [[classC alloc]initwithdata:xx];

  [arrObj addObject:obja]; // Since its ARC we cant release obja will obja=nil this help?
  [arrObj addObject:objb]; // Since its ARC we cant release objb will objb=nil this help?
  [arrObj addObject:objc]; // Since its ARC we cant release objc will objc=nil this help?

}
Ravi Kiran
  • 691
  • 3
  • 9
  • 22

4 Answers4

1

ARC will automatically release it when you no longer need them, you don't have to nil those.

But iIf you have a lot of temporary data you could use an Autorelease Pool Block.

while (x < y){
   @autoreleasepool {
      // lot of temporary stuff
   }
}

You can find really nice and easy tutorials here about Xcode Instruments to address memory management problems.

Laszlo
  • 2,803
  • 2
  • 28
  • 33
  • thanx for ur reply,,So setting nil to an object will not tell compliler to release ? (I was thinking compiler will immediately remove the objects from memory which are set to nil ) – Ravi Kiran Feb 27 '14 at 12:42
  • I will release it when you no longer hold reference for it. This is why you need weak connections to your delegate or you will make a retain cycle where both elements hold by each other. http://stackoverflow.com/questions/8449040/why-use-weak-pointer-for-delegation – Laszlo Feb 27 '14 at 12:47
0

ARC doesn't get rid of retains, releases and autoreleases, it just adds in the required ones for you. So there are still calls to retain, there are still calls to release, there are still calls to autorelease.

You should use the Allocations instrument to narrow the problem. Apple's article about spotting memory issues: Locating Memory Issues in Your App

Use @autoreleasepool blocks. They provide a mechanism whereby you can relinquish ownership of an object, but avoid the possibility of it being deallocated immediately. Apple's Advanced Memory Management Programming Guide explains it:

At the end of the autorelease pool block, objects that received an autorelease message within the block are sent a release message—an object receives a release message for each time it was sent an autorelease message within the block.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
0

Why do you allocate those classA,classB and ClassC in the loop. Consider them to be allocated outside and let them be having there data changed each time. that is the better way of doing.

You may want to just clean the data in between only.

Abin George
  • 644
  • 6
  • 21
0

Try this

NSMutableArray* arrObj = [[NSMutableArray alloc]init];
ClassA * Obja = [[classA alloc]initwithdata:xx];
ClassB * Objb = [[classB alloc]initwithdata:xx];
ClassC * Objc = [[classC alloc]initwithdata:xx];
for(i=0; i<=count;i++)
{
[arrObj addObject:obja]; 
[arrObj addObject:objb]; 
[arrObj addObject:objc]; 
}
Charan Giri
  • 1,097
  • 1
  • 9
  • 15