Question

I am building an application that uses the camera of the iOS device to build up a stream and send it as an MJPG stream over the network.

I am buffering the frames in an array (NSArray *imageBuffer) from where I send them through a socket-based connection to the receiver.

My problem is that after a while imageBuffer seems to be taking too much memory and the application keeps receiving memory warnings from the OS until it ultimately crashes after about 30 seconds.

Now, I tried to simply empty imageBuffer after it counted a number of frames that exceeded a certain threshold with

if([self.imageBuffer count] >= maxImageBuffer){
    [self.imageBuffer removeAllObjects];
}

I also tried to set imageBuffer = nil and reinitialized it directly afterwards, but this didn't solve the problem either...

I am guessing the formerly buffered images still have their own party somewhere in the device's memory and the memory that they were using isn't actually freed when they are removed from the array or when the array is set to nil.

So, my question would be how I can manually remove all these image objects from my memory if removeAllObjects and setting the buffer to nil doesn't work?

Was it helpful?

Solution

Let's try:

//replace NSString with your images.
NSMutableArray *arr = [[NSMutableArray alloc]init];
@autoreleasepool {
    for(int i =0 ; i < 10000;i++){
        // 100000 times, variable a is created and I don't know when it release. But you use it in @autoreleasepool, it will release immediately after escaping for loop
        NSString *a = [NSString stringWithFormat:@"%d",i];
        [arr addObject:a];

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top