问题
I have one Android application that has 4 activities. First is splash screen basically, second is login, third is main and forth is one extra for drawing.
Running that application on real device - Samsung Galazy tab from 2014 or smth with Android 4.4 runs to error and crashes after login activity. It says out of memory error. Main activity contains of multiple edit text fields and buttons, with also radio choice and making fields visible and invisible.
When I set drawing activity after login, it will log in correctly and when going from drawing to main, it crashes first time, but runs second.
So I removed splash and login activity and then application runs smoothly - no lagging or anything. Can go to drawing and come back without problems. All functions work.
My question is, why is this happening? Why it can run without splash and login, but crashes with these two. I assume these two aren't heavy on memory. Can it be problem with that tablet or maybe I should look over memory consumption? Just seems weird for me that it can open up that main activity without these two.
回答1:
Out of Memory exception is quite common problem for applications. It can change device to device depending on their RAM size. To telling what causes the problem for you from here is impossible.
Low memory of the device, not using memory properly (The images you use in the activities, The references of the variable which hold, while not needed in activity which is on the screen) and other factors can cause this problem.
What should you do?
Optimize your materials, try to use lower resolution images, Make your variables null before changing activity or fragment to make system reclaim the memory they hold.
Use Leak Canary and find out if there is a memory leak in the app(https://square.github.io/leakcanary/)
Use Android Studio Profiler to monitor memory usage of the app to find out which part of the code uses memory most.
After understanding trade-off you can call
System.gc()
the garbage collector to reclaim unused memory before going to heavy task.if any of these not helps you can add the code at the below, to your manifest file(between application tag). But you should be aware of the performance trade-off to use this.
<application android:largeHeap="true"
来源:https://stackoverflow.com/questions/58357381/android-activity-memory-consumption