views:

240

answers:

1

I have built an image cache which is an ArrayCollection containing images. I have built functionality so that the cache can hold a max of 250 images. However, the images can be of a different size, so it's better to limit the total memory size of all images in the ArrayCollection.

Does anyone know how I can get the total memory size of an ArrayCollection without looping through all the items in the ArrayCollection?

+1  A: 

I'm not sure you can. My advice is to create a new collection class based on ArrayCollection (say, ImageCacheCollection) that keeps track of the collection's current memory size and its memory limit. Before you add an image to the collection you first verify that the memory limit won't be exceeded. If it isn't, you add the image to the collection, in the process adding the image's file size to the collection's current memory size.

Stiggler
With this kind of solution, I'm always a bit afraid that something might go wrong with counting and the counted cache size isn't accurate. But I implemented it anyway as there doesn't seem to be a better way, and I haven't had any problems yet. Thanks.
Maurits de Boer