-1

I haven't much experience with objective-c language. i am quite confuse with memory management in objective C.I know that memory management is very important factor so we have to strongly focus on that while developing.

My Question is that which basic things we have to follow to reduce the uses of memory as much as possible?

rishi
  • 11,779
  • 4
  • 40
  • 59
freelancer
  • 1,658
  • 1
  • 16
  • 37
  • Following will help you - http://stackoverflow.com/questions/106627/memory-management-in-objective-c – rishi Apr 18 '12 at 16:31
  • 2
    Unfortunately, this is far too broad of a topic to be answered here. Apple has an entire guide on the subject that I recommend reading: https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html – Brad Larson Apr 18 '12 at 17:37

2 Answers2

2

Perhaps the clearest advice I've ever seen (pre-ARC) was from Brent Simmons: How I Manage Memory

JefferyRPrice
  • 895
  • 5
  • 17
0

This is a very good question to ask, because in Objective-C there is no garbage collector. We have to manually handle the memory.

You own an object in Objective-C when you alloc it, copy it or new it. For example (I've copied this example code from http://interfacelab.com/objective-c-memory-management-for-lazy-people/):

    -(void)someMethod
{
  // I own this!
  SomeObject *iOwnThis = [[SomeObject alloc] init];

  [iOwnThis doYourThing];

  // I release this!
  [iOwnThis release];
}

-(void)someOtherMethod:(SomeObject *)someThing
{
  // I own this too!
  SomeObject *aCopyOfSomeThing = [someThing copy];

  [aCopyOfSomeThing doSomething];

  // I release this!
  [aCopyOfSomeThing release];
}

-(void)yetAnotherMethod
{
  // I own this too!
  SomeObject *anotherThingIOwn = [SomeObject new];

  [anotherThingIOwn doSomething];

  // I release this!
  [anotherThingIOwn release];
}
jscs
  • 63,694
  • 13
  • 151
  • 195
freelancer
  • 1,658
  • 1
  • 16
  • 37
  • 4
    You are entirely omitting the existence of ARC. – Till Apr 18 '12 at 16:38
  • 3
    @Till: I like that fact, since ARC allows noobs to make apps without knowing what they're doing - and spam the AppStore with low quality apps. People who know what they're doing don't need ARC. – Christian Schnorr Apr 18 '12 at 16:46
  • 8
    People that know what they are doing use ARC exactly because they (a) know what they are doing and (b) realize that ARC solves a series of problems that cannot be solved in MRR without exposing error prone, bugeriffic, locking patterns. The notion that ARC enables crap apps is just silly. – bbum Apr 18 '12 at 17:11
  • 5
    @Jenox just wow. ARC is a valuable tool that can be used to make Apps more bug free and written faster. Like all tools one benefits the most when one has read the instruction manual. The key to making a good app is not whether you use ARC or not but how much effort you put in to learn. Your comment is simply trolling. – jackslash Apr 18 '12 at 17:15
  • 1
    @Jenox: Good luck with your assembly – Daniel Apr 18 '12 at 17:17
  • 1
    I feel the same way about cars with automatic transmission. Since the auto-tranny was introduced, the quality of driving has decreased worldwide. We need to get these driving n00bs off the road so us stick masters can rule the road once again. – Mark Suman Apr 18 '12 at 18:36