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

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.

Related

Metal drawable musings... ugh

I have two issues in my Metal App.
My call to currentPassDescriptor is stalling. I have too many drawables, apparently.
I'm wholly confused on how to most performantly configure the multiple MTKViews I am using.
Issue (1)
I have a problem with currentPassDescriptor in my app. It is occasionally blocking (for 1.00s) which, according to the docs, is because there is no currentDrawable available.
Background: I have 4 HD 1920x1080 videos playing concurrently, tiled out onto a 3840x2160 second external display as a debugging configuration. The pixel buffers of these AVPlayer instances are captured by 4 independent CVDIsplayLink callbacks and, from within the callback, there is the draw call to its assigned MTKView. A total of 4 MTKViews are subviews tiled on a single NSWindow, and are configured for manual drawing.
I'm using CVDisplayLink callbacks manually. If I don't, then I get stutter when mousing up on the app’s menus, for example.
Within each draw call, I do a bit of kernel shader work then attempt to obtain the currentPassDescriptor. If successful, I do one pass of a fragment/vertex shader and then present the drawable. My code flow follows Apple’s sample code as well as published examples.
According to the Metal System Trace, most of draw calls take under 5ms. The GPU is about 20-25% utilized and there’s about 25% of the GPU memory free. I can also cause the main thread to usleep() for 1 second without any hiccups.
Without any user interaction, there’s about a 5% chance of the videos stalling out in the first minute. If there’s some UI work going then I see that as windowServer work in Instruments. I also note that AVFoundation seems to cache about 15 frames of video onto the GPU for each AVPlayer.
If the cadence of the draw calls is upset, there's about a 10% chance that things stall completely or some of the videos -- some will completely stall, some will stall with 1hz updates, some won't stall at all. There's also less chance of stalling when running Metal System Trace. The movies that have stalled seem to have done so on obtaining a currentPassDescriptor.
This is really a poor design to have this currentPassDescriptor block for ≈1s during a render loop. So much so that I’m thinking of eschewing the MTKView all together and just drawing to a CAMetalLayer myself. But the docs on CAMetalLayer seem to indicate the same blocking behaviour will occur.
I also grab these 4 pixel buffers on the fly and render sub-size regions-of-interest to 4 smaller MTKViews on the main monitor; but the stutters still occur if this code is removed.
Is the drawable buffer limit per MTKView or per the backing CALayer? The docs for maximumDrawableCount on CAMetalLayer say the number needs to be 2 or 3. This question ties into the configuration of the views.
Issue (2)
My current setup is a 3840x2160 NSWindow with a single content view. This subclass of NSView does some hiding/revealing of the mouse cursor by introducing an NSTrackingRectTag. The MTKViews are tiled subviews on this content view.
Is this the best configuration? Namely, one NSWindow with tiled MTKViews… or should I do one MTKView per window?
I'm also not sure how to best configure these windows/layers — ie. by setting (or clearing) wantsLayer, wantsUpdateLayer, and/or canDrawSubviewsIntoLayer. I'm currently just setting wantsLayer to YES on the single content view. Any hints on this would be great.
Does adjusting these properties collapse all the available drawables to the backing layer only; are there still 2 or 3 per MTKView?
NB: I've attached a sample run of my Metal app. The longest 'work' on the top graph is just under 5ms. The clumps of green/blue are rendering on the 4 MTKViews. The 'work' alternates a bit because one of the videos is a 60fps source; the others are all 30fps.

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

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.

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.

Animate screen while loading textures

My RPG-like game has random battles. When the player enters a random battle, it is necessary for my game to load the textures used within that battle (animated monsters, animations, etc). The textures are quite a lot, and rather big (the battles are very graphical intensive).
Such process consumes significant time. And while it is loading, the whole screen freezes.
The game's map freezes, and the wait time is significant - I personally find it annoying.
I can't afford to preload the textures because, after doing some math, I realized:
If I preload all the textures at the beginning of the game, the application will definitely crash.
If I preload the textures that are used in a specific map when the player enters the map, the application is very likely to crash as well.
I can only afford to load the textures when I need them, and dispose of them as soon as the battle ends.
I'd prefer to not use a "loading screen" image because it affects my game's design and concept. I want to avoid this approach.
If I could do some kind of animation while loading the textures, it would be great, which leads to my question: is that possible? What kind of animation, you ask? Well, how about... you remember when Final Fantasy used to distort the screen while apparently loading the textures? Something like that. But well, distorting is quite a time-consuming process as well, so maybe just a cool frame-by-frame animation or something.
While writing this, I realized that I could make small pauses between textures (there are multiple textures), and during such pauses, I update the screen to represent the animation's state. However, this is very unlikely to happen, because each texture is 2048x2048, so the animation would be refreshed at a rather laggy (and annoying) rate. I'd prefer to avoid this as well.
In a similar bind, i chose to
Convert all my animation textures to gzipped PVR. The load time (depending on the device) is improved by a factor of 2 to 4. Any artefacts caused by a conversion to PVR are not noticeable in motion.
I preload the idle animations (almost always on, except during a skill use or when hurt. I do this during the battle scene's fade-in. I control the fade-in myself on a tick rate of 50 ms, and at every frame i fire-up a preload of one of the idles ( there are a maximum of 8 of them, they take 20 or so ms.).
I have an 'engagement' class which computes the entire fight ahead of time. When an animation becomes un-needed, i unload it. Also, during a 'hurt' animation, i prefetch the next skill animation.
loads of fun. Best of luck with your game.
ps. Dont trust the simulator for actual response times. Go to devices rapidly to determine if you really have a performance issue.
pps. About point 1, that caused a significant size reduction for my app.
Since the battles are supposed to be at random, would it be possible to preload the textures for the next battle before that battle happens? Then the battle can start whenever the loading has completed.
Game decides battle should happen soon
Generate random encounter (monsters/background/etc?)
Load textures for the encounter
Start the encounter after the textures have loaded
The battles are still random, it's just that the encounter has been determined a bit before the user is aware a battle is about to happen.
You could load lower-resolution textures first, and in a background thread (NSOperation I think) kick off the load for the bigger textures, and 'swap' them when done.
As for animation, a lot of games start by loading the small textures when the player is far away, and as they get closer, the higher-res textures will 'fade' in