开发者

Core Data is using a lot of memory

开发者 https://www.devze.com 2022-12-17 18:14 出处:网络
I have a data model which is sort of like this simplified drawing: alt text http://dl.dropbox.com/u/545670/thedatamodel.png

I have a data model which is sort of like this simplified drawing:

alt text http://dl.dropbox.com/u/545670/thedatamodel.png

It's a little weird, but the idea is that the app manages multiple accounts/identities a person may have into a single messaging system. Each account is associated with one user on the system, and each message could potentially be seen/sent-to multiple accounts (but they have a globally unique ID hence the messageID property which is used on import开发者_如何学C to fetch message objects that may have already been downloaded and imported by a prior session).

The app is used from a per-account point of view - what I mean is that you choose which account you want to use, then you see the messages and stuff from that account's point of view in your window. So I have the messages attached to the account so that I can easily get the messages that should be shown using a fetch like this:

    fetch.fetchPredicate = [NSPredicate predicateWithFormat:@"%@ IN accounts", theAccount];
    fetch.sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]];
    fetch.fetchLimit = 20;

This seems like the right way to set this up in that the messages are shared between accounts and if a message is marked as read by one, I want it seen as being read by the other and so on.

Anyway, after all this setup, the big problem is that memory usage seems to get a little crazy. When I setup a test case where it's importing hundreds of messages into the system, and periodically re-fetching (using the fetch mentioned above) and showing them in a list (only the last 20 are referenced by the list), memory just gets crazy. 60MB.. 70MB... 100MB.. etc.

I tracked it down to the many-to-many relation between Account and Message. Even with garbage collection on, the managed objects are still being referenced strongly by the account's messages relationship property. I know this because I put a log in the finalize of my Message instance and never see it - but if I periodically reset the context or do refreshObject:mergeChanges: on the account object, I see the finalize messages and memory usage stays pretty consistent (although still growing somewhat, but considering I'm importing stuff, that's to be expected). The problem is that I can't really reset the context or the account object all the time because that really messes up observers that are observing other attributes of the account object!

I might just be modeling this wrong or thinking about it wrong, but I keep reading over and over that it's important to think of Core Data as an object graph and not a database. I think I've done that here, but it seems to be causing trouble. What should I do?


Use the Object Graph instrument. It'll tell you all of the ownerships keeping an object alive.


Have you read the section of the docs on this topic?

0

精彩评论

暂无评论...
验证码 换一张
取 消