Terry
Terry

Reputation: 337

Weird Memory Problem of Cocoa Application

I am writing a Cocoa program for Mac, and it includes a picture viewer. What the picture viewer's job is to download a picture from web and show it. It would do this every time I click a button who would give it a picture URL. So this would be frequent.

My design goal is, every time after I change to another picture or close it temporarily (it is not the main window), it will release the memory it takes. For example, before I open the picture viewer for a picture, my application takes 1M memory. After I open a picture, it takes about, say, 2M memory. And after I close it, it will decrease the memory usage to the original 1M.

The problem is, sometimes after I viewed a picture, especially a big one, the viewer won't give all the memory it took, for example, opening it cost about 20M memory, but after I close it, just 10 of the 20 released, the another 10 is still there and never be freed. It looks like memory leak, but I checked my code and analyzed it many times, I couldn't find any memory leak, and also, some other pictures won't behave like this.

this is weird, is this possibly certain Cocoa internal memory cache policy? Any suggestions would be appreciated. Thanks in advance!

Update: Today after I added a scrollView as a wrapper of the imageView, this weird memory problem seems almost fixed.

Thanks for your advice, edc1591, I will continue to figure out what happened.

Upvotes: 0

Views: 123

Answers (1)

edc1591
edc1591

Reputation: 10182

Without seeing the code it's hard to tell quite what the problem is. But until I see the code, I'll give you a few pointers for dealing with this:

  • Enable Garbage Collection. If you already have it enabled, try forcing a garbage collection after releasing the image file like this:

    [[NSGarbageCollector defaultCollector] collectIfNeeded];

  • Try heapshot analysis. Here's a good tutorial on that.

I'll update this answer once the code has been posted.

Upvotes: 1

Related Questions