【发布时间】:2012-10-16 19:26:40
【问题描述】:
我的 Android 应用程序有一个非常奇怪的内存问题。 我的应用使用以下 3 个类:
public class RGB
{
public int R;
public int G;
public int B;
}
public class CMYK
{
public int C;
public int M;
public int Y;
public int K;
}
public class COLOR
{
public String id;
public CMYK cmyk = new CMYK();
public RGB rgb = new RGB();
public COLOR(String id, int c, int m, int y, int k, int r, int g, int b)
{
this.id = id;
this.cmyk.C = c;
this.cmyk.M = m;
this.cmyk.Y = y;
this.cmyk.K = k;
this.rgb.R = r;
this.rgb.G = g;
this.rgb.B = b;
}
}
然后在代码中,我必须从一个文件中加载 2000 种颜色(文件大约 65K 长,正好有 2000 条记录)并放在 assets 文件夹中
public COLOR[] color_list = new COLOR[2000];
...
...
do
{
s = reader.readLine();
if (s != null)
{
String[] x = s.split(" ");
COLOR c = new COLOR(x[0], Integer.parseInt(x[1]), Integer.parseInt(x[2]), Integer.parseInt(x[3]), Integer.parseInt(x[4]), Integer.parseInt(x[5]), Integer.parseInt(x[6]), Integer.parseInt(x[7]));
color_list[j++] = c;
}
} while (s != null);
在此之后,应用程序将崩溃并停止工作。如果我删除 do..while 一切正常,所以我认为我的阵列会越来越多,然后 65K,我做错了什么?在 Android LogCat 上,我的 HEAP 空间已满(26MB)!!!
最好的问候 GMG
【问题讨论】:
-
@JarrodSmith:他不是。数组的声明分配了一个数组,而不是 2000 个实际元素。
-
您可以使用分配跟踪器来确定谁在内存方面使用什么
-
如果您遇到崩溃,请提供堆栈跟踪。
-
问题是您没有为数组分配内存,您需要为 x[] 数组中的每个元素分配一个“新”。或者声明你的数组以便分配一些堆。这是错误的:String[] x = s.split(" ");
-
@TanjaV 这是错误的。 split 方法为您分配数组。那行代码没有错。
标签: java android arrays heap-memory android-memory