Skip to content

Commit 8392ea7

Browse files
authored
Add regression test for memory leaks in string tensors (#266)
1 parent bd0f779 commit 8392ea7

File tree

1 file changed

+27
-0
lines changed
  • tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types

1 file changed

+27
-0
lines changed

tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TStringTest.java

+27
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
2324

2425
import java.nio.charset.StandardCharsets;
26+
import org.bytedeco.javacpp.Pointer;
2527
import org.junit.jupiter.api.Test;
2628
import org.tensorflow.ndarray.NdArray;
2729
import org.tensorflow.ndarray.NdArrays;
@@ -103,5 +105,30 @@ public void initializingTensorWithRawBytes() {
103105
}
104106
}
105107

108+
@Test
109+
public void testNoLeaks() throws Exception {
110+
System.gc();
111+
Thread.sleep(100);
112+
113+
for (int i = 0; i < 1000; i++) {
114+
TString.scalarOf(A_LARGE_STRING).close();
115+
}
116+
117+
System.gc();
118+
Thread.sleep(100);
119+
long bytesBefore = Pointer.physicalBytes();
120+
121+
for (int i = 0; i < 1000; i++) {
122+
TString.scalarOf(A_LARGE_STRING).close();
123+
}
124+
125+
System.gc();
126+
Thread.sleep(100);
127+
long bytesAfter = Pointer.physicalBytes();
128+
129+
assertTrue(Math.abs(bytesAfter - bytesBefore) < 10_000_000);
130+
}
131+
132+
private static final String A_LARGE_STRING = new String(new byte[1_000_000]);
106133
private static final String BABY_CHICK = "\uD83D\uDC25";
107134
}

0 commit comments

Comments
 (0)