简体   繁体   中英

Mac OSX cocoa app takes huge amount of ram with garbage collection

im writing a video recorder application for mac with cocoa, and have a huge problem with memory usage.

When launching the app, i have a window with two buttons that i can change view with and a simple custom view where the webcam get loaded. Just this view takes 21mb of ram, when i press on one of the buttons i change the custom view to a QTMovie of the content recorded from the cam.

Every time i change view my app fills upp with about 10mb of ram, and never stops. I have garbage collection on. Even if I show the view of the QTMovie and wants to press the button to display it again, its takes more ram.

Code that loads the subview, trigged from the button

 if ([myCurrentViewController view] != nil)
    [[myCurrentViewController view] removeFromSuperview];



        NSViewController* cameraViewController2 = [[NSViewController alloc] initWithNibName:@"kVideo" bundle:nil];
        if (cameraViewController2 != nil)
        {
            myCurrentViewController = cameraViewController2;    
        }


[myTargetView addSubview: [myCurrentViewController view]];

And this is what happens when the the videoView loads, and this takes 10mb of ram every time i want it to display.

-(void)playMovie
{

[movieView setMovie:nil];

NSString* moviePath = [NSBundle pathForResource:@"tempFile" ofType:@"mov" inDirectory:@"/Users/Shared/"];
QTMovie* movie = [[QTMovie alloc] initWithFile:moviePath error:nil];


[movieView setMovie:movie]; 

}

- (void)awakeFromNib
{    

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent3:) name:@"updateVideoPlayer" object:nil];

[self playMovie];
}

I would run your app through Instruments with the Object Graph and Garbage Collection Instruments on. From what your describing it sounds like you have a root object reference to something like an array and you are allocating objects and adding them to this reference but never removing them. The only way the garbage collector knows how to collect something is if all root objects (and any objects referenced through root objects) no longer have a reference to an object.

I'd bet that you still have references to these objects somehow, and you just need to use the object graph to see how you still have a reference to it.

Okay i solved it, every time i pressed a button to change a view, i allocated the new view with something like this SViewController* cameraViewController2 = [[NSViewController alloc] initWithNibName:@"kVideo" bundle:nil]; , and they never got released or thrown away or whatever they should do. Now instead i allocate my views in one place, and just refer to the allocated views when i press new buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM