Memory management

This is a broad topic. Needs to keep updating.

Cocoa/Cocoa Touch can be break up into levels

  • Cocoa /Touch: Objective-C runtime + Foundation Framework + AppKit(/UIKit) Framework
  • Objective-C runtime: library that enables the language feature itself, i.e. dynamic message passing on Obj-C
  • AppKit/UIKit: UI framework
  • Foundation Framework: NS classes, facilities, API to underlying system
  • CoreFoundation Framework: procedural interface to C, CF classes and struct
  • ANSI C: C language

Note: Cocoa /Touch primarily developed using Obj-C, but can include C++ and ANSI C code.  Also able to use scripting language that bridged to Obj-C runtime, such as PyObjC and RubyCocoa.

Memory Management for C:

void *malloc(size_t size)
allocate size in bytes and return pointer of the allocated but not initialized memory.  If size is 0, return either NULL, or pointer can passed to free()

void free(void *ptr)
free memory pointed to by ptr, which must be a previously allocated memory address.  Passing in Null will do nothing.

void *calloc(size_t nmemb, size_t size)
allocate array(length is nmemb) or size in bytes.  With memory initialized with zeros.

void *realloc(void *ptr, size_t size)
change size of memory block pointed to by ptr to size bytes.  Not gonna change contents/ initialize.  If area pointed to was moved, free(ptr) is done.  Return pointer may be different from ptr.  If it fails, original block is left untouched.

int posix_memalign(void **memptr, size_t alignment, size_t size)
similar to malloc, but the starting address is guaranteed to be multiple of “alignment”, which is a power of two multiple of sizeof(void *)

Note:
size_t : unsigned data type of at least 16 bit, good for holding index, bad for arithmetic.
Stack vs Heap: Stack is memory stores all variables currently used by current function, very fast and managed by CPU, freed once function exit. Accessed locally.  Limited size, depends on OS.
Heap is memory stores global memory using pointer. user managed by malloc etc.  Slow but large in size.
Subnote: to keep the stack variable after function exit, use ‘static’ to declaring the variable.

Memory Management for FFmpeg:
av_malloc() and av_free() just redirects to standard malloc() / posix_memalign() / free() functions.
The main difference is that them ensure alignment for some instructions like SSE / MMX and so on available on your CPU and used in FFmpeg decoders/encoders.  So it’s recommended to use these for meta info packets.

Memory Management for CoreFoundation:
Use Allocator to manage object with a retain counter.
CFRetain() : increase retain counter
CFRelease() : reduce retain counter

Allocator: In charge of allocate and deallocate for CoreFoundation Objects.
manage the memory allocate, reallocate, deallocate, and also retain and release counter

Opaque Type: a type that don’t have full definition for the struct or class.
In C, C++, Obj-C, can tell a type will be defined later using a forward declaration.

Ownership: in general any C functions under CoreFoundation contains the word “Create” or “Copy”, you own the object.  Means you need to release it once done using it.

Copy (‘=’):
Copy for CF object would copy the reference, not the actual data, unless using “CreateCopy” function.
If copy a compound object (i.e. collection objects), this copy will copy only container object while leave reference for actual child objects. This is called Shallow Copy.
If want to also copy actual child objects, then need to use “CreateDeepCopy” to make a deep copy of the compound object.

Reference:

https://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/CFMemoryMgmt.html#//apple_ref/doc/uid/10000127-SW1
http://stackoverflow.com/questions/3900549/what-is-runtime
http://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work
http://ffmpeg-users.933282.n4.nabble.com/Is-av-malloc-and-av-free-thread-safe-td3830693.html

http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html
Deep reading:
http://man7.org/linux/man-pages/man3/malloc.3.html
http://www.cocoawithlove.com/2010/05/look-at-how-malloc-works-on-mac.html

Leave a comment