I have the following method that makes a HTTP Get request on a server an parses the response as Java source code. I m doing this for multiple files in the same time. This works quite well for the first few files, but after a certain amount of time I get an exception:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
The exception is thrown in the line: jp.parse(new StringReader(responseString));
I think the problem is a memory leak, because the file I m trying to parse there isn t really big. Only a few dozen lines of code. But I can t find the cause for this exception. Any hints?
public void retrieveSourceCode() {
try {
System.out.println("Try to get: " + getSourceCodeURI());
String responseString = RestServices.getInstance().sendGetRequestJsonTextToString(getSourceCodeURI());
JavaSourceFactory jsf = new JavaSourceFactory();
JavaParser jp = new JavaParser(jsf);
jp.parse(new StringReader(responseString));
Iterator<?> iterator = jsf.getJavaSources();
while(iterator.hasNext()) {
JavaSource source = ((JavaSource) iterator.next());
fileName = source.getQName().toString();
sourceCode = source.toString();
}
} catch (ClientProtocolException e) {
fileName = "no file name";
sourceCode = "no sourcecode available";
e.printStackTrace();
} catch (IOException e) {
fileName = "no file name";
sourceCode = "no sourcecode available";
e.printStackTrace();
} catch (RestServicesException e) {
fileName = "no file name";
sourceCode = "no sourcecode available";
e.printStackTrace();
} catch (RecognitionException e) {
fileName = "no file name";
sourceCode = "no sourcecode available";
e.printStackTrace();
} catch (TokenStreamException e) {
fileName = "no file name";
sourceCode = "no sourcecode available";
e.printStackTrace();
}
if (before == null) {
beforeSourceCode = "no before sourcecode available";
} else {
try {
String responseString = RestServices.getInstance().sendGetRequestJsonTextToString(getBeforeVersionURI());
JavaSourceFactory jsf = new JavaSourceFactory();
JavaParser jp = new JavaParser(jsf);
jp.parse(new StringReader(responseString));
Iterator<?> iterator = jsf.getJavaSources();
while(iterator.hasNext()) {
JavaSource source = (JavaSource) iterator.next();
beforeSourceCode = source.toString();
}
} catch (ClientProtocolException e) {
beforeSourceCode = "no before sourcecode available";
} catch (RecognitionException e) {
beforeSourceCode = "no before sourcecode available";
e.printStackTrace();
} catch (TokenStreamException e) {
beforeSourceCode = "no before sourcecode available";
e.printStackTrace();
} catch (IOException e) {
beforeSourceCode = "no before sourcecode available";
e.printStackTrace();
} catch (RestServicesException e) {
beforeSourceCode = "no before sourcecode available";
e.printStackTrace();
}
}
if (after == null) {
afterSourceCode = "no after sourcecode available";
} else {
try {
String responseString = RestServices.getInstance().sendGetRequestJsonTextToString(getAfterVersionURI());
JavaSourceFactory jsf = new JavaSourceFactory();
JavaParser jp = new JavaParser(jsf);
jp.parse(new StringReader(responseString));
Iterator<?> iterator = jsf.getJavaSources();
while(iterator.hasNext()) {
JavaSource source = (JavaSource) iterator.next();
afterSourceCode = source.toString();
}
} catch (ClientProtocolException e) {
afterSourceCode = "no after sourcecode available";
} catch (RecognitionException e) {
afterSourceCode = "no after sourcecode available";
e.printStackTrace();
} catch (TokenStreamException e) {
afterSourceCode = "no after sourcecode available";
e.printStackTrace();
} catch (IOException e) {
afterSourceCode = "no after sourcecode available";
e.printStackTrace();
} catch (RestServicesException e) {
afterSourceCode = "no after sourcecode available";
e.printStackTrace();
}
}
getChangeSet().addAffectedFile(getFileName());
}