Skip to content

Commit d0434de

Browse files
committed
Fix memory leak occuring with new layout of string tensors
Also fix and actually use JavaCPP deallocator for TF_Session as well
1 parent a905d07 commit d0434de

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Session.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -604,23 +604,16 @@ private static TF_Session allocate(TF_Graph graphHandle, String target, ConfigPr
604604
status.throwExceptionIfNotOK();
605605
}
606606

607-
TF_Session session = TF_NewSession(graphHandle, opts, status);
607+
TF_Session session = TF_Session.newSession(graphHandle, opts, status);
608608
status.throwExceptionIfNotOK();
609609

610-
return session;
610+
return session.retainReference();
611611
}
612612
}
613613

614614
private static void delete(TF_Session handle) {
615615
requireHandle(handle);
616-
617-
try (PointerScope scope = new PointerScope()) {
618-
TF_Status status = TF_Status.newStatus();
619-
TF_CloseSession(handle, status);
620-
// Result of close is ignored, delete anyway.
621-
TF_DeleteSession(handle, status);
622-
status.throwExceptionIfNotOK();
623-
}
616+
handle.releaseReference();
624617
}
625618

626619
/**

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/internal/buffer/ByteSequenceTensorBuffer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import static org.tensorflow.internal.c_api.global.tensorflow.TF_TString_Assign;
2121
import static org.tensorflow.internal.c_api.global.tensorflow.TF_TString_Copy;
22-
import static org.tensorflow.internal.c_api.global.tensorflow.TF_TString_Init;
2322
import static org.tensorflow.internal.c_api.global.tensorflow.TF_TString_GetDataPointer;
2423
import static org.tensorflow.internal.c_api.global.tensorflow.TF_TString_GetSize;
2524

@@ -127,7 +126,6 @@ private class InitDataWriter {
127126
void writeNext(byte[] bytes) {
128127
try (PointerScope scope = new PointerScope()) {
129128
TF_TString tstring = data.getPointer(index++);
130-
TF_TString_Init(tstring);
131129
TF_TString_Copy(tstring, new BytePointer(bytes), bytes.length);
132130
}
133131
}

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/internal/c_api/AbstractTF_Session.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.bytedeco.javacpp.BytePointer;
2626
import org.bytedeco.javacpp.Pointer;
2727
import org.bytedeco.javacpp.PointerPointer;
28+
import org.bytedeco.javacpp.PointerScope;
2829
import org.bytedeco.javacpp.annotation.Properties;
2930

3031
@Properties(inherit = org.tensorflow.internal.c_api.presets.tensorflow.class)
@@ -33,11 +34,14 @@ protected static class DeleteDeallocator extends TF_Session implements Pointer.D
3334
DeleteDeallocator(TF_Session s) { super(s); }
3435
@Override public void deallocate() {
3536
if (!isNull()) {
36-
TF_Status status = TF_Status.newStatus();
37-
TF_CloseSession(this, status);
38-
// Result of close is ignored, delete anyway.
39-
TF_DeleteSession(this, status);
40-
setNull();
37+
try (PointerScope scope = new PointerScope()) {
38+
TF_Status status = TF_Status.newStatus();
39+
TF_CloseSession(this, status);
40+
// Result of close is ignored, delete anyway.
41+
TF_DeleteSession(this, status);
42+
status.throwExceptionIfNotOK();
43+
setNull();
44+
}
4145
}
4246
}
4347
}

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/internal/c_api/AbstractTF_Tensor.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
import static org.tensorflow.internal.c_api.global.tensorflow.TF_AllocateTensor;
2121
import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteTensor;
2222
import static org.tensorflow.internal.c_api.global.tensorflow.TF_NewTensor;
23+
import static org.tensorflow.internal.c_api.global.tensorflow.TF_STRING;
24+
import static org.tensorflow.internal.c_api.global.tensorflow.TF_TString_Dealloc;
25+
import static org.tensorflow.internal.c_api.global.tensorflow.TF_TString_Init;
26+
import static org.tensorflow.internal.c_api.global.tensorflow.TF_TensorData;
27+
import static org.tensorflow.internal.c_api.global.tensorflow.TF_TensorElementCount;
28+
import static org.tensorflow.internal.c_api.global.tensorflow.TF_TensorType;
2329

2430
import org.bytedeco.javacpp.Pointer;
2531
import org.bytedeco.javacpp.annotation.Properties;
@@ -28,7 +34,20 @@
2834
public abstract class AbstractTF_Tensor extends Pointer {
2935
protected static class DeleteDeallocator extends TF_Tensor implements Pointer.Deallocator {
3036
DeleteDeallocator(TF_Tensor s) { super(s); }
31-
@Override public void deallocate() { if (!isNull()) TF_DeleteTensor(this); setNull(); }
37+
@Override public void deallocate() {
38+
if (!isNull()) {
39+
if (TF_TensorType(this) == TF_STRING) {
40+
// we need to deallocate the strings themselves before deallocating the tensor memory
41+
long n = TF_TensorElementCount(this);
42+
TF_TString data = new TF_TString(TF_TensorData(this));
43+
for (int i = 0; i < n; i++) {
44+
TF_TString_Dealloc(data.position(i));
45+
}
46+
}
47+
TF_DeleteTensor(this);
48+
}
49+
setNull();
50+
}
3251
}
3352

3453
/** TensorFlow crashes if we don't pass it a deallocator, so... */
@@ -61,6 +80,14 @@ public static TF_Tensor newTensor(int dtype, long[] dims, Pointer data) {
6180
public static TF_Tensor allocateTensor(int dtype, long[] dims, long length) {
6281
TF_Tensor t = TF_AllocateTensor(dtype, dims, dims.length, length);
6382
if (t != null) {
83+
if (TF_TensorType(t) == TF_STRING) {
84+
// we need to initialize the strings themselves after allocating the tensor memory
85+
long n = TF_TensorElementCount(t);
86+
TF_TString data = new TF_TString(TF_TensorData(t));
87+
for (int i = 0; i < n; i++) {
88+
TF_TString_Init(data.position(i));
89+
}
90+
}
6491
t.deallocator(new DeleteDeallocator(t));
6592
}
6693
return t;

0 commit comments

Comments
 (0)