Skip to content

Commit e0e4545

Browse files
authored
SavedModelBundle leak fix (#335) (#350)
1 parent e786c78 commit e0e4545

File tree

13 files changed

+73
-58
lines changed

13 files changed

+73
-58
lines changed

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ systems, you should add the following dependencies:
6161
<dependency>
6262
<groupId>org.tensorflow</groupId>
6363
<artifactId>tensorflow-core-api</artifactId>
64-
<version>0.3.1</version>
64+
<version>0.3.2</version>
6565
</dependency>
6666
<dependency>
6767
<groupId>org.tensorflow</groupId>
6868
<artifactId>tensorflow-core-api</artifactId>
69-
<version>0.3.1</version>
69+
<version>0.3.2</version>
7070
<classifier>linux-x86_64${javacpp.platform.extension}</classifier>
7171
</dependency>
7272
```
@@ -77,24 +77,24 @@ native dependencies as follows:
7777
<dependency>
7878
<groupId>org.tensorflow</groupId>
7979
<artifactId>tensorflow-core-api</artifactId>
80-
<version>0.3.1</version>
80+
<version>0.3.2</version>
8181
</dependency>
8282
<dependency>
8383
<groupId>org.tensorflow</groupId>
8484
<artifactId>tensorflow-core-api</artifactId>
85-
<version>0.3.1</version>
85+
<version>0.3.2</version>
8686
<classifier>linux-x86_64${javacpp.platform.extension}</classifier>
8787
</dependency>
8888
<dependency>
8989
<groupId>org.tensorflow</groupId>
9090
<artifactId>tensorflow-core-api</artifactId>
91-
<version>0.3.1</version>
91+
<version>0.3.2</version>
9292
<classifier>macosx-x86_64${javacpp.platform.extension}</classifier>
9393
</dependency>
9494
<dependency>
9595
<groupId>org.tensorflow</groupId>
9696
<artifactId>tensorflow-core-api</artifactId>
97-
<version>0.3.1</version>
97+
<version>0.3.2</version>
9898
<classifier>windows-x86_64${javacpp.platform.extension}</classifier>
9999
</dependency>
100100
```
@@ -107,7 +107,7 @@ artifact includes transitively all the artifacts above as a single dependency:
107107
<dependency>
108108
<groupId>org.tensorflow</groupId>
109109
<artifactId>tensorflow-core-platform${javacpp.platform.extension}</artifactId>
110-
<version>0.3.1</version>
110+
<version>0.3.2</version>
111111
</dependency>
112112
```
113113

@@ -137,7 +137,7 @@ to add Sonatype OSS repository in your pom.xml, like the following
137137
<dependency>
138138
<groupId>org.tensorflow</groupId>
139139
<artifactId>tensorflow-core-platform</artifactId>
140-
<version>0.3.0-SNAPSHOT</version>
140+
<version>0.4.0-SNAPSHOT</version>
141141
</dependency>
142142
</dependencies>
143143
```
@@ -151,6 +151,7 @@ This table shows the mapping between different version of TensorFlow for Java an
151151
| 0.2.0 | 2.3.1 |
152152
| 0.3.0 | 2.4.1 |
153153
| 0.3.1 | 2.4.1 |
154+
| 0.3.2 | 2.4.1 |
154155

155156
## How to Contribute?
156157

ndarray/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>org.tensorflow</groupId>
2424
<artifactId>tensorflow-java</artifactId>
25-
<version>0.3.1</version>
25+
<version>0.3.2</version>
2626
</parent>
2727
<artifactId>ndarray</artifactId>
2828
<packaging>jar</packaging>

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>org.tensorflow</groupId>
77
<artifactId>tensorflow-java</artifactId>
8-
<version>0.3.1</version>
8+
<version>0.3.2</version>
99
<packaging>pom</packaging>
1010

1111
<name>TensorFlow Java Parent</name>

tensorflow-core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>org.tensorflow</groupId>
2424
<artifactId>tensorflow-java</artifactId>
25-
<version>0.3.1</version>
25+
<version>0.3.2</version>
2626
</parent>
2727
<artifactId>tensorflow-core</artifactId>
2828
<packaging>pom</packaging>

tensorflow-core/tensorflow-core-api/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.tensorflow</groupId>
88
<artifactId>tensorflow-core</artifactId>
9-
<version>0.3.1</version>
9+
<version>0.3.2</version>
1010
</parent>
1111
<artifactId>tensorflow-core-api</artifactId>
1212
<packaging>jar</packaging>

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License.
14-
==============================================================================*/
15-
14+
=======================================================================
15+
*/
1616
package org.tensorflow;
1717

18-
import static org.tensorflow.internal.c_api.global.tensorflow.TF_LoadSessionFromSavedModel;
1918
import static org.tensorflow.internal.c_api.global.tensorflow.TF_NewGraph;
2019
import static org.tensorflow.internal.c_api.global.tensorflow.TF_SetConfig;
2120

@@ -32,7 +31,6 @@
3231
import java.util.Map;
3332
import java.util.stream.Collectors;
3433
import org.bytedeco.javacpp.BytePointer;
35-
import org.bytedeco.javacpp.PointerPointer;
3634
import org.bytedeco.javacpp.PointerScope;
3735
import org.tensorflow.exceptions.TensorFlowException;
3836
import org.tensorflow.internal.c_api.TF_Buffer;
@@ -412,14 +410,19 @@ private static SavedModelBundle load(
412410
// load the session
413411
TF_Graph graph = TF_NewGraph();
414412
TF_Buffer metagraphDef = TF_Buffer.newBuffer();
415-
TF_Session session = TF_LoadSessionFromSavedModel(
416-
opts, runOpts, new BytePointer(exportDir), new PointerPointer(tags),
417-
tags.length, graph, metagraphDef, status);
413+
TF_Session session =
414+
TF_Session.loadSessionFromSavedModel(
415+
opts, runOpts, exportDir, tags, graph, metagraphDef, status);
418416
status.throwExceptionIfNotOK();
419417

420418
// handle the result
421419
try {
422-
bundle = fromHandle(graph, session, MetaGraphDef.parseFrom(metagraphDef.dataAsByteBuffer()));
420+
bundle =
421+
fromHandle(graph, session, MetaGraphDef.parseFrom(metagraphDef.dataAsByteBuffer()));
422+
// Only retain the references if the metagraphdef parses correctly,
423+
// otherwise allow the pointer scope to clean them up
424+
graph.retainReference();
425+
session.retainReference();
423426
} catch (InvalidProtocolBufferException e) {
424427
throw new TensorFlowException("Cannot parse MetaGraphDef protocol buffer", e);
425428
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/*
2-
Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2+
Copyright 2019 The TensorFlow Authors. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
=======================================================================
16-
*/
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
=======================================================================
16+
*/
1717

1818
package org.tensorflow.internal.c_api;
1919

@@ -25,29 +25,40 @@
2525

2626
@Properties(inherit = org.tensorflow.internal.c_api.presets.tensorflow.class)
2727
public abstract class AbstractTF_Graph extends Pointer {
28-
protected static class DeleteDeallocator extends TF_Graph implements Pointer.Deallocator {
29-
DeleteDeallocator(TF_Graph s) { super(s); }
30-
@Override public void deallocate() { if (!isNull()) TF_DeleteGraph(this); setNull(); }
28+
protected static class DeleteDeallocator extends TF_Graph implements Pointer.Deallocator {
29+
DeleteDeallocator(TF_Graph s) {
30+
super(s);
3131
}
3232

33-
public AbstractTF_Graph(Pointer p) { super(p); }
34-
35-
/**
36-
* Calls TF_NewGraph(), and registers a deallocator.
37-
* @return TF_Graph created. Do not call TF_DeleteGraph() on it.
38-
*/
39-
public static TF_Graph newGraph() {
40-
TF_Graph g = TF_NewGraph();
41-
if (g != null) {
42-
g.deallocator(new DeleteDeallocator(g));
43-
}
44-
return g;
33+
@Override
34+
public void deallocate() {
35+
if (!isNull()) TF_DeleteGraph(this);
36+
setNull();
4537
}
38+
}
4639

47-
/**
48-
* Calls the deallocator, if registered, otherwise has no effect.
49-
*/
50-
public void delete() {
51-
deallocate();
40+
public AbstractTF_Graph(Pointer p) {
41+
super(p);
42+
}
43+
44+
/**
45+
* Calls TF_NewGraph(), and registers a deallocator.
46+
*
47+
* <p>Note {@link org.tensorflow.Graph} will call TF_DeleteGraph on close, so do not use this
48+
* method when constructing a reference for use inside a {@code Graph} object.
49+
*
50+
* @return TF_Graph created. Do not call TF_DeleteGraph() on it.
51+
*/
52+
public static TF_Graph newGraph() {
53+
TF_Graph g = TF_NewGraph();
54+
if (g != null) {
55+
g.deallocator(new DeleteDeallocator(g));
5256
}
57+
return g;
58+
}
59+
60+
/** Calls the deallocator, if registered, otherwise has no effect. */
61+
public void delete() {
62+
deallocate();
63+
}
5364
}

tensorflow-core/tensorflow-core-generator/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.tensorflow</groupId>
77
<artifactId>tensorflow-core</artifactId>
8-
<version>0.3.1</version>
8+
<version>0.3.2</version>
99
</parent>
1010
<artifactId>tensorflow-core-generator</artifactId>
1111
<packaging>jar</packaging>

tensorflow-core/tensorflow-core-platform-gpu/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>org.tensorflow</groupId>
2424
<artifactId>tensorflow-core</artifactId>
25-
<version>0.3.1</version>
25+
<version>0.3.2</version>
2626
</parent>
2727
<artifactId>tensorflow-core-platform-gpu</artifactId>
2828
<name>TensorFlow Core API Library Platform GPU</name>

tensorflow-core/tensorflow-core-platform-mkl-gpu/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>org.tensorflow</groupId>
2424
<artifactId>tensorflow-core</artifactId>
25-
<version>0.3.1</version>
25+
<version>0.3.2</version>
2626
</parent>
2727
<artifactId>tensorflow-core-platform-mkl-gpu</artifactId>
2828
<name>TensorFlow Core API Library Platform MKL GPU</name>

tensorflow-core/tensorflow-core-platform-mkl/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>org.tensorflow</groupId>
2424
<artifactId>tensorflow-core</artifactId>
25-
<version>0.3.1</version>
25+
<version>0.3.2</version>
2626
</parent>
2727
<artifactId>tensorflow-core-platform-mkl</artifactId>
2828
<name>TensorFlow Core API Library Platform MKL</name>

tensorflow-core/tensorflow-core-platform/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>org.tensorflow</groupId>
2424
<artifactId>tensorflow-core</artifactId>
25-
<version>0.3.1</version>
25+
<version>0.3.2</version>
2626
</parent>
2727
<artifactId>tensorflow-core-platform</artifactId>
2828
<name>TensorFlow Core API Library Platform</name>

tensorflow-framework/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>org.tensorflow</groupId>
2424
<artifactId>tensorflow-java</artifactId>
25-
<version>0.3.1</version>
25+
<version>0.3.2</version>
2626
</parent>
2727
<artifactId>tensorflow-framework</artifactId>
2828
<packaging>jar</packaging>

0 commit comments

Comments
 (0)