2012-01-13 14 views
-1

私は、サーバー上でHTTP GetリクエストをJavaソースコードとしてレスポンスを解析する方法を持っています。私は同じ時間に複数のファイルに対してこれをやっています。これは、最初のいくつかのファイルのために非常によく動作しますが、一定の時間後、私は例外を取得:私のコードでメモリリークを見つけることができません

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space 

例外がラインにスローされます。jp.parse(new StringReader(responseString));私は問題はメモリリークだと思う、ファイルので、私は本当に大きなものではないことを解析しようとしています。わずか数十行のコードしかありません。しかし、私はこの例外の原因を見つけることができません。何かヒント?

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()); 
    } 
+3

なぜメモリプロファイラを使用せず、メモリの使用状況を確認してください。 – NPE

+1

メモリダンプを作成して分析しましたか? – Thomas

+0

お勧めできますか? –

答えて

2

getChangeSet().addAffectedFile()は何をしますか?既に提案されているようにプロファイラを使用したいかもしれませんか?

これは物事を保持していますか?また、そのメソッドを分割して分割することもできます。

+0

これは渡された文字列をarraylistに追加するだけです。 – RoflcoptrException

+0

arraylistはこれまでにクリアされていますか? – Bill

+0

いいえ私はそれらの文字列が必要です;) – RoflcoptrException

1

なぜあなたは内部の同じ処理のコードでこれらのcatchブロックを繰り返し続けるのですか?私はExceptionを一度キャッチして、それで済むだろう。残りの部分は視覚的な混乱を加えるだけで何もしません。

しかし、それはあなたの質問への答えではありません....

は、これらのクラスのあなたです、または彼らは、ライブラリからですか?私はそれがApache JAXMeライブラリだと思っています。

Sun JVMを使用している場合は、Visual VM 1.3.3をダウンロードし、すべてのプラグインをインストールして開き、プロセスを開始して何が起こっているのかを確認できます。それは世代、CPU、スレッドなどによってメモリを表示します。これは素晴らしいツールです。

finallyブロックを追加して配列を消去してみてはいかがですか?あなたはコール間でそのデータを保持しませんか?はいの場合、他の発信者がどのように検索して結果を使用しますか?おそらくWeakHashMapがあなたの解決策になるかもしれません。必要に応じて、JVMにクリーンアップの機会を与えます。

スレッドの安全性に注意する必要があります。サーブレットは共有されているので、その可変オブジェクトにぶら下がっている場合は、同期について心配する必要があります。

+0

はい、1つのtry/catchブロックにリファクタリングする必要があります。それは私のクラスです。 – RoflcoptrException

+0

どのアレイについてお話していますか? – RoflcoptrException

+0

あなたが要素の数を印刷したもの。私はあなたがローカル変数を意味していたと推測しました。 – duffymo

関連する問題