8000 Use try / finally to clean up memory · wcork/arrayfire-java@52b7036 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52b7036

Browse files
committed
Use try / finally to clean up memory
Java does not play well with other memory managers
1 parent 4dc03f9 commit 52b7036

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

examples/MonteCarloPi.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,26 @@ public static double hostCalcPi(int size) {
1717
}
1818

1919
public static double deviceCalcPi(int size) throws Exception {
20-
int[] dims = new int[] {size, 1};
20+
Array x = null, y = null, res = null;
21+
try {
2122

22-
Array x = Array.randu(dims, Array.FloatType);
23-
Array y = Array.randu(dims, Array.FloatType);
23+
int[] dims = new int[] {size, 1};
24+
x = Array.randu(dims, Array.FloatType);
25+
y = Array.randu(dims, Array.FloatType);
2426

25-
Array x2 = Array.mul(x, x);
26-
Array y2 = Array.mul(y, y);
27-
Array res = Array.lt(Array.add(x2, y2), 1);
27+
x = Array.mul(x, x);
28+
y = Array.mul(y, y);
2829

29-
double count = Array.sumAll(res);
30-
return 4.0 * ((double)(count)) / size;
30+
res = Array.add(x , y);
31+
res = Array.lt(res, 1);
32+
double count = Array.sumAll(res);
33+
return 4.0 * ((double)(count)) / size;
34+
35+
} finally {
36+
if (x != null) x.close();
37+
if (y != null) y.close();
38+
if (res != null) res.close();
39+
}
3140
}
3241

3342
public static void main(String[] args) {

0 commit comments

Comments
 (0)
0