I wanna calculate Euclidean distances between each pairs of elements in a two dimensional array list in JAVA. this two dimensional array list consists of 40000 records in 40 dimensions. I encountered a memory deficiency problem:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
I increased the heap-memory size to: Xmx16000M (16Gb RAM). but the problem also exists. so, how can I get rid of out of memory problem? In the following you can see the pseudocode that exactly describe my code. Thank you all of the respondents.
ArrayList<ArrayList<Double> dataset = new ArrayList<ArrayList<Double>>();
dataset = readDataset(); // a method returns data to my 2-d arraylist
//now I have 40000 records in 40 dim in dataset!
distanceMatrix = new double[dataset.size()][dataset.size()];
for (int i=0 ; i<dataset.size(); i++) {
for (int j=0 ; j<(dataset.size()-i); j++) {
if (i == j) {
distanceMatrix[i][j] = 0.0;
continue;
}
double ans= getDistance(dataset.get(i), dataset.get(j));
distanceMatrix[i][j] = ans;
distanceMatrix[j][i] = ans;
}
}
public double getDistance(ArrayList<Double> a , ArrayList<Double> b) {
double dist=0;
for (int i = 0; i < 40; i++) {
double c = Math.abs(a.get(i) - b.get(i));
dist += Math.pow(c, 2);
}
dist = Math.sqrt(dist);
return dist;
}