Skip to content

Commit 929f908

Browse files
committed
GH-4554 Close models without relying on finalize.
1 parent 9863514 commit 929f908

File tree

5 files changed

+87
-44
lines changed

5 files changed

+87
-44
lines changed

core/sail/base/src/main/java/org/eclipse/rdf4j/sail/base/Changeset.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,21 @@ public void close() throws SailException {
127127
refbacks = null;
128128
prepend = null;
129129
observed = null;
130+
if (approved instanceof AutoCloseable) {
131+
try {
132+
((AutoCloseable) approved).close();
133+
} catch (Exception e) {
134+
throw new SailException(e);
135+
}
136+
}
130137
approved = null;
138+
if (deprecated instanceof AutoCloseable) {
139+
try {
140+
((AutoCloseable) deprecated).close();
141+
} catch (Exception e) {
142+
throw new SailException(e);
143+
}
144+
}
131145
deprecated = null;
132146
approvedContexts = null;
133147
deprecatedContexts = null;

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/MemoryOverflowModel.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*******************************************************************************/
1111
package org.eclipse.rdf4j.sail.lmdb;
1212

13+
import java.io.Closeable;
1314
import java.io.File;
1415
import java.io.IOException;
1516
import java.io.ObjectInputStream;
@@ -40,7 +41,7 @@
4041
* estimated memory usage is more than the amount of free memory available. Once the threshold is cross this
4142
* implementation seamlessly changes to a disk based {@link SailSourceModel}.
4243
*/
43-
abstract class MemoryOverflowModel extends AbstractModel {
44+
abstract class MemoryOverflowModel extends AbstractModel implements AutoCloseable {
4445

4546
private static final long serialVersionUID = 4119844228099208169L;
4647

@@ -246,7 +247,7 @@ private synchronized void checkMemoryOverflow() {
246247
}
247248

248249
// Sync if either the estimated size of the next block is larger than remaining memory, or
249-
// if less than 15% of the heap is still free (this last condition to avoid GC overhead limit)
250+
// if less than 25% of the heap is still free (this last condition to avoid GC overhead limit)
250251
if (freeToAllocateMemory < MIN_AVAILABLE_MEM_BEFORE_OVERFLOWING ||
251252
freeToAllocateMemory < Math.max(0.25 * maxMemory, maxBlockSize)) {
252253
logger.debug("syncing at {} triples. max block size: {}", size, maxBlockSize);
@@ -264,26 +265,7 @@ private synchronized void overflowToDisk() {
264265
dataDir = Files.createTempDirectory("model").toFile();
265266
logger.debug("memory overflow using temp directory {}", dataDir);
266267
store = createSailStore(dataDir);
267-
disk = new SailSourceModel(store, verifyAdditions) {
268-
269-
@Override
270-
protected void finalize() throws Throwable {
271-
logger.debug("finalizing {}", dataDir);
272-
if (disk == this) {
273-
try {
274-
store.close();
275-
} catch (SailException e) {
276-
logger.error(e.toString(), e);
277-
} finally {
278-
FileUtil.deleteDir(dataDir);
279-
dataDir = null;
280-
store = null;
281-
disk = null;
282-
}
283-
}
284-
super.finalize();
285-
}
286-
};
268+
disk = new SailSourceModel(store, verifyAdditions);
287269
disk.addAll(memory);
288270
memory = new LinkedHashModel(memory.getNamespaces(), LARGE_BLOCK);
289271
logger.debug("overflow synced to disk");
@@ -292,4 +274,22 @@ protected void finalize() throws Throwable {
292274
logger.error("Error while writing to overflow directory " + path, e);
293275
}
294276
}
277+
278+
@Override
279+
public void close() throws IOException {
280+
if (disk != null) {
281+
logger.debug("closing {}", dataDir);
282+
disk.close();
283+
try {
284+
store.close();
285+
} catch (SailException e) {
286+
logger.error(e.toString(), e);
287+
} finally {
288+
FileUtil.deleteDir(dataDir);
289+
dataDir = null;
290+
store = null;
291+
disk = null;
292+
}
293+
}
294+
}
295295
}

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/SailSourceModel.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,21 @@ private synchronized SailDataset dataset() throws SailException {
397397
return dataset;
398398
}
399399

400+
public void close() {
401+
if (sink != null) {
402+
try {
403+
sink.flush();
404+
} finally {
405+
sink.close();
406+
sink = null;
407+
}
408+
}
409+
if (dataset != null) {
410+
dataset.close();
411+
dataset = null;
412+
}
413+
}
414+
400415
private boolean contains(SailDataset dataset, Resource subj, IRI pred, Value obj, Resource... contexts)
401416
throws SailException {
402417
if (dataset == null) {

core/sail/nativerdf/src/main/java/org/eclipse/rdf4j/sail/nativerdf/MemoryOverflowModel.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* estimated memory usage is more than the amount of free memory available. Once the threshold is cross this
4141
* implementation seamlessly changes to a disk based {@link SailSourceModel}.
4242
*/
43-
abstract class MemoryOverflowModel extends AbstractModel {
43+
abstract class MemoryOverflowModel extends AbstractModel implements AutoCloseable {
4444

4545
private static final long serialVersionUID = 4119844228099208169L;
4646

@@ -246,7 +246,7 @@ private synchronized void checkMemoryOverflow() {
246246
}
247247

248248
// Sync if either the estimated size of the next block is larger than remaining memory, or
249-
// if less than 15% of the heap is still free (this last condition to avoid GC overhead limit)
249+
// if less than 25% of the heap is still free (this last condition to avoid GC overhead limit)
250250
if (freeToAllocateMemory < MIN_AVAILABLE_MEM_BEFORE_OVERFLOWING ||
251251
freeToAllocateMemory < Math.max(0.25 * maxMemory, maxBlockSize)) {
252252
logger.debug("syncing at {} triples. max block size: {}", size, maxBlockSize);
@@ -264,26 +264,7 @@ private synchronized void overflowToDisk() {
264264
dataDir = Files.createTempDirectory("model").toFile();
265265
logger.debug("memory overflow using temp directory {}", dataDir);
266266
store = createSailStore(dataDir);
267-
disk = new SailSourceModel(store, verifyAdditions) {
268-
269-
@Override
270-
protected void finalize() throws Throwable {
271-
logger.debug("finalizing {}", dataDir);
272-
if (disk == this) {
273-
try {
274-
store.close();
275-
} catch (SailException e) {
276-
logger.error(e.toString(), e);
277-
} finally {
278-
FileUtil.deleteDir(dataDir);
279-
dataDir = null;
280-
store = null;
281-
disk = null;
282-
}
283-
}
284-
super.finalize();
285-
}
286-
};
267+
disk = new SailSourceModel(store, verifyAdditions);
287268
disk.addAll(memory);
288269
memory = new LinkedHashModel(memory.getNamespaces(), LARGE_BLOCK);
289270
logger.debug("overflow synced to disk");
@@ -292,4 +273,22 @@ protected void finalize() throws Throwable {
292273
logger.error("Error while writing to overflow directory " + path, e);
293274
}
294275
}
276+
277+
@Override
278+
public void close() throws IOException {
279+
if (disk != null) {
280+
logger.debug("closing {}", dataDir);
281+
disk.close();
282+
try {
283+
store.close();
284+
} catch (SailException e) {
285+
logger.error(e.toString(), e);
286+
} finally {
287+
FileUtil.deleteDir(dataDir);
288+
dataDir = null;
289+
store = null;
290+
disk = null;
291+
}
292+
}
293+
}
295294
}

core/sail/nativerdf/src/main/java/org/eclipse/rdf4j/sail/nativerdf/SailSourceModel.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,21 @@ private synchronized SailDataset dataset() throws SailException {
397397
return dataset;
398398
}
399399

400+
public void close() {
401+
if (sink != null) {
402+
try {
403+
sink.flush();
404+
} finally {
405+
sink.close();
406+
sink = null;
407+
}
408+
}
409+
if (dataset != null) {
410+
dataset.close();
411+
dataset = null;
412+
}
413+
}
414+
400415
private boolean contains(SailDataset dataset, Resource subj, IRI pred, Value obj, Resource... contexts)
401416
throws SailException {
402417
if (dataset == null) {

0 commit comments

Comments
 (0)