How to Manage memory for iPhone?
- As name itself suggest,it is a process of managing systems memory.
- A Process by which system allocates memory to other processes.
- Includes freeing the memory when any particular or more than one process is not in use OR some other critical process needs the memory to execute.
- Garbage collector is used to de-allocate the memory from the process. Garbage collector is implemented via programming language.
- Virtual memory system is also used where it swaps the program in and out of the RAM making effective utilization of the RAM.
Issue with Memory Management:
- Relocation: A Running program in the memory should be saved in different parts of memory and hence when it has to be swapped in\out it cannot take the same place in memory. That is why memory management in the OS should able to relocate the program in different location and switch the reference and address in code.
- Protection: Different programs should not be able to reference the memory location of another program without any permission. It stops malevolent code of one program to hamper process of another program.
- Local Organization: different parts of the program can be shared with other program so is to provide data and information. Segmentation is one way to do it.
- Sharing: usually the memory for a program is different from memory used in other program but sometimes it requires information (data) from other program. This is known as Sharing for inter-process communication.
How Memory Management works on iPhone?
In iPhone OS, every program use virtual memory method but it is restricted to the size of Physical memory available.Reason is that iPhone do not store changable memory in disk, It doesn’t swap the program back to memory when required. iPhone OS provides memory to a program when it is under execution and revokes all the memory when the execution is over.
Practical Memory Management
When a program is executed which uses many objects, there could be many objects which are not in use. For minimal use of memory these objects have to deleted from memory. While doing so we have to keep in mind that the object should not be in use. For this purpose we need a management via which we can mark up objects as still useful.
An object is still useful or not can be decided by these factors:
- An object has one or more parents.
- When an object has no parents, it is destroyed.
- To ensure that a particular object is not destroyed, we must become parent of that object.
- To destroy an object we should abandon the parent (ownership).
When an object is created it’s retain count is 1. When this retain count become 0, the object is destroyed.
To change the retain count of an object, there are many method which are used:
alloc: allocates memory to objects and return retain count as 1.
copy:it makes a copy of object and returnd with retain count 1.
retain: increases retain count by one.
release:decreses the retain count by 1, also takes the ownership of the object.
autorelease:decrease the reference count by 1.
name = [[NSString alloc] initwithstrin:@”jitendra”];
NSString * name;
Name = [[NSString stringWithFormat:@”jitendra”];
name = [myWindowtitle];