内存溢出异常解决方案总结

  这几天刚好碰到一个java 内存溢出的问题。一直尝试着解决这个问题目前想到了三个方法:一 调整 虚拟机参数二 立即 释放无用的对象三 利用序列化和反序列化技术

  下面是具体的代码实现

  package com.free;

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileNotFoundException;

  import java.io.FileOutputStream;

  import java.io.IOException;

  import java.io.ObjectInputStream;

  import java.io.ObjectOutputStream;

  import java.math.BigInteger;

  import java.util.ArrayList;

  import java.util.List;

  /**

  * @author free

  * springcomingagain@gmail.com

  */

  public class OutMemorySolution {

  /***

  * Change JVM Arguments

  * 调整 虚拟机参数

  *

  *

 

  *

  •  

      * First Try

      *

      * -Xloggc:gc.log

      * -Xms256M

      * -Xmx256M

      *

      * throw java.lang.OutOfMemoryError: Java heap space

      *

      *

  •  

      * Second Try

      *

      * -Xloggc:gc.log

      * -Xms512M

      * -Xmx512M

      *

      *

 

  */

  public static void changeJVMArguments() {

  long startTime = System.currentTimeMillis();

  System.out.println("Start : " + startTime);

  List list = new ArrayList (0);

  for (int i = 0; i < 5000000; i++) {

  list.add(new BigInteger("1000000"));

  }

  long endTime = System.currentTimeMillis();

  System.out.println("End.. : " + endTime + " Cost : "

  + (endTime - startTime));

  }

  /**

  * Release Object Immediate

  * 立即 释放无用的对象

  *

 

  *

  •  

      * Release Object

      *

      *

      * //First Release Object

      * BigInteger temp1 = null;

      * for (int i = 0; i < 1200000; i++) {

      * temp1 = array1[i];

      * temp1 = null;

      * array1[i] = null;

      * }

      * array1 = null;

      * ...

      * //Second Release Object

      * temp1 = null;

      * for (int i = 0; i < 1200000; i++) {

      * temp1 = array2[i];

      * temp1 = null;

      * array2[i] = null;

      * }

      * array2 = null;

      *

      *

 

  */

  public static void releaseObjectImmediate() {

  long startTime = System.currentTimeMillis();

  System.out.println("First Start : " + startTime);

  BigInteger[] array1 = new BigInteger[1200000];

  for (int i = 0; i < 1200000; i++) {

  array1[i] = new BigInteger("10000000");

  }

  // First Release Object

  BigInteger temp1 = null;

  for (int i = 0; i < 1200000; i++) {

  temp1 = array1[i];

  temp1 = null;

  array1[i] = null;

  }

  array1 = null;

  long endTime = System.currentTimeMillis();

  System.out.println("First End.. : " + endTime + " Cost : "

  + (endTime - startTime));

  // do something start

  // do something end..

  startTime = System.currentTimeMillis();

  System.out.println("Second Start : " + startTime);

  BigInteger[] array2 = new BigInteger[1200000];

  for (int i = 0; i < 1200000; i++) {

  array2[i] = new BigInteger("10000000");

  }

  // Second Release Object

  temp1 = null;

  for (int i = 0; i < 1200000; i++) {

  temp1 = array2[i];

  temp1 = null;

  array2[i] = null;

  }

  array2 = null;

  endTime = System.currentTimeMillis();

  System.out.println("Second End.. : " + endTime + " Cost : "

  + (endTime - startTime));

  }

  /**

  * delaySave

  * 利用序列化和反序列化技术

  *

  • Store Object

     

      *

  • read Object

     

      *

  • do something

     

      */

      @SuppressWarnings("unchecked")

      public static void delaySave() {

      final String objectStoreFolder ="D://obj//";

      System.out.println("Store Object Start..");

      // Store Object 序列化对象

      ObjectOutputStream out = null;

      try {

      out = new ObjectOutputStream(new FileOutputStream(

      objectStoreFolder+ "objectFile.obj" + 0));

      List list = new ArrayList (0);

      for (int i = 0; i < 5000000; i++) {

      if (((i+1) % 100000) == 0) {

      out.writeObject(list);

      out.close();

      out = null;

      list = null;

      list = new ArrayList (0);

      out = new ObjectOutputStream(new FileOutputStream(

      objectStoreFolder+ "objectFile.obj" + ((i % 1000000) + 1)));

      }

      System.out.println(i);

      list.add(new BigInteger("1000000"));

      }

      } catch (FileNotFoundException e) {

      System.err.println(e.getMessage());

      } catch (IOException e) {

      System.err.println(e.getMessage());

      }

      finally{

      if(null != out ){

      try {

      out.close();

      out = null;

      } catch (IOException e) {

      // ignore

      }

      }

      }

      System.out.println("Store Object End..");

      System.out.println("Read Object Start..");

      // read Object 反序列化对象

      File [] fileList = new File(objectStoreFolder).listFiles();

      int fileSize = fileList.length;

      ObjectInputStream in = null;

      try {

      for (int i = 0; i < fileSize; i++) {

      in = new ObjectInputStream(

      new FileInputStream(fileList[i]));

      List list = (List ) in.readObject();

      // do something start

      System.out.println(list.size());

      // do something end..

      }

      } catch (FileNotFoundException e) {

      System.err.println(e.getMessage());

      } catch (IOException e) {

      System.err.println(e.getMessage());

      } catch (ClassNotFoundException e) {

      System.err.println(e.getMessage());

      }

      finally{

      if(null != in ){

      try {

      in.close();

      in = null;

      } catch (IOException e) {

      // ignore

      }

      }

      }

      System.out.println("Read Object End..");

      }

      /**

      * Test code

      * @param args

      */

      public static void main(String[] args) {

      changeJVMArguments();

      releaseObjectImmediate();

      delaySave();

      }

      }

 

<script type="text/javascript"> if ($ != jQuery) { $ = jQuery.noConflict(); } </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值