2017-03-08 6 views
0

私はJavaプログラムを持っていますが、サイズが約40GBのファイルから読み込み、フライとストリーミングのデータをDataOutputstreamに変換しています。以下はOutOfMemoryError JSON配列を出力ストリームに書き込むとき

私は2つの方法1は、JSONデータを印刷され、他方は、HTTPSを介してJSONデータを送信しているを持って自分のコード

JSONObject jsonObj= new JSONObject(); 
       JSONArray jsonArray = new JSONArray(); 
       JSONObject properties = new JSONObject(); 

      while((strLine = in.readLine()) != null) 
       { 


      if (jsonArray.length()%100 == 0) { 

        printJsonData(jsonArray); 
      //  sendJsonData(jsonArray, output); 
        jsonArray = new JSONArray(); 
       } 

if (strLine.trim().contains("data")) { 
         jsonObj.put("properties",properties); 
         jsonArray.put(jsonObj); 

    if (strLine.trim().contains("data1")) { 
       String secondPart = strLine.split(":",2)[1]; 
       properties.put("data1", secondPart);  
       continue; 
       } 
      if (strLine.trim().contains("id")) { 
       String secondPart = strLine.split(":",2)[1]; 
       jsonObj.put("id", secondPart); 
      continue; 

      } 

    } 

です。

private void printJsonData(JSONArray jsonArray) { 
     int count = jsonArray.length(); // get totalCount of all jsonObjects 
     for(int i=0 ; i< count; i++){ // iterate through jsonArray 
      JSONObject jsonObject = jsonArray.getJSONObject(i); // get jsonObject @ i position 
      System.out.println("jsonObject " + i + ": " + jsonObject); 
     } 
    } 

private void sendJsonData(JSONObject jsonObj,DataOutputStream output) throws IOException { 

    int count = jsonArray.length();  

    for(int i=0 ; i< count; i++) { // iterate through jsonArray 
     JSONObject jsonObject = jsonArray.getJSONObject(i); // get jsonObject @ i position 
     System.out.println("Sending Data-->" + jsonObject.toString()); 
     output.writeBytes(jsonObject.toString()); 
    } 
} 

私がprintメソッドを呼び出すと、すべて正常に動作します。しかし、私はsendJsonDataメソッドを呼び出すとき。私はOutofMemoryerrorを取得しています。それを修正する方法をお考えですか?

+0

? –

+0

私は、 'sendJsonData'がどこから呼び出されたかを知ることさえできません。 –

+0

@MichaelMarkidis int count = jsonArray.length();それを私が直した。ありがとう – Ammad

答えて

1

どのような方法でjsonを操作しているわけではないので、私はテキストファイルとして扱い、その方法で送信します。大きなJsonObjectを見つける可能性が高いので、あまりにも多くを押し過ぎている可能性があります。

巨大なオブジェクトがない限り、40GBの配列は非常に大きな配列です。

** EDITあなたはsendJsonDataメソッドでcountの値を取得している**個別

それとも、「ファイル」として、個々のJSONオブジェクトを扱い、それらの文字列をプッシュすることができ

+0

意味一度に1つのオブジェクト?配列なし – Ammad

+1

私は配列要素ごとに接続を開始しようとします。各要素を文字列として扱い、ストリームを作成し、より制御された方法で送信します。たぶん、あなたは物事をスピードアップするためにスレッドプールを起動することができます。すべての行を1行ずつ読むのが遅すぎる – efekctive

+0

1つの接続で一度に1つのBig Arrayを送信すると、この問題を解決できます。 - Thans Ammad – Ammad

関連する問題