How can I prevent app from hanging on phones with low RAM? - android

I have developed an app which uses appcompat theme as the main theme.
In some activities there are more than 300 lines of code which use several card views, and in them are spinners and edit texts.
It performs well in many phones, but in phones with low RAM it hangs and causes the memory to be freed
I've used asynctask but the InBackground method is worker and doesn't interfere with graphics.
How can I handle this?

There is really good documentation on this issue on the Android Developer website: http://developer.android.com/training/articles/memory.html
Please post code if there is a specific area that you are concerned with.

When programming to mobile we need to face this kind of challenge. You should specify a target when you start to develop. In other words, it is really hard to develop an app which has the same and perfect behavior in all devices.
Brotip: Try to break your code. Is not usual to have more than 300 line of code in one file. Your method probably can be optimized. Otherwise, there is nothing you can do, once you reach the physical limitation of RAM size you will face this problem.
A solution for this problem COULD be use a Local DB and store the information that you use in this activity. Then you read it little by little.
Hope it helps and give you some ideas.

Related

App Memory Consumption

I have been playing with optimizing my app, and I see that there are some general ideas on how much memory it should use... I'm running between 70-80Mb right now, is that a crazy number or just a little on the heavy side?
My second question is about the memory I am using. About 42Mb is taken up by graphics, whether I run the app with the layout graphics or run it without setting any views... any ideas on where to look? I feel like it is the theme, but I am not sure how to test that.
I've been searching for days to figure out why it uses so much and cannot figure it out. It remains constant regardless of which activity I am on, any ideas are welcome!
UPDATE:
I ended up running it on an emulator and got about 26Mb, more what I was expecting, but it runs at 70Mb with the 42Mb of graphics on the actual device.
The emulator allows for a 384Mb app after getting memoryclass information, while the device only allows 256Mb... the emulator has more storage and runs more efficiently.
The emulator is Android 8.1 27api while the device is Android 7.0 24api.
Tried on an older Android 5.1.1 api 22 below also.
Any ideas?
I have another question with about why the emulators have a such a difference in performance here if you know the answer on that, but I wanted to keep these questions separate: Other Question
Android Studio has an awesome tool called Memory Profiler. It helps you analyze possible memory leaks and see which calls are using up the most memory. You can even pause the processor and 'zoom in' to deep dive on the call stack.
You can use LeakCanary A memory leak detection library for Android and Java. Maintain by Square. This is one of the best developer's Tool for memory optimization and detecting leaks.here

Any Chance to make CodeMirror play on Android Chrome?

I understand CodeMirror has issues on mobile and behavior varies with extensions in use.
However, for me it seems pretty much unusable everywhere I encounter it on Android. Have a look at Kotlin Koans and try the Backspace key. Amongst other things, I get gobbledygock and a bunch of newlines.
I did not find a single way around this. Tried two devices (OnePlus 3T and Pixel C) running Android 7 and 8 using on-screen keyboards or bluetooth physical ones. The results are pretty much the same.
This seems pretty odd and I am surprised nobody (Google?) is stepping in to fix it.
I'd like to use an Android device for basic (CodeMirror-based) coding, and I'd very much appreciate a suggestion to get around this annoyance. :)
Thanks
Code Editor inserts a new line above current line on every keypress
Android native app using WebView, backspace doesn't work properly

Soft References for Android 2.3+

I have an app that loads a lot of bitmaps to form one large one.
I use soft references to allow these bitmaps to be stored appropriately. You are able to scroll over them with no problem on android OS < 2.3 However, once you get to 2.3 or higher, Android became much more aggressive with its soft reference collection. Since this happens, the app then runs very slowly on these newer phones.
I've seen this as a documented issue here:
http://code.google.com/p/android/issues/detail?id=20015
Does anybody know a good way to work around this? It's ridiculous that an app works way worse on a newer phone! Thanks for the help.
Soft references and weak references are handled in a different way above android 3.0. So it is better if you don't use them for bitmaps. You can use alternate methods like scaling down the bitmaps, disk cache, memory cache, etc. For more details on how to handle bitmaps refer http://developer.android.com/training/displaying-bitmaps/index.html

Move from tablet to phone

I have developed my app for tablet. Now I need to convert it to run on phones. What changes to I need to make? Are there any particular steps I need to follow? I tried deploying the app as-is on the phone emulator, but stuff appeared to be cut off.
You've asked a general question, so the best I can do is give you a general answer. The steps and procedures outline here should be just what you're looking for. In a very general sense, you're simply going to want to have different layouts for different screens sizes. Android makes this very easy to do with their resource "buckets".

How to test android application?

Developed a application of size 40MB. I need to test it for all screen support but the android emulator really a bad choice(I feel... ). It gives Insufficient memory error almost every time). How developer test their application?
One more Question
I have designed app for four different layout(normal,small,large and xlarge). Will every device(In future) satisfy these layout params?
And i faced a real problem that i tested my app in Sony xperia minpro(Small screen 240*320 2.4inch) and in Samsung galaxy 5(smallscreen 240*320, 2.8inch) but the layout is overlapping in samsung device. This can be a serious problem , actually we cant check our app in every device.. that is impossible too.
TIA
40MB is way too big for an Android application. Many users will have problems installing the app on their devices. You should consider moving some resources out of the application and downloading them either on demand or on first app run.
The list of layout types (normal, small, large, xlarge) is definitely not final, for there quite possibly will be even larger screens (xxlarge) or tiny ones (xsmall?).
Developed a application of size 40MB. I need to test it for all screen
support but the android emulator really a bad choice(I feel... ). It
gives Insufficient memory error almost every time). How developer test
their application?
You can configure the emulator with any amount of memory you wish, including an emulated SD card so memory shouldn't be a problem. However, 40MB is quite big so you may be hitting the package size limit.
One more Question I have designed app for four different
layout(normal,small,large and xlarge). Will every device(In future)
satisfy these layout params?
You're asking us to predict the future - there's no way we can know what Google are planning if they haven't already announced it though I would suggest that there will never be a commitment to keep screen sizes or resolutions static - technology constantly evolves and specs that are OK for today, will not be OK for tomorrow.
I have seen dictionaries weighing in at 40Mb, best practise is to download the database as a separate file. Some graphically intensive games approach that size. If you want to emulate many Android devices make sure your PC is up to snuff and you have the latest SDK.
How developer test their application?
You do not have so many choices: you have to use as many (and different) physical devices as you can, from different vendors and technical specifications (screen, etc), to try to detect as many specific bugs as possible.
This is difficult, as you are often limiten to a few physical devices.
To give you examples, I recently struggled with the Camera, for a bug happening with Motorola Defy only. I am currently struggling with the Camera, but only for Samsung Galaxy this time.
When you find a specific bug, try to fix it "the general way": instead of detecting the vendor/version of the device to write specific code for it, try to enhance your code in a way it will work for all tested phones. So far, I never had to write anything specific to a given device. The bugs I encountered were always tied to a permissivities or particular cases that could be handled by making the common code more complete or resiliant. Let's say by "making as less assumptions as possible" knowing that we tend to make assumptions without meaning it.
On top of testing on as many physical devices as possible, create emulators. You can parameter them to have different screen layouts, different embedded hardware, memory, etc. And on top of the default emulator that comes with the Android distribution, you also have emulators provided by the devices vendors and that reproduce the specificity of these devices. For example, Samsung released a Galaxy Tab emulator. Sony Ericsson released a EDK Cellphone emulator. You can get them thru the regular android distribution update workflow.
Will every device(In future) satisfy these layout params (normal,small,large and xlarge)?
Yes, as Android distributions are backward compatible. Any of these layout will still be supported in the future, but may become 'deprecated' (so not recommended, but still working), and new layout types will certainly be created.

Categories

Resources