Memory is not getting released

拟墨画扇 提交于 2020-01-06 18:03:56

问题


When I run the following program ... application is holding some 5MB of memory even after releasing all objects ... when I don't add an object to list everything works fine ... but I am not sure why application is holding memory even after releasing the list.

I have read this link "NSMutableArray big memory hog?" and even tried with custom mutable array by mikeash MACollections -- in all the cases ... the memory allocated in the loop never get released if I add the object into list.

Please advice me some suggestion to know what is going wrong in the code flow. I appreciate your valuable time and help.

@interface TestData : NSObject 
{
    NSString *strData;
}
@property (nonatomic,retain)NSString* strData;

@end

@implementation TestData

@synthesize strData;

- (void)dealloc
{
    self.strData = nil;
    [super dealloc];
}

@end

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSMutableArray *list = [[NSMutableArray alloc] init];
    TestData *_object = nil;
    NSString *val = nil;
    for (long i = 0; i < 100000; i++)
    {
        _object = [[TestData alloc] init];
        val = [[NSString alloc] initWithFormat:@"%lu",_object];
        _object.strData = val;
        [val release];

        [list addObject:_object];
        [_object release];
    }

    [list release];


    [pool release];

    return NSApplicationMain(argc, (const char **)argv);
}

Update: I used "vmmap" command in terminal ... I can see some thing like this ...

                                     VIRTUAL   RESIDENT      DIRTY ALLOCATION      BYTES MALLOC ZONE                             SIZE     SIZE       SIZE      COUNT  ALLOCATED  % FULL
===========                          =======  =========  =========  =========  =========  ======

DefaultMallocZone_0x100005000          37.0M      5868K      2692K     8548      1210K      3% 
DispatchContinuations_0x1000cc000      4096K   48K        48K          1         32      0% 

unnamed_zone_0x100037000  0K         0K         0K          0         0K

===========                          =======  =========  =========  =========  =========  ====== 
TOTAL                                  41.0M      5916K      2740K       8549      1210K      2%

so looks like though the object is released ... it is still app memory as unused space ... not sure what can be done in this case. I am not sure is it a fragmentation issue ... or behavior.

来源:https://stackoverflow.com/questions/13818809/memory-is-not-getting-released

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!