I'm using the AssetManager class in Starling to load a lot of textures, around 600-700 png-files. All files are not loaded on start and I use multiple AssetManagers to hold the images. On start it loads around 70 images to 3 different managers and while it's loading I get some memory warnings on iOS.
iPhone appName[644] : Received memory warning.
iPhone appName[644] : Application received a memory warning from the system.
iPhone SpringBoard[74] : Received memory warning.
iPhone MobilePhone[199] : Received memory warning.
I've tried to load them at the same time and tried start each load when the other one is completed. I've also tried System.gc(); and System.pauseForGCIfCollectionImminent(0.1); between each load, this fixed the problem on iPhone 5 (7.0.4) but not on an iPhone 3gs (6.1.3).
Starling version: 1.4.1
Anyone got any tips on how to solve this?
Typically from a mobile development standpoint there is never a "one size fits all" solution. There are many different screen sizes and hardware configurations. In order to combat this many developers will generate -as mentioned previously, Sprite Sheets. Using this method the application only has to load one image vs -in your case hundreds. Image loaders take quite a while connecting to a memory address to initiate a texture upload to the GPU -I can't even image how slow it must be for that many assets.
This texture packer is phenomenal, and also aides with another feature many dev's incorporate. Asset Densities. Wherein the iPhone 3Gs a dev will load a set of sprite sheets developed for smaller screen sizes, a larger sprite sheet with twice the sized assets will load for a tablet or some of the higher end devices. This method will not only solve your memory issues but make your application look much better over a broader range of devices.
Here is a link to the Starling Wiki on multi-resolution development.
Of all the changes you can make to a project "for the better" the ones mentioned above are very high on the list.
You should NOT be loading 600-700 images. Even if they load successfully, you'll have a lot of problems with the performance. Instead, use atlas/sprite sheets.
http://wiki.starling-framework.org/manual/textures_and_images
And for even more optimized work, use ATF Textures instead png.
http://wiki.starling-framework.org/manual/atf_textures
If you run XCode Instruments against your app you'll see the total load of the device, which is the real metric that determines how often you receive memory warnings.
If your total load % is above 80%, expect a slew of constant memory warnings. The GC will try and run every time causing drastic clipping in framerate.
There's a bug about this and Adobe is working on it, but the main solution is to simply use less memory. Downscale textures for devices that have lower resolutions, etc...
https://bugbase.adobe.com/index.cfm?event=bug&id=3649713
Related
I know this is a stupid problem, but this is my first real app that I have to make, I have no one to ask and I looked up this problem and found no other similar problems.
My app crashes on real devices with no exception. I saw in the simulator that uses too much RAM and after a while I got to the conclusion that the pictures I am using are to blame.
The app is structured in this way: it has 8 viewControllers for different things: for example, it starts with one which lets the user select the avatar with which he/she will play and here I have two pictures, next is a viewController which shows the stats for that avatar and here it is another picture and so on. The problem is that each picture uses 40MB of RAM to be displayed and things add up so the app uses more than 300MB of RAM when the user gets to the gameviewCOntroller where the game is. Because of this, on devices like iPAD 2 or iphone 4 it crashes, but not on iphone 5.
I tried to set the images both from "images.xcassets" and from a ".atlas" folder, but the result is exactly the same. The pictures have a dimension of no more than 1500x1999px, they are in png format.
Also, I saw that if the app were to start directly into the gaveViewController it would use 180MB so the other viewController remain in memory or something like that. Should I "clear" them or something similar?
//-------update-------
This is what I got from Instruments:
Memory is a big deal on mobile devices, there is not a clear answer to you question, but I can give you some advices:
If your images are plain colors or have symmetric axes use resizable images. You can just use one line of pixel multiplied by with or height to cover the entire screen using a small amount of memory
Image compression doens't have effects when the image is decompressed. So if you have a png that is 600kb and you are thinking that converting in a 300kb will lower memory usage is only true for "disk space" when an image is decompressed in memory the size is widthXheightXNumber_of_channelXbit_for_channel
resize images: if are loading a 2000px square image into memory and you show it inside an image view of 800 px square, resize before adding it.You will have just a peak while resizing, but later it will use less memory
If you need to use big images, use tiling techniques such as CATiledLayer
If you don't need an image anymore get rid of it. It's ok to have an array of path to images, but not an array of full uncompressed images
Avoid -imageNamed it caches images and even if Apple says that this cache is released under memory pressure, you don't have a lot of control on it and it could be too late to avoid a crash
Those are general advices, it's up to you if they fit your requirements.
You should definitely follow Andrea's advices.
Additionally you should consider setting the image size to exactly what your need is. You're saying that you've tried to set them from xcassets so you have full control over the images you're loading, which is great (compared to downloading an image that you cannot modify).
I highly suggest you read some documentation on using Asset catalog files. This will allow you to have high-resolution image for bigger screens that also have more memory, and smaller ones for older devices, which is what you want here.
Also, note that 1500x1999px is still a very big size for most mobile devices.
More links about screen-size:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html
http://www.paintcodeapp.com/news/iphone-6-screens-demystified
I'm developing a game with lots of graphics for iOS platform. None of the graphics are dynamic, they're all ready made images. There are 6 layers on the screen and each layer contains 3-4 graphics objects in average that constantly scroll to left, each with different speeds. So with each screen refresh about 20-25 objects are scrolled, removed from screen and added again from right. The game is universal so images of all sizes present in the resources folder.On iPads, iPhone 5, iPhone 5s it goes smooth. But on 4th Gen iPod touch I notice some hiccup.
When I test the app with Instruments, I notice a critic memory problem at start and then the problem goes away. So this is all because I load all the above graphics on application start. All the images are combined into 4 different sprite sheets. So here's my question:
Is it the weight in kilobytes that matters or is it the dimensions? I'm asking this because I reduced the size of the images by about 70-80 percents, though their height and width remain the same, but that memory problem still exists.
If you are reducing the size of the images by utilizing compression, then expect there to be delay and a memory hit as the images are uncompressed when first loaded, and they will take up as much memory as they did before compression.
The 4th generation iPod Touch probably is having some memory pressures and it will try to release memory elsewhere in order to allow for your images to load up and stay in memory. That may be the hiccup you see, as other apps, like Mobile Safari or Mail are asked to give up some memory or to terminate.
(The size the images take in the resources folder is inconsequential. It is the size they take when they are uncompressed and in memory that matters).
You could make the images smaller by making them 4bit instead of 8bit color, or you could make them monochrome. You can also use a non-retina size or smaller, and then let the OS stretch them out to fit the space required. I would try to use the best images first, and if you get a memory warning (didReceiveMemoryWarning), then reload with the images that are less dense.
you can not load all image before the game beginning, you should use the cache to control the size of the memory. And before the scenery of game change ,the release the cache ,reload the need ones.
So you must control your resource dependents in the every game scenery.
This is the common method to management the large resource application.
I'm creating an iOS app with some preloaded data which includes images, it will have between 150 and 200 images. This images are not downloaded from a server, since the app won't connect to the internet.
I'm wondering about if saving the images locally can cause any problem, what is the max size that I can use to store them? Can this affect the app's performance? Anything else that I should be aware of?
Thanks in advance!
Over the air cellular maximum download is now 100MB since the iOS 7 launch.
Your maximum app size is 2GB with a maximum binary size of 60MB. With 200 images I don't think you'll hit the 2GB mark, so long as you use the proper compression techniques.
The only thing that would affect your app's performance is the way the images are presented. If they're really large images, 10+ MB you'll most likely hit memory problems in older devices and if you go larger, memory problems in newer devices. There's plenty of techniques like tiling in a scroll view to get around these problems but it requires you splice up your images and create multiple resolution versions of the image.
If your app is greater than 50MB (at the time of writing) because of the images being included then users will need WiFi to install it.
Other than that, your performance concerns should be around how many of the images you will have loaded into memory at any one time and how you can reduce that number.
My app crashes with low memory warning on device, even though the max live bytes in instruments is 3 MB tops. I do use a lot heavy PNG's in my app(in about 20 ViewControllers) , I believe ARC should've taken care of it.
Here is the screenshot.
Reduce size of heavy png files. Png file must be good in resolution but size must be less not more than 2 MB.I also had the same problem but used Three 20 Framework which solved my issue because it manages Images files allocation perfectly. Now a days it seem Three 20 is not getting updated but still you can try it in a sample app if you want . Here is the link :http://three20.info/
I figured it out myself. The problem was that I was doing animation using a bunch of PNG's.
So when using this
image.animationImages=imageFrames;
it was caching all the images in memory each time it was called, which led to dirty memory filling up and crashing the application. So, after using it each time to release the image cache we need to do this
image.animationImages=nil;
I am developing an iphone application and i am using a background picture for my app. I know that before iphone 4 , the resolution of the screen was 480x320 and after 960x640. I read that i should use 2 images like : MyImage.png and MyImage#2x.png with the 2 different resolutions and the app will know which one to choose according to the phone. My question is very simple. If i just use the 960x480 , whats the difference? I am developing on an iphone 3gs and i am using this resolution and everything works perfect. So why have a small resolution for these phones?
The difference, in my opinion, is the following: the phone is going to resize the doubled-resolution to half the size, and this will lead to (1) the final image is not as smooth as if you would have exported it from a image application like Illustrator or Photoshop - at least from my own experience, when I export both resolutions, in non retina devices, the images don't look as good if I had exported the normal resolution. Also (2) the device will waste time/performance to make this conversion, although I don't know how much it is significant. And (3) finally, you may overload memory with no need, considering that the doubled sized images are bigger files, and you could use smaller files instead.
If you don't mind about this issues, you can go ahead. Try yourself.
If nothing else, the high-resolution images will tax the memory much more. One full-screen image at 320✕480 is around 600K in memory, whereas the 640✕960 is almost 2.5M, four times as much. On the older devices the memory is much more scarce than on the new ones and you might run into problems later in the development cycle, when there are more resources in play.
Even if you are able to test the app on all older supported devices and you can guarantee that there are no immediate performance problems or visual glitches, the app will take more memory, forcing the OS to kill other apps more frequently (= worse overall user experience).
if you use one version of image, (960x640) , there will be a memory overhead when the app run on non-retina. you dont need to display the high res in non-retina, only in retina display, making two versions of images .png and #2x.png will tell ios which image version it should load, depends on what device is currently in use.