Out of Memory when Switching Activities - java

I'm making an android app with a menu activity and a game activity. Switching from the menu to the game and then back to the menu works, but as soon as you try to go back to the game activity, I get an out of memory error. The game activity loads in a few bitmaps, so in the onStop() method for the game activity I tried recycling all the bitmaps, calling finish(),and calling gc(), but none of it worked, I still get the out of memory error when I try to switch back to the game activity. I've tried researching this but so far I haven't found anything that helps. Is there any way to fix this and ensure my game activity is completely released from memory when it is closed?

OutOfMemory error occurs when your application did not get enough memory to proceed for object allocation. Check with the memory size given to the application since you are using bitmap memory consumption might be more. Please increase the memory limit and check the behavior.

Related

How to manage memory with too many items on android recycleview

I am developing an android social media type application, where I have a multiple tab activity. The tabs are fragments using viewpager.I have a home screen with a recycler view. The post have images on them. I am having memory problem with the recyclerview for post. I am using firebase as a database if thats helpful. To load the imageview I am using glide and I am doing diskcache and skipping the memory caches.
Issues I am having are
When too many posts are loaded, recyclerview becomes slow and laggy (I know that there are other post with this but couldnt really found anything helpful).
When I click on a username from the post, a new profile activity starts which also have all the posts from that user, I am trying to find a way clear the homepage tab recyclerview memory but struggleing with how to.
To solve the first problem, I have set up an onscrolllistner that loads 10 new post everytime you are near the end of the post list. The recycler view adapter currently loads 20 items initially and then so on.but I am not sure how to clear the old posts in an efficient way since I want the user to be able to scroll up.
To solve the second problem, In the homescreen fragment, when I launch the new activity, the onPause() function gets called. In the onpause() function I tried setting the list to empty, recyclerview adapter to null, recylerview to null but nothing has worked. In the android profiler tool, I see that I am using 200 mb memory, and when I start the new activity, it adds another 100 mb or so. I tried manual garbage collections but it doesnt work either. To be specific, native memory jumps up in the profiler. Ive tested and made sure that was only because of the images in the recyclerview.
I am not sure why when I set recyclerview, adapter and the post list to null, why the images from posts are still being held in the memory.
Glide caches the images
To load multiple post check paging library
When moving to detail fragment and clearing the reacyclerview of home fragment , I would not recommend this as next time user switches to home tab ,the user will not have previous state saved which he left previously.

How can I fix an OutOfMemory error in java?

I have 8 different activities that open each other on a button click randomly. After clicking the button that opens a new activity around 15 times, it crashes. Do I need to close the activities or is there another solution?
As Harsh Pandey said, you may want to look into using fragments instead of activities. Another possible way is to call finish(); right after you start the next activity. This will close the previous activity. However, if you ever intend to go back to this activity using a back button or something, it will not exist.
When you create new activities and close them, GC will automatically clean up the memory used by the old activities when required. You are getting an out of memory error because,
Your activities are holding up a reference which is still alive. This will prevent GC from cleaning up the old activities.
for example,
Look for animations which are not cleared properly.
The best way to deal with this is find the memory leak.
https://developer.android.com/studio/profile/investigate-ram.html
Suggestion:
As 3kings mentined, try to do in one activity. ie Use Fragments !!
You can request more heap size by adding this in your manifest:
android:largeHeap="true"
However, based on your comment, my guess is that this case is ideally suited for fragments, instead of activities. You can accomplish all of that in a single activity, with multiple reusable fragments.

Rajawali live wallpaper slow back from sleep

Im working on a 3D live wallpaper for android using rajawali. When I turn the screen off and on, its loading the whole scene again what takes some seconds depending on device and scene complexity. Id like to keep my wallpaper in memory "forever" to have the wallpaper visible instantly after the screen turn on.
From what I have found this behavior is because openGL loose context when calling OnPause and OnResume events. I tried setPreserveEglContextOnPause which "may" preserve context, but actually it changes nothing.
Can anybody help me to solve this problem?

Prevent LibGDX game from completely stopping when paused

In my LibGDX game for android, if the user backs out of the game (either by pressing the home button or switching to another application) LibGDX's built in pause() method is supposed to run. Now, this is fine, and it works fine as well. My problem is that if I back out of the game to do whatever, and then rejoin the game, it has restarted the app completely (kind of like if every time you exited and rejoined in the middle of a game of Pacman your score would be zero and all the dots would be back). For my screen switching, it is necessary that the game NOT restart every time the user exits, but simply enter the corresponding state to actually simulate the 'paused' game. How do I stop LibGDX/Android from killing the game altogether upon user exit, but simply pausing it?
The libGDX application lifecycle matches the lifecycle of the Android Activity as documented in the ApplicationListener interface so you should expect the same behavior. When you press the home button while in a libGDX game then the pause method will be called, which is the same as onPause in Android. The game will go to the background but will stay in memory. However this is not guaranteed and the OS might release the games memory for other applications, there really is no way to get around this. In the case when the game comes back to the foreground and the game restarts you'll need to load the games state from when it was paused.
I've written my own article on how to save and load the game state using Json in libGDX, maybe that will be useful to you.
You should use Asset Manager to prevent it, here a good tutorial :
http://code.google.com/p/libgdx/wiki/AssetManager
the official doc :
http://code.google.com/p/libgdx/wiki/AssetManager
Load all of your textures with assets manager
I have the same problem, but I found the solution by:
Disable option in Developer options called
- Don't keep activities (Destroy every activity as soon as the user leaves it)

Recycling Activity Views on the Activity-Back-Stack

I have an application that is very image-intense, and I'm finding that I'm running into alot of OutOfMemory issues when loading up multiple activities.
The activities are all gridviews or listviews of bitmaps, and clicking on an image takes you to another activity that contains another set of images (sort of like an album of albums of albums of albums). The first 3 activities run fine, but when navigating further down, I start running into some serious OutOfMemory Errors.
After doing a stack dump with DDMS, it appears that the GridViews and Listviews of the previous activities are hogging all the memory. This is kind of expected, since they are showing lots of bitmaps themselves, but I'm not sure how the VM goes about freeing activities in the stack, and if they do it even when you havn't called "finish()" on them.
Do Activities recylce their views when they go into the background and then restore them when you navigate back to the activity, or is clearing the imageviews in my gridviews and listviews this something I need to handle manually on the activities onPause() and onResume() when navigating away from the activity in the lifecycle functions?
Please see my answer on OOME
Always call bitmap.recycle() after using bitmaps, since GC cannot clear memory held by Bitmaps.
The above link has a generic solution, please go through it.
consider recycling bitmaps onPause

Categories