Sunday 8 November 2015

Interview Queations 6 Memory Management

1) What is the memory management?
        It allocates and reallocates memory and find out memory leaks.
2) What the difference is between retain and release?
        Retain: Increment index by 1
        Release: decrement index by 1 

     The default auto-release pool is drained every time through the run loop, not once per application lifecycle. So as soon as your current stack of method calls completes, and you're back waiting for a UI event, the pool is drained.


3) Can you explain what happens when you call autorelease on an object?

When you send an object a autorelease message, its retain count is decremented by 1 at some stage in the future. The object is added to an autorelease pool on the current
thread. The main thread loop creates an autorelease pool at the beginning of the function, and release it at the end. This establishes a pool for the lifetime of the task. However, this also means that any autoreleased objects created during the lifetime of the task are not disposed of until the task completes. This may lead to the task
ʼs memory footprint increasing unnecessarily. You can also consider creating pools with a narrower scope or use NSOperationQueue with itʼs own autorelease pool. (Also important – You only release or autorelease objects you own.)  



4) Explain the correct way to manage Outlets memory

Create them as properties in the header that are retained. In the viewDidUnload set the outlets to nil(i.e self.outlet = nil). Finally in dealloc make sure to release the outlet.

5)Can you just explain about memory management in iOS?

Memory management is the programming discipline of managing the life cycles of objects and freeing them when they are no longer needed. Managing object memory is a matter of performance; if an application doesn’t free unneeded objects, its memory footprint grows and performance suffers. Memory management in a Cocoa application that doesn’t use garbage collection is based on a reference counting model. When you create or copy an object, its retain count is 1. Thereafter other objects may express an ownership interest in your object, which increments its retain count. The owners of an object may also relinquish their ownership interest in it, which decrements the retain count. When the retain count becomes zero, the object is deallocated (destroyed).

To assist you in memory management, Objective-C gives you methods and mechanisms that you must use in conformance with a set of rules.



6) What is Automatic Reference Counting (ARC)?

Is a compiler-level feature that simplifies the process of managing the lifetimes of Objective-C objects. Instead of you having to remember when to retain or release an object, ARC evaluates the lifetime requirements of your objects and automatically inserts the appropriate method calls at compile time.


7) Why dynamic memory allocation is required?
            Dynamic Memory allocation - memory is allocated during run-time in heap. This is used when the amount(size) of memory is variable and is known only during run-time. Dynamic allocation is achieved using certain functions like malloc(), calloc(), realloc(), free in C and "new", "delete" in C++.
Static Memory Allocation - memory allocated at compile time in stack or other data segments. This is used when the amount(size) of memory is static/constant and is known during compile-time.

8) Given an object of an unknown type, how would you determine its class? How would you
Check to see if it conforms to a given protocol? How would you check to see if it responds to
a given message/selector?
               [theObject respondsToSelector:@selector(theSelector)]
The most common pattern for this problem in Cocoa/Cocoa Touch is to define a protocol. A protocol is a collection of methods that can be implemented by any object.If you make ClassA and ClassB conform to a protocol containing the methods you need them to respond to then you don't need to worry about which type of object you get. The idea is "if it looks like a duck and quacks like a duck, then it's probably a duck".
You can use dynamic typing and create your objects depending on the outcome of your query, but ensure that the resulting object conforms to a particular protocol, like so:
id <MyProtocol> myObject;



if (YES)

    myObject = [[ClassA alloc] init];

else

    myObject = [[ClassB alloc] init];



[myObject myMethod];

[myObject release];


No comments:

Post a Comment