Memory issue on iPhone?

I am using one UIImageView object, allocating memory once. But change the names of the images several times. The question is that

A) one UIImageView object, allocated once and never changing the name of the image. eg UIImageView * firstObj = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "blue.png"]];

B) another UIImageView object selected once and changing the image names multiple times

for example, UIImageView * secondObj = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "blue.png"]];

// changed the name of the image once,

secondObj.image = [UIImage imageNamed: @ "green.png"];

 and so on....

      

which objects use the maximum memory or use equal memory?

which is the best way to use secondObj with minimal memory usage?

please explain it briefly because I need to use the number of images in my project and I want to avoid the memory issue due to images.

0


a source to share


4 answers


Reformatting your code, second example:

UIImageView *secondObj = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue.png"]];
// changed image name once
secondObj.image = [UIImage imageNamed:@"green.png"];
// and so on....

      

This code is fine. When you assign an image to an instance of a UIImageView, it will increment the number of samples in the image by one. If the image is already adjusted, it will release it first.



Since [UIImage imageNamed: ...] will give you an object already marked for autorelease, you can continue to assign images as shown in the example without leaking memory. When the UIImageView releases an existing image, it will be collected by the Autorelease pool.

In terms of minimizing memory usage, the [UIImage imageNamed: ...] method stores the image in a small amount of the application's cache, which you don't directly control over. The cache has an upper limit, but you cannot clear it to reclaim memory, so using it will increase the amount of memory when new UIImages are received.

You may want to avoid this cache by using [UIImage imageWithData: ...] to load your images, as discussed in the Stackoverflow question [UIImage imageNamed ...] vs [UIImage imageWithData ...] .

+3


a source


I'm not a great Objective-C expert, but your use in B looks like it should work without leaking. Since no other objects (other than UIImageView) store UIImages, they must be released when they are replaced in the UIImageView and the garbage collector must do its job.



David

0


a source


A UIImage created using imageNamed: auto-implemented to make it disappear. To give an example:

UIImage *image1 = [UIImage imageNamed:@"blue.png"]; //image1 has retain count 1
[myImageView setImage:image1]; //image1 is retained by UIImageView so has retain count 2

//run loop ends, all auto released objects are released and image1 now has retain count 1

//Later in app

UIImage *image2 = [UIImage imageNamed:@"green.png"]; //image2 has retain count 1
[myImageView setImage:image2]; //image1 is released and now has retain count 0, so the memory is freed, image2 now has retain count 2 as it is retained 

      

0


a source


Since no other objects (other than UIImageView) store UIImages, they must be released when they are replaced in the UIImageView and the garbage collector must do its job.

Keep in mind that there is no garbage collector on the iPhone. You can read more about it here:
fooobar.com/questions/421079 / ...

(This should be a comment on Davids' answer, but I don't have enough reputation yet)

0


a source







All Articles