When is memory actually freed in Objective-C? -
i'm trying understand memory management stuff in objective-c. if see memory usage listed activity monitor, looks memory not being freed (i mean column rsize). in "object allocations" looks fine. here simple code:
#import <foundation/foundation.h> int main (int argc, const char * argv[]) { nsautoreleasepool * pool = [[nsautoreleasepool alloc] init]; nsinteger i, k=10000; while (k>0) { nsmutablearray *array = [[nsmutablearray alloc]init]; (i=0;i<1000*k; i++) { nsstring *srtring = [[nsstring alloc] initwithstring:@"string...."]; [array addobject:srtring]; [srtring release]; srtring = nil; } [array release]; array = nil; k-=500; } [nsthread sleepfortimeinterval:5]; [pool release]; return 0; }
as retain , release it's cool, balanced. rsize decreases after quitting little program. possible "clean" memory somehow before quitting?
no point in trying scrub memory before quitting; you're quitting, sparse resources own returned system.
a common misconception free()
(the underlying mechanic release) returns memory system immediately; doesn't in implementations, rather adds internal free list; on memory kept around application use, liberated system if asked for. second time loop rolls around, application reuses free memory allocate rather requesting more system, making smoother everybody.
Comments
Post a Comment