May many webviews cause my app to crash on my iphone 4? - iphone

//The point
I gradually load many objects (UIWebView) to the memory and after a while it crashes. Please Help
//Details
I have a UIScrollView with horizontal paging, wherein each page (with a separate view controller) contains a UIWebView with disabled user interaction. At first only webview #0 on page 0 gets loaded and as I advance through the pages the app gradually sends requests to load specific data. Downloaded data stay in memory.
After I load about 20 Webviews from internet source, my app freezes and crashes. If I use a dummy html string stored on the device, this happens after I load about 50 webview. But the problem persists.
I fixed much of the leaks and am only left with Mallocs. Since the problem does not occur with low quantities of UIWebviews Loaded I assume that I have to change my code to say 3 webviews and reuse them. But I don't know if that'd fix the issue. I have developed several apps before which don't crash and this is a unique case.
I store pointers to webviews and some other elements inside every Webview (such as labels) in arrays. The arrays get incremented in count with every new page loaded.
I use xcode 4.3 and a jailbroken iphone 4 to develop. I turned off requiring signatures in my xcode as a result of which the app gets copied onto my phone yet fails to attach to process. I cannot debug on my phone. My app works well in simulator but crashes a while after I launch it on the phone by tapping the icon.
From what I am able to describe as so, what do you think might be the problem?
Thanks

Yes, certainly having too many UIWebViews will cause a crash due to low memory. I'm not sure what your reason is for having multiple UIWebViews, but I would follow through with your idea to reuse a smaller amount of them instead of creating several.
Based on your device not crashing when you use a small amount, I would say that the problem lies in the use of too many UIWebViews and not in a compatibility problem with your jailbroken device, for example.

WebKit is notorious for its memory hogging. It mallocs large blocks of memory and fails before giving you the opportunity to release other resources. One UIWebView is bad enough. I've had this problem in apps with as few a 5 UIWebViews, let alone 50.
There are a couple of things you can try. One, if the web pages aren't too complicated, you could use a replacement view such as DTCoreText to render them. If you have to have webviews, you could cache the various HTML contents and re-render using one UIWebView.

You only need to use three UIWebView's for paging. Lets say the user is on page 2 (page 1 is the left page and page 3 is to the right). When the uses swipes to page 3, you could just display the web view currently on page 1 on page 3. You need to re-load the proper html when the user swipes between the pages.
With some caching of the web content, speed shouldn't be a problem once the stuff is downloaded.

Related

How many UIWebView 's visible at once before hitting memory limits on ipad

I'm working on an ipad app which is meant to have several UIWebView controls visible at once, and was wondering what a sensible limit is to the number that we can have before running into memory issues on the ipad?
Thanks
You have to be dynamic about it. Don't set a hard limit in your app, wait for the memory warning and then start purging web views, lazily reloading them as the user needs them. You should screenshot the view first using [CALayer renderInContext:] so it appears seamless.
After all each web view will have different memory and CPU requirements. As always, the mantra is test, test, test.
... but, of course, Safari limits you to nine open tabs. Which is very much a hard limit. So... there's that.
it has no a sensible limit for webview for your app, it depends on the ios that the memory left
but use it carefully, webview is hard to controller the momery ,
it's a trick , before you release a webview do this may reduce the memory uses
[web loadRequest: nil];
use lots of webview at the sametime may not be a good idea
FWIW, I managed to get 20 UIWebviews on screen at once on an iPad 1. The pages all had javascript code in them, they weren't bare bones either. It took a while to load them but i got no memory warnings, and once loaded everything scrolled smoothly. Using the code from here:
Determining the available amount of RAM on an iOS device
I then checked the memory usage, which was:
113M used, 17M free, 131M total, which sounded pretty good.
I didn't push for any more than 20, that's way more than i'll ever use in practice. I'm impressed.

Is it a good idea to add uiwebviews in table cells?

I have several local html files and I want to show them in a table with static(for now) cells.
Each UIWebView would have a different height (I dont want to have any scrolls in the UIWebView) so obviously each cell has a different height. Most of the comments i read discourage the usage of UIWebView in table cells. What do you think : is this applicable ? will it be slow and need time to load?
UIWebview in tableview cells will affect the performance for sure. To display Html content in tableview cells you can use NSAttributedString.
I used a UIWebView in an app to display content coming from the web as rows in a table view.
I know that doing that is discouraged, due to the high memory use of UIWebView, but in my case it was not only a matter of displaying rich text: I had to display HTML loaded from the web, so I give that option a try. In short, I tried it and it worked fine.
The app got memory warnings from time to time, but it was also able to recover memory effectively and run unaffected even on an ipod touch (just 256MB of memory). Performance was possibly affected, but due to the fact that the app was doing just that (loading text from the web and displaying it in the table), the app was perfectly responsive.
One important notice: pages were pretty simple, just text, and each table did not contain more than 15 elements. Furthermore, one thing you have to consider is that the UIWebView introduces a small delay in the rendering, so you will have to deal with it and accept it in your app (there is no way to get rid of it that I know of).
In a later project, I used a different approach: using just one UIWebView and creating the table using HTML/js inside of it. Well, it also worked, but I could not see any big improvement as to peak memory usage nor performance (I checked with instruments).
This is just my experience, I am not trying to say that this will work or that is good practice to do. In any case, I hope it helps.

UIScrollView paging works fine in simulator but application crashes in device after 15-20 pages

I have used UIScrollView with paging enabled. I have more then 1000 images and I am adding 12 images per page. I am loading 12 images every time when page is scrolled. But after scrolling 15-20 page application is getting crashed in device. It works fine with simulator. Please let me know what can be the issue ?
Thanks in advance.
You didn't provide any code but it looks like this is a case of bad
memory management, as all the comments suggest.
One way to solve this would be to allocate just three pages (each holding 12 images)
and then joggle with their positions and contents according to user interaction.
I think you'll find these two links helpful:
Creating Circular and Infinite UIScrollViews
UIScrollView Infinite Scrolling

iPhone Application Memory Issue

I have created an iPhone Application where I have managed to handle leaks using the Profiling tool of XCode.
I have a gallery of images shown in UIScrollview when I load the view.Images in the gallery changes on each load of the view of iPhone.
I managed to remove the leaks using the profiler, but what happens now is memory gets increased by some amount on each load. I really cannot get why the memory increases on each load,when there are no leaks in the application.
Can anybody help me in finding this issue?
If you don't have leaks in the app it does not mean that your memory management logic is correct.:d
Do an analyze from tools menu.
And read again your code to see where you can release objects that are no longer needed.
In a gallery you should load only what users sees and the previous and next image only one step.
So if you are displaying one image on screen load only the next and the previous if you have one. So you will have only 2 or 3 images alive in memory. When user scrolls load next and release the previous, you can cash more than one image like 2 3 4 depending on the size,

App like default photo browser in iphone?

i am developing a app which contains feature like default photo browser in iphone. I done some what similar to that. but after loading some(near about 10-15) images from remote server,i am receiving memory warning.My requirement is loading image one by one. For this, on scroll view i am putting an images and increasing the contentSize of scroll view. it will work fine. but due to memory warning app quite.
Guys, any have any idea to approach for this feature which work similar to photo app without problem?
thanks in advance .
You're running out of memory because you're keeping the data for 10 or more images in memory at one time. You need to have more logic in your code that not only preloads and increases the scroll view's content size, but also removes UIImageViews from the scrollview (and thus from memory) as the user scrolls to newer stuff. (You can also save "evicted" images to the cache area on disk so if the users scrolls back you don't have to go to the server again.)
If you use a UITableView, it will request the images only when needed, and will automatically purge off-screen cells to save memory. It may not fit into the aesthetic for your application, though.