Crashing Ipad app, memory issues - objective-c

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

Related

UIWebView crashing

I have a problem with using UIWebViews in an objective-C app we're developing. It crashes any Retina iPad. Both iPad 1 and 2 behave fine.
The web view is loaded from local HTML/CSS/JS and is dynamic content that contains pricing information so needs to be essentially a mirror of a website which is pre-downloaded to the device.
The page contains lots of images so I think this is memory related. I've tried reducing the payload on the page which stops the crashes. Obviously the stupidity of Apple to choose to quadruple resolution whilst only doubling memory is a root cause of why it works fine on non-retina devices, but how can I manage memory within the page to prevent iOS from destroying the entire app?
Does iOS automatically store imagery as 2048x1536x32bpp as a bitmap (theoretically 12MB per image?) irrespective of the file format? I've tried converting to JPG / PNG but with no effect on the crashes. Only reducing the volume of images present on the page stops the crashes. It's my first foray into iOS development, so please be gentle!
iOS won't change the resolution of the original images that the page loads, but of course it will de-serialize them from .png/.jpg to a bitmap in memory for display, so you should start by trying to reduce the resolution of the .png/.jpg images that the page loads.

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.

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.

AVAssetWriter fails when Application enters Background during Rendering

In my app I am rendering a Video generated from images I retrieve from the users photos. I have set up an AVAssetwriter with a AVAssetwriterInput has an AVAssetWriterInputPixelBufferAdaptor. I'm able to transform the ALAsset objects I retrieve from the users library to CVPixelBuffers and add them to the Video, which then is saved as an mp4. Adding all the images to the video is done on a background thread which sends a Notification to the main thread every frame, so the interface can be updated. All this works well, and I get a usable Movie file out of the app.
My problem now is, that when the user enters another application, after becoming active again the status of the ALAssetWriter changes to "failed", an I am not able to add any more images to the movie file. First I thought I might have to end the current Session on the writer and reopen a new one, once the app has become active again, but that doesn't seem to help.
I was just wondering how the general approach would be when I'd like the user to enter other applications. The best solution would be, if the rendering could continue in the background. I suppose I'd need a background thread from the UIApplication. But for now I'd be happy, if rendering could just continue after resuming my app.
I won't post any code for now, because it's really a lot, and my question possibly is conceptual. If you need to see code, I'll post it.
Edit 1:
Tested on iOS 4.3 and iOS 5. I've seen background rendering on other apps such as iTimelapse, but I'm not sure which frameworks they use.
Edit2:
I now have the information of an apple devforum member, that AVAssetWriter does not work in the background. So is there any other framework out there capable of rendering quicktime videos?
Turns out that AVAssetWriter just won't survive the app being suspended. You can add an extra 10 Minutes of rendertime by requesting background time, but after that the AssetWriter fails. Same happens if you use certain services on the phone. For example making or answering a call will make the AVAssetWriter fail as well.
If there are any OpenGL calls made when your app is in the background then that would explain this behaviour, looks fairly likely. From the OpenGL ES Pragramming Guide
Background Applications May Not Execute Commands on the Graphics Hardware
An OpenGL ES application is terminated if it attempts to execute
OpenGL ES commands on the graphics hardware. This not only refers to
calls made to OpenGL ES while your application is in the background,
but also refers to previously submitted commands that have not yet
completed. The main reason for preventing background applications from
processing OpenGL ES commands is to make the graphics processor
completely available to the frontmost application. The frontmost
application should always present a great experience to the user.
Allowing background applications to hog the graphics processor might
prevent that. Your application must ensure that all previously
submitted commands have been finished prior to moving into the
background.
The docs go on to enumerate a set of guidelines for the enter background/foreground app delegate callbacks. I think finding a way to do the rendering without the graphics hardware would be tricky, also the frameworks that allow mp4 encoding (like ffMpeg) are mainly GPL/LGPL, so you need to be careful if dealing with a commercial product (LGPL means you can link to the library dynamically, not statically, which is useless on iOS), as the license would propagate to your code.

Unload image of UIImageView thats offscreen

I'm coding an application on Ipad, in a certain point of my application I present a ViewController with the presentModalViewController.
My ViewController is a UISScrollView who take the larger of the modalView and inside it I display some images, I allow pagingEnabled so I can see all my images inside the scrollView.
Sometimes I have to display more than 10 images inside the scrollView, so I have this error
RECEIVE MEMORY WARNING LEVEL=1 after this one RECEIVE MEMORY WARNING LEVEL=2 and finnaly the debugger exited due to signal 10 (Sigbus).
What can I do? is there a way to unload the image thats offscreen? or others things to do?
Thanks,
I guess you're adding all the images to the UIScrollView? Then the iPad has to keep all 10 images in memory. If they are full screen images, each of them will take up about 3 MB of memory, so you're using 30MB just to keep the 10 images in the scroll view.
You should only add the one or two images that are actually visible. Once they scroll out of sight, remove the UIImageView from your UIScrollView (and make sure you don't retain it anywhere else so it can be deallocated). When a new image scrolls into sight, add it to the UIScrollView only then.
In your UIScrollViewDelegate method -scrollViewDidScroll: get the current contentOffset and use that to calculate which images are visible.
Actually, this problem is even worse on the iPhone 3G, which can't even manage to display two full size photos (taken with it's own camera) at the same time.
If you can make the photos smaller ahead of time by resizing them (server side if you're snagging them from your own services, or client side if they're somehow generated on the device or retrieved from third party services), you should. They will load faster, and you can put more of them on the screen at once without running into memory issues.
The Three20 library has a really great photo-browsing control for the iPhone. I'm pretty sure they only have three photos around at any given time... the photo you're looking at, and the photos going forward or backwards.
http://github.com/facebook/three20
I also think there is an iPad branch of the Three20 controls (at ./tree/ipad), although I don't know if they include the photo browser or not, and haven't tried them yet myself... maybe that's an option for you if you don't want to spend a lot of time on that particular feature of your application. If the iPad photo browser isn't done maybe you could just adapt the iPhone version to suit your needs (shouldn't be hard).
If you want to bake the feature yourself, I'd take some inspiration from the UITableViewController. Your controller should come with associated datasource and delegate protocols to retrieve photos and respond to user events. The controller base class itself should reuse three UIImageViews and should shuffle those around to create the illusion that the user is scrolling through a large list.