views:

94

answers:

1

Hello all.

I'm trying to understand memory management stuff in Objective-C. If I see the memory usage listed by Activity Monitor, it looks like memory is not being freed (I mean column rsize). But in "Object Allocations" everything looks fine. Here is my 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];

    for (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 for retain and release it's cool, everything is balanced. But rsize decreases only after quitting from this little program. Is it possible to "clean" memory somehow before quitting?

+2  A: 

No point in trying to scrub the memory before quitting; you're quitting, so any sparse resources you own are returned to the system.

A common misconception is that free() (the underlying mechanic for release) returns memory to the system immediately; it doesn't in most implementations, but rather adds it to an internal free list; on which the memory is kept around for your application to use, but liberated to the system if asked for. The second time your loop rolls around, your application reuses this free memory to allocate from rather than requesting more from the system, making everything smoother for everybody.

Williham Totland
Thanks for you answer, but if I don't need all this resources right now. Let's imagine that this code was something like a main algorithm, and after that a just wanna look through results without quitting my program?
zhyk
The free memory might be reclaimed at a later point by the system; if needed. Free memory is just that, free; it's just not reclaimed by the system until necessary.
Williham Totland
But this simple code could take all free memory, I mean for about 1GB or 2GB, and nothing is happening. OS goes slow even after [pool release], and memory isn't freed.
zhyk
The code only takes a few MB of memory, (78 MB on my system), but it does take *all* of a single processors capacity; that's probably why your system is crawling.
Williham Totland
Actually, further testing indicates that it peaks out at about 100MB (still not a lot, tho', at least not a lot alot.
Williham Totland
That's just an example to illustrate problem.Take k = 90000000 and you 'll see lots of used memory ;) I don't care about processors capacity, the only thing I care about is memory, which is not being freed :((((
zhyk
Except that it is. When I say peak out; I mean *peak out*. It sinks back to about 40 megs afterwards. And I have to ask, why in the name of allmighty bob are you allocating these kinds of memory anyway?
Williham Totland
Ok, right answer is "don't allocate too much memory, reuse objects". Am i right?
zhyk
@zhyk: Partially: Sometimes you *need* to allocate a lot of objects; if you are say, modeling the movements of hydrogen nuclei in the center of a medium-sized star. But that's what supercomputers are for. For a desktop computer; where RAM is quite limited, you want to be as memory efficient as possible. As an exercise your question is interesting, but it's not something you should *ever* do in a desktop application; if you are doing something like, say, getting *a lot* of database row; process one at a time. Big file? Read it line by line; don't keep more of it in memory than you need.
Williham Totland
@Williham Totland: thanks a lot! I don't read database row, trying to do some math. I think i create too much objects ;)
zhyk
Consider: https://developer.apple.com/mac/library/technotes/CachingPurgeableMemory/Introduction/Introduction.html
bbum