Menu

Showing posts with label memory management. Show all posts
Showing posts with label memory management. Show all posts

Reference Count in Memory Management

What is Reference Count in Objective-C’s Memory Management

Memory Managment

  • This is Objective-C Memory technique which holds number of counts which is handling by Object. i.e one object is holding how many reference are count value.
  • When we are allocating the memory for any object then automatically reference count is incremented by 1. Ex. NSString *stringObject=[NSString alloc]init]; means you allocating the memory for stringObject. when we are releasing the memory then automatically reference count decrees by one (1).
  • Whenever one object reference count values is Zero(0) then it’s called object is free.
  • When we are using the “release” method then it will deallocate memory for the object and reference count also decrees by one (1).
  • When we are calling “retain” method then memory will deallocated for the object but reference count will not decrease.
  • When we are working with autoreleasepool object then always recommended to use “drain” method only because,”drain” method will call garbage collection system.
  • Garbage Collection is a automated process which will destroyed the application related memory when the application will terminated.
  • In place of creating object of type NSAutoreleasePool then always recommended to go for “@autoreleasepool” block.
    @ autoreleasepool
    {
    //code benefiting from a local autoreleasepool
    }
  • In place of holding the reference count manually and deallocating by using “release” or “retain” it is recommended to go for ARC(Auto Reference Counting).
  • According to autorelease method whenever reference count value is zero then automatically object will be destroyed.
  • If we require to make enable ARC, then go for select Project File->Choose build setting and go to “APPLE LLVM Compiler 4.2- language” and choose Objective-C Auto Reference Count(ARC) YES or NO.

For More Information Please Visit

Memory Management in Objective-C

Memory Management in Objective-C

  • In Objective-C we having two type of memory management i.e Static Memory Management & Dynamic Memory Management
    1)Static Memory Management
  • When we are creating the memory of the time of compilation then it is called compile time memory management or static memory management.
    Generally in Objective-C, doesn’t require to go for Static Memory allocation.
    2)Dynamic Memory Allocation
  • When we are creating the memory at the time of execution then it’s called dynamic memory allocation.
    In Objective-C NSAutoReleasePool class is responsible for dynamic memory allocation.
    In Objective-C dynamic memory allocation will handle by reference counting mechanism.

strong and weak in iOS

strong and weak.

  • this is a two memory management attributes for properties introduced by in ARC.
  • weak
  • Weak reference are not support in OSX v.10.6 and iOS4>Weak reference does not extend the lifetime of object it points to, and automatically becomes nil when there are no strong reference to the object.
  • Weak reference most commonly used in the connection with thing such as a notification.you will keep weak reference to an object and keep it sending it messages as long as it is referenced elsewhere,then you can have cleaned up automatically later.
  • It says to the compiler keep this member as long as possible.
  • On weak members we can’t call retain or release methods
  • When the weak member is de-allocated then automatically pointer will set to nil.
  • For More Information Please Visit
Related Posts Plugin for WordPress, Blogger...