Ah.. We've developed a good iPhone application. Now, 'm passing through last phases of it, i.e. profiling it and I've encountered few problems. Application has few leaks and objects occupying large memory chunks. We just checked somehow, application is not lowering its memory requirements and blocks remain occupied with creation of each View Controller.
Some of the views I really don't want after their disappearance, but they are not deallocated.
We're also downloading large files into iPhone through app but once we download very large file (> 10 MB), it crashes. Because after download we've also used thumbnail generation logic into which UIImage is created with 'contentsOfFile'..! So, app generally crashes after use of large files. We've used UIWebView for thumbnails.
My real problem is download, thumbnail, preview of larger files... clearing unnecessary memory (objects) once view is not in focus..!
Can anyone help me get rid of such problems easily???
I really don't wanna go through long long code..!
Thank You..!
As has been written hundreds of times on SO, use ASIHTTPRequest for networking, especially for large files. It can stream big files directly to disc so you don't run out of memory. As for creating a thumbnail of a >10mb file, it sounds like you would do yourself a favor by storing a thumbnail on the server instead.
If your views don't unload, something is wrong with your retain/release cycles. Have you implemented viewDidUnload on all your view controllers? Without more details, it's very hard to help.
Related
I have a RSS feed reader app that I'm running through profiler, and I'm noticing that whenever I load a uiwebview and pop it from my navigation controller, my Live Bytes goes up by a small amount (.10-.40MB). I'd expect Live Bytes to stay where it was at before the uiwebview was launched. Does this indicate that I'm leaking something or is this normal for uiwebview objects?
On the basis of your comments, I went back and examined my webview to see if there were any leaks. Profiler didn't report any leaks, but the number of Live Bytes was definitely going up a small amount every time I presented and subsequently dismissed the webview. In my case, Live Bytes was going up by 40-50kb each time, not 100-400kb, but nonetheless, it was definitely growing. My earlier comments were based upon leaks identified by Profiler's Leaks tool (e.g. the Twitter.framework definitely has a known identified leak but it's tens of bytes, not kilobytes). I generally don't track Live Bytes, so I apologize if I misled you.
So, in answer to your question, I guess it is not unusual for memory to be consumed as you present and dismiss webview controls. I would not be inclined, though, to consider the growth in Live Bytes a leak unless the Profiler is also reporting a leak in the Leaks tool.
The Info
I recently launched an app on the AppStore. After testing on the simulator thousands of times, and actual devices hundreds of times we finally released our app.
The Problem
Reviews started popping up about app crashes when the user launches the app. We figured that the app crashes on launch on iOS devices with less than (or equal to) 256 Mb of RAM. The following devices are devices our app supports with less than 256:
iPod Touch 4G
iPhone 3GS
iPad 1
The app doesn't always crash. Sometimes it launches fine and runs smoothly. Other times it crashes. The time from launch (when the user taps the icon) to crash is usually two seconds, which would mean that the system isn't shutting it down.
Findings
When using Instruments to test on certain devices, I find the following:
There are no memory leaks (I'm using ARC), but there are memory warnings
Items are being allocated like crazy. There are so many allocated items, and even though I'm using ARC it's as if ARC isn't doing what it's supposed to be doing
Because of what I see as "over-allocation", the result is:
This app takes (on average) 60 MB of Real Memory and 166 MB of Virtual. When the app launches the memory being used quickly increases until it reaches about 60 MB at which point the view has been loaded.
Here is a snapshot of the Activity Monitor in Instruments:
I know that those figures are WAYY to high (although the CPU % never really gets up there). I am worried that ARC is not working properly, or the more likely case: I'm not allocating objects correctly. What could possibly be happening?
The Code and Warnings
In Xcode, there are only a few warnings, none of which pertain to the app launch or any files associated with the launching of the app. I placed breakpoints in both the App Delegate and my viewDidLoad method to check and see if the crash occurred there - it didn't.
More Background Info
Also, Xcode never generates any errors or messages in the debugger. There are also no crash reports in iTunes Connect, it just says, "Too few reports have been submitted for a report to be shown." I've added crash reporting to my app, but I haven't released that version.
A Few Questions
I started using Obj-C just as ARC arrived, so I'm new to dealing with memory, allocation, etc. (that is probably obvious) but I'd like to know a few things:
How can I use #autoreleasepool to reduce my memory impact? What do I do with memory warnings, what do I write in the didRecieveMemoryWarning since I'm using ARC?
Would removing NSLog statements help speed things up?
And the most important question:
Why does my app take up so much memory and how can I reduce my whopping 60 MB footprint?
I'd really appreciate any help! Thanks in advance!
EDIT: After testing on the iPhone 4 (A4), we noticed that the app doesn't crash when run whereas on devices with less than 256 MB of RAM it does.
I finally solved the issue. I spent a few hours pondering why my application could possibly take up more RAM than Angry Birds or Doodle Jump. That just didn't make sense, because my app does no CALayer Drawing, or complex Open GL Graphics Rendering, or heavy web connection.
I found this slideshow while searching for answers and slide 17 listed the ways to reduce memory footprint. One thing that stuck out was PNGCrush (Graphics Compression).
My app contains a lot of custom graphics (PNG files), but I hadn't thought of them affecting my app in any way, apparently images (when not optimized properly) severely increase an applications memory footprint.
After installing PNGCrush and using it on a particularly large image (3.2 MB) and then deleting a few unused images I ended up reducing my apps memory footprint from 60+ MB and severe lag to 35 MB and no lag. That took a whopping five minutes.
I haven't finished "crushing" all my images, but when I do I'll update everyone on the final memory footprint.
For all those interested, here is a link to a blog that explains how to install PNGCrush (it's rather complicated).
UPDATE: Instead of using the PNGCrush process (which is very helpful, although time consuming with lots of images) I now use a program called ImageOptim that provides a GUI for multiple scripts like PNGCrush. Heres a short description:
ImageOptim seamlessly integrates various optimisation tools: PNGOUT, AdvPNG, PNGCrush, extended OptiPNG, JpegOptim, jpegrescan, jpegtran, and Gifsicle.
Here's a link to the website with a free download for OS X 10.6 - 10.8. Note, I am not a developer, publisher or advertiser of this software.
In my application I'm displaying available free space for my application in the device. The value is changing every time when I launching the application. Why is it happening like this.
Initially my available free space is 30,752,768 bytes and after allocating all the objects the available free memory is :2,809,856 bytes. after this I added some images to the view by using camera option then I received memory warning level = 1 and after that i get free memory space as 12,705,792 bytes. Why it is increased like this.
Can any one help me.
Memory warnings trigger a release of unneeded objects. This may include views that are not displayed and intermediary data that does not need to be in memory, especially stuff that can be recreated as needed – perhaps even pre-scaled images.
I am currently writing an app where I allowing a user to select images from the photos app, and add them to my app. I find that when the users does this a couple times it is fine. The problem comes in when they do this 5-6 times, it starts to slow the app down.
I am freeing up resources, and ran it through instruments and found no leaks. I'm just curious if there are any other things I should consider when saving images to the app this way. I just not seeing anything obvious at this point.
Try FreeImage or OpenCV library for reading/writing Images. Maybe they'll be more efficient than yours
I'm having strange effects with my app. I implemented my own PDF viewer. It shows ONE page at a time. Using Instruments Activity Monitor I see that my real memory is constantly at around 50MB.
After switching pages forth and back a couple of times I receive a memory warning level 0.
I do my best to react on it and sacrifice the low-res background image I'm rendering first to show something until CATiledLayer catches up.
Does not help. A few pages later I get memory warning level 1 and level 2 and after a few more pages my app gets killed with reason "9". Memory NEVER goes above 50MB!
Why do I get those warnings in the first place? There IS enough memory available.
This is happening on on iPad running iOS 4.3.
I don't think there's anything mysterious going on here -- which I'm sure is not what you wanted to hear. There are no absolute figures of "safe" amounts of memory to use. The rule is: when the OS tells you you're using too much, use less. It will jettison background processes first and in preference to your foreground app but there are still limits.
In the "olden days," you used to be lucky to get 20Mb. I'm sure you can safely get more than that on an iPad but, apparently, it's less than 50Mb.
You don't say how much memory you free by releasing the background image, but it seems that you need to be caching less data. You might also want to check Leaks (also in Instruments) to make sure you're releasing the objects you think you are.