JVM out of memory (I want to solve this with out increasing the heap size)

public ThreeVec findVec(double[] pix_lab,double[][] graphic_labs){
ThreeVec result = new ThreeVec();
Vector<Double> diff = new Vector<Double>();
Vector<Double> tempdiff = new Vector<Double>();
double d1,d2,d3;
for(int i=0;i<graphic_labs.length;i++){
d1 = graphic_labs[0]-pix_lab[0];
d2 = graphic_labs[i][1]-pix_lab[1];
d3 = graphic_labs[i][2]-pix_lab[2];
diff.add(Math.sqrt((d1*d1)+(d2*d2)+(d3*d3)));
tempdiff.add(Math.sqrt((d1*d1)+(d2*d2)+(d3*d3)));
Collections.sort(tempdiff);
int[] vecIdx = new int[3];
vecIdx[0] = diff.indexOf(tempdiff.elementAt(0));
vecIdx[1] = diff.indexOf(tempdiff.elementAt(1));
vecIdx[2] = diff.indexOf(tempdiff.elementAt(2));
double[][] vecs = new double[3][3];
vecs[0][0] = (graphic_labs[vecIdx[0]][0]);
vecs[0][1] = (graphic_labs[vecIdx[0]][1]);
vecs[0][2] = (graphic_labs[vecIdx[0]][2]);
vecs[1][0] = (graphic_labs[vecIdx[1]][0]);
vecs[1][1] = (graphic_labs[vecIdx[1]][1]);
vecs[1][2] = (graphic_labs[vecIdx[1]][2]);
vecs[2][0] = (graphic_labs[vecIdx[2]][0]);
vecs[2][1] = (graphic_labs[vecIdx[2]][1]);
vecs[2][2] = (graphic_labs[vecIdx[2]][2]);
Matrix vec_matrix = new Matrix(vecs);
Matrix vec_mat_inv = vec_matrix.inverse();
Matrix pix_matrix = new Matrix(1,3);
pix_matrix.set(0,0,pix_lab[0]);
pix_matrix.set(0,1,pix_lab[1]);
pix_matrix.set(0,2,pix_lab[2]);
Matrix abc = pix_matrix.times(vec_mat_inv);
result.setVecIdx(vecIdx);
result.setABC(abc);
return result;
Matrix is the class defined in JAMA library.
I am getting OutOfMemoryError in this method.
Let me explain. The above method is called from a nested 'for' loop something like the pseudocode below.
BufferedImage in_graphic = ImageIO.read(graphic);
for(int j=0;j<in_graphic.getHeight();j++){
for(int i=0;i<in_graphic.getWidth();i++){
findVec(param1,param2);
lengths of param1 and param2 are fixed
param1's length is always 3
param2.length is 8 and param2[i] is 3
I thought that this code is independent of image size but it not. As the image size increases beyond a certain size I am getting OutOfMemoryError. I dont know why am I doing something wrong here. Thanks in advance.

I know you asked about a memory issue, not a speed issue. But, another slight speed-up would be to compute the square root once, and then put it in both lists:
diff.add(Math.sqrt((d1*d1)+(d2*d2)+(d3*d3)));
tempdiff.add(Math.sqrt((d1*d1)+(d2*d2)+(d3*d3)));could be:
Double distance = Double.valueOf(Math.sqrt((d1*d1)+(d2*d2)+(d3*d3)));
diff.add(distance);
tempdiff.add(distance);That not only speeds it up (fewer calculations), but it creates half as many Double objects as your way was creating.
Autoboxing is slowing you down, too. (The use of Double and the autoboxing will go away if you use ejp's suggestion to use arrays of double. But, you could still compute that value only once.)
Your Vector<Double> are local variables, anyway. If I'm reading your code correctly, they are each only 8 elements long. If so, that's not the main memory issue. Using an array of double primitives will use a little less memory and will be faster, but I don't think the Vector<Double> is the biggest issue. You aren't storing the references to those Vectors anywhere, so they will be eligible for garbage collection at the end of the method.
So, I know you said it was pseudocode, but your pseudocode as to how findVec was called didn't even use the return value (the ThreeVec that the method creates). Maybe if you explain better what param1 and param2 are (how they are calculated based on height and width, or other useful information), someone could give you a better suggestion. Also, what's a ThreeVec, anyway?
Maybe try posting the actual code for your loop, instead of pseudocode. Use the CODE button (above the posting box) to format your code nicely.

Similar Messages

Maybe you are looking for