English 中文(简体)
我的法典中找不到记忆漏。
原标题:Can t find a memory leak in my code
  • 时间:2012-01-13 11:53:15
  •  标签:
  • java

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());
    }
最佳回答

getChangeSet,addAffected File ( do? 你们可能想像已经建议的那样使用简介者?

Is it holding on to things? Also, you might want to split that method up into pieces.

问题回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签