iPad iOS memory management - how to free up "Real Memory" used by UIImageViews, UIScrollViews? - objective-c

I'm having memory issues with one of my apps and I've identified "Real memory" as defined in Instruments> Activity monitor as a possible culprit.
My app allocates large UIImages within UIScrollViews. There's a CIImageFilter applied to one of the images. Activity monitor shows that upon the first pushing of the view controller containing scrollviews with large images, the real memory use jumps to around 300mb. Subsequent pushes/pops raise it to about 500mb:
I read that "Live Bytes" does not count memory used by textures and CALayers, so my question is: How do I properly release memory that is used by CALayers of my Image/Scrollviews?
See the real memory usage blue pie chart on the right:
Both real and virtual memory are the highest for this process:
What bothers me is that I'm trying to clean up my large scrollviews and images when popping that controller, and the numbers for the "Live bytes" go down to about 5mb, while "Real Memory" stays outrageously high(~500 mb):
ContainerScrollView* container = ...;
[container.view removeFromSuperview];
container.view = nil;
Here's the allocations profiling:

I found a person experiencing a similar issue here:
Mysterious CoreImage memory leak using ARC
The answer (I really hope it is) appears to start using NSData dataWithContentsOfFile: and then create a UIImage imageWithData:. Got an image a user picked? Write it to a temporary file and read it back. I do not trust any other image methods, as they, in my 12 hours of testing appear to behave irrationally in iOS 6.1.2 for large image views.

Related

Major iOS9 performance issues with Sprite Kit

I am experiencing HUGE performance issues with iOS9 and I just can't figure out what to do. I've read many posts - here, and here for example, but their suggested solutions don't help or make little difference.
My game has gone from 60fps on an old iPad 2 (iOS 8.4) to < 15fps on a new iPad mini (iOS 9).
I'm trying to work out the main culprit. I'm pretty certain one of them is SKCropNodes. I usually render several SKCropNodes in my scene (6 - 18). This was never an issue in iOS8, but it appears iOS9, while it does a better job of cropping, also eats up performance doing so.
If I render the crop nodes as normal SKSpriteNodes, I gain maybe 5fps on old devices, and up to 30 on a newer iPhone 6. I have no alternative to using crop nodes, but it can't be the whole problem.
I thought perhaps the wrong texture atlas was being used - i.e. one of a much larger resolution. However forcing my device to use a very small atlas made no difference.
I'm using Texture Packer to generate my atlases with scaling variants for the different devices. I've noticed XCAssets now features an option to add a Sprite Atlas (I can't seem to find any documentation about this). This isn't suitable to my game, since I use 100s of sprites. I've tried adding my atlases to XCAssets, but then for some reason it won't use the scaling variants. Nevertheless, with low res textures, it still runs terribly.
I have tried setting
skView.ignoresSiblingOrder = YES;
and given all my nodes zPosition values, but still no effect. I have also added the .png extension to every image name (originally a problem that meant they wouldn't render.)
I have some SKEffectNodes in my scenes, but removing and adding these doesn't seem to have an effect.
I don't understand how the same hardware and the same code can produce such drastically different results. Obviously Apple has changed something to do with rendering that has had an adverse effect. They also seem to have no intention of fixing these issues. I am aware of bugs on the issue that have been there for months - long before iOS9 was released.
I've been working on this game for 2 years now, and only just released it before iOS9. It's now suffering from terrible performance, and regular crashes.
Has anyone figured out what exactly Apple did to kill performance? If I knew that, I could at least try to work around it... Thanks.
UPDATE
Below are some figures for the same scene, with the absolute maximum number of nodes the game would generate at one time.
iOS 8, iPad 2, ~200 nodes, ~100 draws, 58.7 - 60 fps
iOS 9, iPhone6, ~280 nodes, ~216 draws, around 20 fps
I assume the difference in number of nodes is due to the different screen sizes. If I change the scene on iPhone 6 to achieve equivalent values, the FPS is still around 24.
UPDATE 2
Using Xcode's template Sprite Kit project, and changing the spaceship to an SKCropNode containing the spaceship, on iOS 8 I am able to add 100s of ships with no frame rate issue. On iOS 9, same project, I can add about 25 before the frame rate drops to < 30.
iOS 8 on iPad2:
iOS 9 iPhone 5:
In terms of texture atlas use, as in my comment, I can't guarantee that anything will be drawn from the same atlas. My game contains customised characters, with assets from a range of atlases (each of which contains ~100 textures). There can be up to 9 characters on screen at a time. I understand this isn't the most efficient in terms of draws, but I've never had a problem until iOS9...
Update 3
I've submitted a bug to Apple, including my sample program. I also used up one of my tech support requests. So far, nothing from Apple.
There are two main issues.
One is a radical drop in performance of Sprite Kit from iOS 8 to iOS 9 for all manner of reasons, some of which you've linked to, but there are others. It seems many aspects of rendering, sorting and storing/dealing with nodes is either broken or doubling or tripling their previous loads on CPU/GPU.
However there's another issue that further compounds efforts to resolve any of the possible performance problems. It's a pervasive and seemingly arbitrary frame rate capping mechanism that's most frequently noticeable when it operates at 40fps. But it also operates at other frequencies.
For many years this capping has been (rarely) noticed when folks manually use CADisplayLink to create game loops or other timing based mechanisms on a frame per frame basis.
With iOS 9 this seemingly automated capping has become a terribly unwanted "feature" of Sprite Kit, SceneKit, Metal and OpenGL ES based applications.
In the case of SceneKit this is most telling because the capping occurs regardless of rendering choice - Metal or OpenGL - and seemingly on all devices, including things like the new 6S phones and the iPad Air 2, even with very simple, default template projects.
"Renderer" is a line item in the detailed statistics of SceneKit, onscreen, on the device. It's the most telling indication of a capping process. It's not there when a game runs stably at 60fps.
When capped at 40fps, no matter the amount of time required to do other activities onscreen and in logic, this component will absorb all remaining time in the game loop needed to maintain a solid 40fps capping. It varies according to the time required for other actives, always forcing an apparent goal of the underlying OS to hold the frame-rate at 40fps.
This problem in conjunction with the issues regarding iOS 9 Sprite Kit performance mean that it may not currently be possible to resolve all your issues. It will be extremely difficult to ascertain when you're hitting one of these (seemingly) arbitrary fps caps versus having caused an actual problem.
Just as an aside, these caps are not limited to 40fps. I've noticed them at 30fps, 24fps, 20fps, 15fps, 12fps and 8fps.
Of course Apple has never conceded nor admitted to this capping mechanism within the OS, nor commented on when/how/why it's so heavily impacting game and rendering processes.
My theory, as expressed in this post ( Inconsistent SceneKit framerate ), is that it's part of iOS designed to facilitate the use of variable frame rate technology soon to come in the iPad Pro, and possibly in other devices.
It would make sense that 120Hz become a base rate for future devices, particularly given the focus on performance advantages of iOS, the new Apple TV and 240Hz sampling of the screen touches/pen within the iPad Pro... and the considerable number of 120Hz televisions in the market.
Even without variable frame rate technology (say... your TV), 120Hz display rates means 24fps movies can be played back at a stable 5:5:5 pattern of frame display -- this massively increases the joy/immersion when watching films, just about all of which are shot at and truly exploit the advantages of a true 24fps for blur and motion effects.
120Hz with either variable frame rate technology or 5:5:5 frame display will also save Apple enormous effort in terms of compression and decompression of films when compared to the pulldown methods currently used on all devices with a maximum frame rate of 60fps.
All speculation, but I'd guess the use of these frame rate cappings in game engine technologies are there to help make games use less power, too, and give (in the future) developers an option to framerate lock their games in a variable frame rate device world. It's very unfortunate that (if this is the case) they have done such a poor job or sorting out the capping issues in the OS and the nature of Sprite Kit, leading to a scenario where you're fighting blindly to get good, high, consistent frame rates.
Apple silence and seemingly uncaring attitude towards the problems these two sets of issues are causing is (quite possibly) a very strong indication of how they feel about "their game development community".
And it's the greatest single problem of dealing with the kinds of cutting edge and performance critical development problems inherent to game making within a closed source framework from a needlessly secretive and uncommunicative (almost belligerent) organisation.
It seems the majority of iOS9 issues are addressed in the latest iOS9.2 beta & Xcode 7.2 beta.
Thank you Apple for finally addressing the problem. Too bad I spent 2 months working on work-arounds.
I've been posting on Apple's forums, submitting bugs and communicating with a support technician. It's a shame that at no point did Apple offer any clarity over what issues they were aware of, and what the Sprite Kit team were addressing.
Still, at least it seems the majority of Sprite Kit issues are now resolved, and a career change is no longer necessary :]
In the example you provided I can see one way to improve the draw count and to validate that the crop node isn't the problem. I don't know how practical this is in your actual game, but this drastically limits your draw calls.
#import "GameScene.h"
#interface GameScene ()
#property(nonatomic, strong)SKTexture *spaceshipTexture;
#end
#implementation GameScene
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:#"Chalkduster"];
myLabel.text = #"Hello, World!";
myLabel.fontSize = 45;
myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[self addChild:myLabel];
//Create one texture to be used over an over
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:#"Spaceship"];
sprite.xScale = 0.5;
sprite.yScale = 0.5;
SKCropNode *cropNode = [[SKCropNode alloc] init];
SKSpriteNode *mask = [SKSpriteNode spriteNodeWithColor:[UIColor blackColor] size:CGSizeMake(100, 100)];
mask.position = sprite.position;
[cropNode setMaskNode:mask];
[cropNode addChild:sprite];
[self addChild:cropNode];
//temp add to scene to create the texture than remove
//keep pointer to texture so it can be reused
self.spaceshipTexture = [self.view textureFromNode:cropNode];
[cropNode removeFromParent];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
//re use texture
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:self.spaceshipTexture];
sprite.position = location;
[self addChild:sprite];
}
}
#end
The downside is that the issue remains when you get up to 120+ ish nodes on screen.
Also note I created a fresh SpriteKit app using the default template and removed the SKAction. Unfortunately even the default Apple template suffers huge fps drops when you get up to 120+ nodes. So I don't know if there will be an answer and your best course of action is to file a bug report because I believe it should (and use to be able to) handle that many on screen without a drop in fps.
Also something worth noting this error comes up on the default project which could be related...
2015-10-20 10:46:50.640 Test[2218:658384] <CAMetalLayer: 0x15fe987c0>: calling -display has no effect.
Update
Doing a little more research I ran into this...
https://forums.developer.apple.com/thread/14487
Looks like others are filing bugs for this...
Lots of draws... Have you tried this?
skView.ignoresSiblingOrder = YES;
Are you using SKLightNode? If so try removing all light node's from your code. I did this and my game is running back at 60fps like it did in iOS8.
Source:SKLightNode performance issues

Crashing Ipad app, memory issues

I'm very new to objective C, that's actually my first app...
I'm working on an application which has a list of projects, each project has its own gallery of images, segues וarc. the gallery takes about 90% of the screen, and a row of thumbnail takes the rest.
It is running OK on the simulator, but when I get out of one gallery to another (somtimes after three or four passages) the application crashes (on real device - iPad2 with IOS 6).
There is no exception or error, the log is clean. It seems to crash when the application reaches 350MB of RAM.
I believe there is no memory release between passing trough the galleries, even though I am usingבarc וsegues.
In addition, on the first entry to each gallery, it takes a few seconds for the gallery to load (only on first run, if I exit and reenter the same gallery it enters immidiatly) which seems further clue that it is kept in the memory.
I would really appriciate any idea, even if it simple as this is a first app and I'm not very experienced.
Thanks for your time and help...
I am not sure the exact reason for this is the memory issue. but when you handling the big payload(data) on your project,you have to think about what happen the memory reach the maximum reachable data size allocate for the app at the time.
Thanx for the API you have a call back method when fires the app reach the maximum Data size the system can handle.
- (void)didReceiveMemoryWarning{
// in this metod you can remove(release) additional memory used by your view controller
// in your case UIImage objects of the gallery.
}
You can't call this method directly. it's a system call method.
but you have a option to ask to call method, when you debug in the simulator.
(Simulator Status Bar: -- Hardware > Simulate Memory Warning).
documentation here

Memory warning and crashes, but no leaks

I'm creating an app based on storyboard, where each of the views has quite a lot of large images. The app crashes (not a lot, it usually crashes after 10-15 minutes of intensive use) after having displayed a few memory warning.
I've checked instruments, and it's not reporting any single memory leak. Also, the allocation seems to be reasonable (I've only got 1 or 2 peaks in the game when I load some very xib containing very big images - around 8mo for the iPad retina version).
I don't really have any objects I can release when I receive a memory warning, as all the stuff from the previous view has already been deallocated.
I've seen that similar problem, but it seems to be related to a specific line of code, which is not my case : iOS - App crashing after Memory Warning - Instruments showing no leaks
Is there a way to force xcode to clean the images that are cached?
Otherwise, what can I do to prevent these crashes?
Thanks for your help !
I have had the similar problem, but my solution was at the end easy:
You should think the Iphone is like a car. A car can't speed up to 100 Mph in one second. A Iphone can't load big images in one second. So what you have to do is: you should shrink the size of the images and if there are more than 2 big images on one view just delete one of them or put it to other views.
If that is not your solution look for mistakes in the code and check up, where the app crashes.

Objective C iPad Animation with large images - What method to use?

I'm trying to build a weather application on the iPad but it seems that I need some help in animation. Say I'm animating a Radar, so the radar source files have 10 gif/jpeg pictures in 900x700 pixel size. I've tried the UIImage animation technique using the tutorial here:
http://www.icodeblog.com/2009/07/24/iphone-programming-tutorial-animating-a-game-sprite/
but it seems that loading 10 images that big is too much for the iPad to handle and its crashing due to memory warnings. I'm researching other techniques to animate but I can't seem to find something that will do this efficiently.
I've looked at others like Core Animation using sprites, and Cocos2D with sprites. Can someone point in the right direction the best way to animate these big images? (keep in mind that these images are dynamic and changes often so the sprites will have to be recreated on a server and fetched from the iPad to do the animation). Thanks
OpenGL only creates textures with dimensions at powers of 2. In the case of your images, that's 1024x1024, which is a meg of memory per image. Still, that shouldn't be a problem with the iPad.
First, investigate using Xcode's profiling tools to ensure the images aren't being repeatedly loaded into memory at each loop of the animation (likely by way of new objects that aren't sharing cached textures). That could solve your problem from the start.
Second, I recommend using Cocos2D if only for the easy handling of textures and caching. Toss the images into a CCAnimation, pop that into a CCRepeatForever, run it with a CCSequence. When you're done hit CCTextureCache to release unused textures.
Third, lower your animation framerate to 30 or less (if only for this animation). It may be the iPad, but you making a weather app. Not a video game.
Finally, downgrade the size of your image. Justify all you want, but a large radar animation will not sell your app. And just because a website might already be playing that animation beautifully, remember that a desktop has vastly more memory and power than any smart phone.
Try breaking the animation image into into smaller parts and animate those instead by treating each components as sprites. It would be best if you use primarily code (CoreGraphics) and draw your radar "by hand" instead of just using images as if they were animated GIFs.

Objective-C Cocoa-Touch WebView Memory Allocation doesn't stop rising

Related to this question: Memory leak tool tells me zero leaks but memory footprint keeps rising
I also have a rising memory-footprint, but I don't allocate images, and more importantly, I don't do anything in the background. There's just a UIWebView on the screen, showing a JavaScript-free page. Here are two screenshots from the profiling instrument, one is 10 minutes after the other, with zero interaction and zero background activity:
(NOTE: If I set the Allocation Lifespan radio button on the left on another value, it does no difference. The live bytes have rising by about 6MB within 10 minutes.)
Now my question:
Is this an instruments-bug? Or is the WebView allocating more and more memory? Or do you think it's impossible and that I must have gotten something wrong here?
Aww sorry, there seems to have sneaked in some JavaScript that made the problem. Using a test HTML page, memory remained static.