Question

I have been coding this application that shows images of bus lines schedules. There are currently 32 lines and each of them has it's own image (can't use text due to the formatting and lack of data, had to do a compromise). I made it fetch an echo from a PHP file on my server and it retrieves data regarding bus lines and their images, then it downloads that data and images to the internal storage and loads the buttons for each line (dynamic layout).

ImageViews are set to null when they are created and they are only populated once you click on the button. I have noticed that my app uses around 20MB of RAM just as it starts. Opening 10-15 lines (10-15 images are loaded into ImageViews), RAM usage climbs up to 60MB and then it crashes (reaches my device's heap size limit). I know that image intensive applications tend to use a lot of RAM and my plan is to limit it to only one image per time (so once the user clicks on some other bus line, the others are closed and their imageviews are set back to null), but what is wondering me, can empty ImageViews cause RAM to be around 20MBs? My images are 320px in width and ranging from 200 to 1500px in height (I know, it's a lot), but I have set them to grayscale and compressed them (Size of the images is from 50kb up to 150kb).

My application's APK is 710kb in size, app when installed is around 3MB and with data it totals to 7.5MB.

So, to clarify, my questions are:

*1) Can empty ImageViews cause high RAM usage as well?

2) If yes, should I dynamically create an ImageView and populate it once the user clicks on the button, instead of the current approach when they are all created at startup?

3) Can multidimensional variables (mostly strings, 6 strings with 35 dimensions) cause high RAM usage as well? Most of them are around 10 characters long.

4) Are there any tools which I could use on Android Studio which could help me to locate possible memory leaks?

5) Do non-global variables get automatically destroyed once the ie. function has returned a value or do I need to manually set them to null after I'm done with them?

6) What are generally some good practices to reduce RAM usage on Android when developing an app?*

Sorry for not being very technical here, I'm not a professional programmer or anything, this is more of a hobby of mine and I'm trying to learn a bit more here. Thank you for reading.

Image that shows what my app does:

All bus lines are collapsed

enter image description here

Once you click on the button, it shows the bus schedule for that line enter image description here

Was it helpful?

Solution

*1) Can empty ImageViews cause high RAM usage as well?

Probably not. It's the bitmaps themselves that take a lot of space, not the ImageViews. Keep in mind that in-memory bitmaps won't be compressed, so it's not the file size that you need to take into account but the number of pixels.

3) Can multidimensional variables (mostly strings, 6 strings with 35 dimensions) cause high RAM usage as well? Most of them are around 10 characters long.

Probably not.

4) Are there any tools which I could use on Android Studio which could help me to locate possible memory leaks?

You should use the Android Debug Monitor (Tools menu > Android > Monitor (DDMS included). It has a memory heap viewer that will show you what the big offenders are. It can take heap dumps in HPROF format that you can also analyze with other Java heap viewing tools.

5) Do non-global variables get automatically destroyed once the ie. function has returned a value or do I need to manually set them to null after I'm done with them?

Unless you're doing something strange, the memory used by local variables is eligible for garbage collection once those variables pass out of scope, so to answer your question, you don't need to set them to null.

OTHER TIPS

In my case, I only put the 1 image to drawable so the RAM of application increase a lot.
Then, I put image to each drawable-...pdi files, the RAM size decrease

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top