Block Array or multidimensional array Variables. For some reason my app is throwing out of memory on my loading of my pub vars class. This started happening when I started using Block Arrays. It works on actual devices but not on the emulators. Any thoughts? Should I set my block arrays to actual needs?
块阵列或多维数组变量。出于某种原因,我的应用程序在我的pub vars类加载时丢失了内存。当我开始使用Block Arrays时,这种情况就开始发生了。它适用于实际设备,但不适用于仿真器。有什么想法吗?我应该将块数组设置为实际需要吗?
Current Block Arrays:
当前块数组:
public static String[][] Name = new String[1000][1000];
however I only use maybe 10 or so. Is the device allocating space for the potential of the 1000 and is that why it is errors out? If so, how can I use these as the need may grow and I do not want to put a small limit on it. Thanks in advance for any thoughts.
但是我只用了10个左右。设备是否为1000的潜力分配空间,这就是它出错的原因吗?如果是这样,我如何使用这些,因为需求可能会增长,我不想对它施加一个小的限制。提前感谢任何想法。
1 个解决方案
#1
2
With
public static String[][] Name = new String[1000][1000];
you are allocating 1 million strings (1000x1000) which is quite a bunch. If the information on this page is correct each string at least takes 40 bytes, so that would be around 39 Mbytes in your case and this can easily be too much memory on the heap for 1 activity. I would start there to refactor if you are only using 10. There is probably a better solution than your approach but without any more details on your code it's hard to give them. But of the top of my head, why not use a Set<String>
or List<String>
?
你正在分配100万个字符串(1000x1000),这是相当多的。如果此页面上的信息是正确的,则每个字符串至少需要40个字节,因此在您的情况下大约为39 MB,这对于1个活动来说很容易在堆上占用太多内存。如果你只使用10,我会从那里开始重构。可能有一个比你的方法更好的解决方案,但是没有关于你的代码的更多细节,很难给出它们。但在我的头脑中,为什么不使用Set
Edit: So it's seems to me that you just want a Collection that scales dynamically. For that array is not the best choice. There are many of datatypes for that but one simple example whould be an ArrayList
which also uses a array as backing datatype but by default will be instanciated with a capacity of 10 and expands dynamically if you continue to add elements
编辑:所以在我看来,你只想要一个动态扩展的Collection。因为那个阵列不是最好的选择。有许多数据类型,但是一个简单的例子可能是一个ArrayList,它也使用数组作为后备数据类型,但默认情况下将以10的容量进行实例化,并在继续添加元素时动态扩展
List<String> stringList = new ArrayList<String>();
stringList.add("string1");
stringList.add("string2");
...
If you want each element to have its own list of strings just create an object for that:
如果您希望每个元素都有自己的字符串列表,只需为其创建一个对象:
public class CompoundString {
private String key;
private List<String> stringList;
...
}
and use it like this
并像这样使用它
List<CompoundString> compoundStringList = new ArrayList<CompoundString>();
compoundStringList.add(new CompoundString("string1", new ArrayList<String>());
or just use a map:
或者只是使用地图:
Map<String,List<String>> stringMap = new HashMap<String,List<String>>();
stringMap.put("string1", new ArrayList<String>());
This is pretty basic concept in most programming languages and I would start to read some docs about the various collections:
这是大多数编程语言中非常基本的概念,我将开始阅读有关各种集合的一些文档:
#1
2
With
public static String[][] Name = new String[1000][1000];
you are allocating 1 million strings (1000x1000) which is quite a bunch. If the information on this page is correct each string at least takes 40 bytes, so that would be around 39 Mbytes in your case and this can easily be too much memory on the heap for 1 activity. I would start there to refactor if you are only using 10. There is probably a better solution than your approach but without any more details on your code it's hard to give them. But of the top of my head, why not use a Set<String>
or List<String>
?
你正在分配100万个字符串(1000x1000),这是相当多的。如果此页面上的信息是正确的,则每个字符串至少需要40个字节,因此在您的情况下大约为39 MB,这对于1个活动来说很容易在堆上占用太多内存。如果你只使用10,我会从那里开始重构。可能有一个比你的方法更好的解决方案,但是没有关于你的代码的更多细节,很难给出它们。但在我的头脑中,为什么不使用Set
Edit: So it's seems to me that you just want a Collection that scales dynamically. For that array is not the best choice. There are many of datatypes for that but one simple example whould be an ArrayList
which also uses a array as backing datatype but by default will be instanciated with a capacity of 10 and expands dynamically if you continue to add elements
编辑:所以在我看来,你只想要一个动态扩展的Collection。因为那个阵列不是最好的选择。有许多数据类型,但是一个简单的例子可能是一个ArrayList,它也使用数组作为后备数据类型,但默认情况下将以10的容量进行实例化,并在继续添加元素时动态扩展
List<String> stringList = new ArrayList<String>();
stringList.add("string1");
stringList.add("string2");
...
If you want each element to have its own list of strings just create an object for that:
如果您希望每个元素都有自己的字符串列表,只需为其创建一个对象:
public class CompoundString {
private String key;
private List<String> stringList;
...
}
and use it like this
并像这样使用它
List<CompoundString> compoundStringList = new ArrayList<CompoundString>();
compoundStringList.add(new CompoundString("string1", new ArrayList<String>());
or just use a map:
或者只是使用地图:
Map<String,List<String>> stringMap = new HashMap<String,List<String>>();
stringMap.put("string1", new ArrayList<String>());
This is pretty basic concept in most programming languages and I would start to read some docs about the various collections:
这是大多数编程语言中非常基本的概念,我将开始阅读有关各种集合的一些文档: