I have a very strange memory problem with my Android app.
My app use 3 classes that are the following:
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;
}
}
then somewere in the code I have to load 2000 colors from a file (file is about 65K lenght and has exactly 2000 records) and is placed in assets folder
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);
after this the app will crash and stop working. If I remove the do..while all is working, so I think my array will be more and more and more then 65K, what I have done wrong? On Android LogCat I have reached the full of HEAP space (26MB) !!!
Best regards
GMG
I don’t think that code is responsible of the
OutOfMemoryException. Maybe there are other fields you don’t mention, but without running the code one can’t tell.However there may be a small leak when you create the ID. Whenever you create a
Stringfrom an existing one (eithersubstring()-based or methods in the regex package), the returned string keeps an internal reference to the old one: it’s just a thin wrapper around the old sequence of characters, just with different start and different length. This means that you’d better create your ID like thisThis way you don’t keep the whole line in memory just to store a few characters.
However, this is an optimization, since you state that your file is 65KB, so even if you retain it all in-memory, it wouldn’t crash your application. Post the whole code so we can run and analyze it.
BTW, you can save an indentation level this way:
I also changed the API a bit (I found this more comfortable)