开发者

Android Garbage Collector and ArrayLists

I'm having a problem with the Android Garbage Collector and was hoping someone could point out what I may be doing wrong here. I have a lot of data at the start of my program that is generated and thrown into an ArrayList. Example of what I'm doing below...

m_aUnitsPhysical.add(new UnitData("P01", 1, 0, 0, false, false, 300, UnitType.PHYSICA开发者_StackOverflow中文版L, 1, "Unit Name"));

There's about 236 of these statements in my class constructor. The array is defined as:

private ArrayList<UnitData> m_aUnitsPhysical;   // Physical array listing

And in case it helps, the class constructor is...

public UnitData(String p_szID, int p_iAtk, int p_iDef, int p_iUp, boolean p_bLoot, boolean p_bHonor, int p_iCost, UnitType p_Type, int p_iLevel, String p_szName)

UnitData is a class of mine that simply stores the data, kinda like the old C struct. I know I'm chewing up huge amounts of memory during this operation. When it completes however, the Garbage Collector (as shown in logCat) kicks in and free's up huge amounts of data.

So, what's going on? Is the garbage collector angry with the way I'm doing the above statement, or is it telling me it's killing off memory from other applications? Is there any way to tell? Is there a better way of doing what I'm doing above that won't use so much memory? Should I try and use the SqlLite database instead of doing it this way?

I rarely get reports from my users that they've run into a memory issue, but they are occurring. Predominantly its when they leave the app for some reason or another then come back to it; the data in the array dissapears. I'm just trying to get a handle on how I should be doing this in case I'm doing it wrong. Any insight would be greatly appreciated!


If you are shipping the app with a static set of data (as it sounds you are) then you may want to include your data set in a pre-made database. Here is a great tutorial on how to do just that.

That said, 200 records should not cause any issues with memory. If the issue occurs after a user re-opens your app a few times, make sure that you aren't reloading any data unnecessarily; check to see if your array is populated already before loading it up again.


Use a database for large amounts of data. that's the best and recomended way to go.

for the current setup try creating less objects. Try this with a single object and just replace its data and add it to a list.. Don't know if this will improve performance but i believe it should. constructors are costly.


The GC will never delete strongly referenced objects (i.e. not referenced via WeakReference or the like), even if it runs out of memory (in that case, it will throw an OutOfMemoryError).

Your problem must be somewhere else.

(Other posters have recommended using a database to store your values, but for just 200 items, I see no problem with your approach -- unless these objects are extremely complex/huge)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜