Skip to content

Commit 6172bea

Browse files
committed
Fixing memory leak in JavaExtractor
1 parent 978564a commit 6172bea

4 files changed

Lines changed: 17 additions & 25 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.class
2+
*.lst

JavaExtractor/JPredict/src/main/java/JavaExtractor/ExtractFeaturesTask.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,11 @@
1818

1919
public class ExtractFeaturesTask implements Callable<Void> {
2020
CommandLineValues m_CommandLineValues;
21-
CompilationUnit m_CompilationUnit;
22-
String code = null;
2321
Path filePath;
2422

2523
public ExtractFeaturesTask(CommandLineValues commandLineValues, Path path) {
2624
m_CommandLineValues = commandLineValues;
2725
this.filePath = path;
28-
try {
29-
this.code = new String(Files.readAllBytes(path));
30-
} catch (IOException e) {
31-
e.printStackTrace();
32-
this.code = Common.EmptyString;
33-
}
3426
}
3527

3628
@Override
@@ -52,19 +44,24 @@ public void processFile() {
5244
if (features == null) {
5345
return;
5446
}
55-
47+
5648
String toPrint = featuresToString(features);
5749
if (toPrint.length() > 0) {
58-
System.out.println(toPrint);
50+
System.out.println(toPrint);
5951
}
6052
}
6153

6254
public ArrayList<ProgramFeatures> extractSingleFile() throws ParseException, IOException {
63-
FeatureExtractor featureExtractor = new FeatureExtractor(m_CommandLineValues, code);
64-
65-
ArrayList<ProgramFeatures> features = featureExtractor.extractFeatures();
55+
String code = null;
56+
try {
57+
code = new String(Files.readAllBytes(this.filePath));
58+
} catch (IOException e) {
59+
e.printStackTrace();
60+
code = Common.EmptyString;
61+
}
62+
FeatureExtractor featureExtractor = new FeatureExtractor(m_CommandLineValues);
6663

67-
m_CompilationUnit = featureExtractor.getParsedFile();
64+
ArrayList<ProgramFeatures> features = featureExtractor.extractFeatures(code);
6865

6966
return features;
7067
}

JavaExtractor/JPredict/src/main/java/JavaExtractor/FeatureExtractor.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
@SuppressWarnings("StringEquality")
2424
public class FeatureExtractor {
25-
private String code;
26-
private CompilationUnit m_CompilationUnit;
2725
private CommandLineValues m_CommandLineValues;
2826
private static Set<String> s_ParentTypeToAddChildId = Stream
2927
.of("AssignExpr", "ArrayAccessExpr", "FieldAccessExpr", "MethodCallExpr")
@@ -34,20 +32,15 @@ public class FeatureExtractor {
3432
final static String upSymbol = "^";
3533
final static String downSymbol = "_";
3634

37-
public FeatureExtractor(CommandLineValues commandLineValues, String code) {
35+
public FeatureExtractor(CommandLineValues commandLineValues) {
3836
this.m_CommandLineValues = commandLineValues;
39-
this.code = code;
4037
}
4138

42-
public CompilationUnit getParsedFile() {
43-
return m_CompilationUnit;
44-
}
45-
46-
public ArrayList<ProgramFeatures> extractFeatures() throws ParseException, IOException {
47-
m_CompilationUnit = parseFileWithRetries(code);
39+
public ArrayList<ProgramFeatures> extractFeatures(String code) throws ParseException, IOException {
40+
CompilationUnit compilationUnit = parseFileWithRetries(code);
4841
FunctionVisitor functionVisitor = new FunctionVisitor();
4942

50-
functionVisitor.visit(m_CompilationUnit, null);
43+
functionVisitor.visit(compilationUnit, null);
5144

5245
ArrayList<MethodContent> methods = functionVisitor.getMethodContents();
5346
ArrayList<ProgramFeatures> programs = generatePathFeatures(methods);
-135 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)