mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-25 06:24:35 +00:00
Fix File/Memory leak in Annotation Processor. (#67)
* Fix File/Memory leak in Annotation Processor. Mixin's AP requires the minecraft jar + deps on the AP classpath to generate a refmap. These files are then opened by the classloader. TargetMap uses Java's object Serialization to save/load itself from files. When doing so it gets placed into ObjectStreamClass.Caches, this does not get garbage collected enough to allow the Classloader to close, and thus close the file handles it has open. This commit uses Gson to read/write TargetMap to prevent this class form being added to the global JVM cache. * Manually build/read json structure
This commit is contained in:
1 parent
a9683d9a88
commit
87f48a945c
2 files changed
+42
-36
No files matched your search
+2
-1
@@ -103,7 +103,7 @@ sourceSets {
|
||||
ap {
|
||||
compileClasspath += main.output
|
||||
ext.languageVersion = 8
|
||||
ext.compatibility = '1.6'
|
||||
ext.compatibility = '1.8'
|
||||
}
|
||||
fernflower {
|
||||
compileClasspath += main.output
|
||||
@@ -177,6 +177,7 @@ dependencies {
|
||||
// Annotation Processor
|
||||
apImplementation "org.ow2.asm:asm-tree:$asmVersion"
|
||||
apImplementation guava
|
||||
apImplementation gson
|
||||
|
||||
// Fernflower decompiler
|
||||
fernflowerImplementation 'org.jetbrains:intellij-fernflower:1.0.0.9'
|
||||
|
||||
@@ -24,13 +24,14 @@
|
||||
*/
|
||||
package org.spongepowered.tools.obfuscation;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -44,6 +45,7 @@ import org.spongepowered.tools.obfuscation.mirror.TypeHandle;
|
||||
import org.spongepowered.tools.obfuscation.mirror.TypeReference;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import com.google.gson.*;
|
||||
|
||||
/**
|
||||
* Serialisable map of classes to their associated mixins, used so that we can
|
||||
@@ -52,6 +54,7 @@ import com.google.common.io.Files;
|
||||
public final class TargetMap extends HashMap<TypeReference, Set<TypeReference>> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
/**
|
||||
* Session ID, used to identify the temp file
|
||||
@@ -203,26 +206,29 @@ public final class TargetMap extends HashMap<TypeReference, Set<TypeReference>>
|
||||
* @param temp Set "delete on exit" for the file
|
||||
*/
|
||||
public void write(boolean temp) {
|
||||
ObjectOutputStream oos = null;
|
||||
FileOutputStream fout = null;
|
||||
try {
|
||||
File sessionFile = TargetMap.getSessionFile(this.sessionId);
|
||||
if (temp) {
|
||||
sessionFile.deleteOnExit();
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
|
||||
for (Entry<TypeReference, Set<TypeReference>> entry : this.entrySet()) {
|
||||
final JsonArray array = new JsonArray();
|
||||
|
||||
for (TypeReference reference : entry.getValue()) {
|
||||
array.add(new JsonPrimitive(reference.getName()));
|
||||
}
|
||||
fout = new FileOutputStream(sessionFile, true);
|
||||
oos = new ObjectOutputStream(fout);
|
||||
oos.writeObject(this);
|
||||
|
||||
jsonObject.add(entry.getKey().getName(), array);
|
||||
}
|
||||
|
||||
String json = GSON.toJson(jsonObject);
|
||||
|
||||
File sessionFile = TargetMap.getSessionFile(this.sessionId);
|
||||
if (temp) {
|
||||
sessionFile.deleteOnExit();
|
||||
}
|
||||
|
||||
try (FileOutputStream outputStream = new FileOutputStream(sessionFile, true)) {
|
||||
outputStream.write(json.getBytes(StandardCharsets.UTF_8));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (oos != null) {
|
||||
try {
|
||||
oos.close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,22 +239,21 @@ public final class TargetMap extends HashMap<TypeReference, Set<TypeReference>>
|
||||
* @return deserialised map or null if deserialisation failed
|
||||
*/
|
||||
private static TargetMap read(File sessionFile) {
|
||||
ObjectInputStream objectinputstream = null;
|
||||
FileInputStream streamIn = null;
|
||||
try {
|
||||
streamIn = new FileInputStream(sessionFile);
|
||||
objectinputstream = new ObjectInputStream(streamIn);
|
||||
return (TargetMap)objectinputstream.readObject();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (objectinputstream != null) {
|
||||
try {
|
||||
objectinputstream .close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
try (Reader reader = new BufferedReader(new FileReader(sessionFile))) {
|
||||
final JsonObject jsonObject = GSON.fromJson(reader, JsonObject.class);
|
||||
final TargetMap targetMap = new TargetMap();
|
||||
|
||||
for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
|
||||
JsonArray array = entry.getValue().getAsJsonArray();
|
||||
|
||||
for (JsonElement element : array) {
|
||||
targetMap.addMixin(entry.getKey(), element.getAsString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return targetMap;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user