swift:ARC: how to release all references to an UIImage

I have a ios/swift project where I place a lot of UIImageViews in an scrollview. I have multiple scrollviews and I want to make sure all memory is released when I close one. So far it seems like if I add an UIImage, it gets 16 references with the following code. If I run it on any device it runs out of memory and crashes.

Only 16 references? This code creates 9,999 scroll views (each time, leaving the previous one in the view hierarchy, but abandoning your reference to it upon the next iteration), each with their own copy of 1.jpg, and each added as a subview of view. In terms of how to release the memory, you should remove any strong references you have, as well as remove any other references, such as the view hierarchy.

So, if you want to remove the references to the images, you can either reset the image property of the UIImageView, and/or remove the UIImageView (or the UIScrollView) from the view hierarchy.

Note, your attempts to set backgroundView and scrollView to nil remove your strong reference to those, but called addSubview earlier, so the view hierarchy still has a strong reference to them. If you want to get rid of everything, before setting scrollView to nil, make sure to call scrollView.removeFromSuperview() in order to remove it from the view hierarchy.

Email codedump link for swift:ARC: how to release all references to an UIImage