简体   繁体   English

Android垃圾收集器和ArrayLists

[英]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. 我在使用Android Garbage Collector时遇到问题,希望有人可以指出我在这里做错了什么。 I have a lot of data at the start of my program that is generated and thrown into an ArrayList. 我的程序开始时有很多数据,这些数据已生成并扔到ArrayList中。 Example of what I'm doing below... 我在下面做的事的例子...

m_aUnitsPhysical.add(new UnitData("P01", 1, 0, 0, false, false, 300, UnitType.PHYSICAL, 1, "Unit Name"));

There's about 236 of these statements in my class constructor. 在我的类构造函数中,大约有236条这样的语句。 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. UnitData是我的一类,仅存储数据,有点像旧的C结构。 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. 但是,完成后,垃圾收集器(如logCat中所示)启动并释放大量数据。

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? 我应该尝试使用SqlLite数据库而不是这样做吗?

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. 也就是说,200条记录不应引起任何内存问题。 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 (ie not referenced via WeakReference or the like), even if it runs out of memory (in that case, it will throw an OutOfMemoryError ). 即使内存用完,GC也永远不会删除强引用的对象(即未通过WeakReference等引用的对象)(在这种情况下,它将抛出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) (其他张贴者建议使用数据库来存储您的值,但是对于仅200个项目,我认为您的方法没有问题-除非这些对象非常复杂/巨大)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM