Reputation: 39748
So, I tested my game out for the first time with the Monkey test. I managed to go about 3 minutes without a crash, but I ended up crashing with an out of memory error, and I'm trying to figure out how I might make it better.
My program is structured as follows:
What I'm wanting to know is how I can improve the memory management of my program so it won't crash. I suspect that I need to manually delete some of these variables, but I'm not sure what the right place to do so is. Thanks!
private Level_Score_bar score_bar; // Custom view
private number_viewer num_viewer; // Custom view
private number_pad num_pad; // Custom View
private int time,score,level,num_remaining,current_var,change_loc,time_remaining;
private ArrayList<Integer> the_key;
private ImageView Number_to_select;
private Boolean update_viewer;
Random rseed;
Vibrator bzzz;
long ctime;
private Activity self=this;
private SharedPreferences prefs;
private Editor prefs_edit;
The out of memory occurred
setContentView(R.layout.level_layout);
This layout is rather complex, containing several image views, buttons, text views, etc.
Upvotes: 0
Views: 3587
Reputation: 39748
So it turns out, I was right on the hairy edge of the Heap. I am using quite a few images, and it turns out that I was over the "normal" heap size. I've managed to improve this situation by shrinking some of the images, but the best solution came by changing my manifest to the following:
<application
android:label="@string/app_name"
android:icon="@drawable/logo"
android:screenOrientation="portrait"
android:largeHeap="true">
The large heap will allow me to do future upgrades (Which will include level designs, etc, which would take up quite a bit of space...) All in all, it should be a pretty minimal affect.
I selected @Booger's answer as this was the piece which allowed me to do some research and figure out that my heap space just wasn't large enough, but I also included @Ramam-Mystry's piece of code.
I also used many answers from this excellent question in my quest. I started to store references instead of bitmaps, and a few other related bits. All in all, my memory consumption is down 25%, and continuing to improve.
Another tip could be to use the onLowMemory()
function in Activity, trim the memory that's not needed then.
Upvotes: 3
Reputation: 5854
Try by using System.gc();
in onresume()
to avoid memory leaks on using high resolution or high memory pictures in imageview.
Note: For imageviews using bitmaps, default garbage collection is not called, so the execution memory exceeds the allocated memory when images memory is higher. manual garbasge collection to be done to avoid memory leak. it is better to call in onresume();
Upvotes: 1
Reputation: 18725
It sounds like you need to check out the "Allocation Tracker" tool, which is available in the "DDMS' Perspective in eclipse.
This will show you exactly which data structures are consuming memory.
Upvotes: 2