I’m rolling my own object cache. It’s basically a NSMutableSet.
Is there a way to find out (programmatically) how much memory the collection is currently using, so that then I can trigger some purging of less important objects?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Define “the collection”. The size of the structure you use to maintain the collection? The size of the object pointers? The size of the instance variables that the objects hold?
You could purge data as needed on a memory warning.
The Objective-C runtime method to find out how much memory an object’s instance variables uses is
class_getInstanceSize, although I don’t think this is the right approach. One object holding a single NSData with 5 MB of data will clock in lower than an object with two NSStrings.Since I don’t know why you’re rolling your own cache, here’s what I would do. I would define a method to work out a proportional cost to the items that I keep. (For an image, let’s say width * height.) I would then use
NSCachewhich supports supplying a cost with each object, keeping a maximum cost and purging automatically.