Skip to content

Commit de03f66

Browse files
committed
Avoid OOM by not loading entire JAR into RAM for env loading
1 parent de59036 commit de03f66

File tree

8 files changed

+19
-12
lines changed

8 files changed

+19
-12
lines changed

API/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<parent>
3232
<groupId>org.ohnlp.backbone</groupId>
3333
<artifactId>backbone-parent</artifactId>
34-
<version>3.0.30</version>
34+
<version>3.0.31</version>
3535
</parent>
3636

3737
<artifactId>api</artifactId>

API/src/main/java/org/ohnlp/backbone/api/components/xlang/python/PythonBridge.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ private void extractPythonResourcesIfNotExists() {
129129
}
130130
if (!CLEANUP_ENVS_ON_SHUTDOWN) {
131131
boolean reinit = false;
132-
try {
133-
InputStream bundleIS = findBundle();
132+
try (InputStream bundleIS = findBundle()) {
134133
byte[] data = bundleIS.readAllBytes();
135134
String currModuleChecksum = new BigInteger(1, MessageDigest.getInstance("MD5").digest(data)).toString(16);
136135
if (currModuleChecksum.equals(cachedModuleChecksum)) {
@@ -203,9 +202,17 @@ private void extractPythonResourcesIfNotExists() {
203202
pathRelative = pathRelative.replaceAll("^" + File.separator + "?python_resources" + File.separator, "");
204203
File pathInTmp = new File(workDir, pathRelative);
205204
pathInTmp.getParentFile().mkdirs();
206-
byte[] contents = jar.getInputStream(entry).readAllBytes();
207-
try (FileOutputStream fos = new FileOutputStream(pathInTmp)) {
208-
fos.write(contents);
205+
try (InputStream inputStream = jar.getInputStream(entry);
206+
FileOutputStream fos = new FileOutputStream(pathInTmp)) {
207+
208+
// Define a buffer to transfer data in chunks
209+
byte[] buffer = new byte[8192]; // 8KB buffer
210+
int bytesRead;
211+
212+
// Copy data in chunks from the JAR entry input stream to the output file
213+
while ((bytesRead = inputStream.read(buffer)) != -1) {
214+
fos.write(buffer, 0, bytesRead);
215+
}
209216
fos.flush();
210217
}
211218
}

Core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.ohnlp.backbone</groupId>
99
<artifactId>backbone-parent</artifactId>
10-
<version>3.0.30</version>
10+
<version>3.0.31</version>
1111
</parent>
1212

1313
<artifactId>core</artifactId>

Example-Backbone-Configs/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.ohnlp.backbone</groupId>
99
<artifactId>backbone-parent</artifactId>
10-
<version>3.0.30</version>
10+
<version>3.0.31</version>
1111
</parent>
1212

1313
<artifactId>example-backbone-configs</artifactId>

IO/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.ohnlp.backbone</groupId>
99
<artifactId>backbone-parent</artifactId>
10-
<version>3.0.30</version>
10+
<version>3.0.31</version>
1111
</parent>
1212

1313
<groupId>org.ohnlp.backbone.io</groupId>

Plugin-Manager/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.ohnlp.backbone</groupId>
99
<artifactId>backbone-parent</artifactId>
10-
<version>3.0.30</version>
10+
<version>3.0.31</version>
1111
</parent>
1212

1313
<artifactId>plugin-manager</artifactId>

Transforms/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.ohnlp.backbone</groupId>
99
<artifactId>backbone-parent</artifactId>
10-
<version>3.0.30</version>
10+
<version>3.0.31</version>
1111
</parent>
1212

1313
<groupId>org.ohnlp.backbone.transforms</groupId>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.ohnlp.backbone</groupId>
88
<artifactId>backbone-parent</artifactId>
9-
<version>3.0.30</version>
9+
<version>3.0.31</version>
1010

1111

1212
<properties>

0 commit comments

Comments
 (0)