几个能解决内存溢出异常的方案
一 调整 虚拟机参数
二 立即 释放无用的对象
三 利用序列化和反序列化技术
下面是具体的代码实现
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
* 调整 虚拟机参数
*
* < pre>
* < li>
* First Try
* < /li>
* -Xloggc:gc.log
* -Xms256M
* -Xmx256M
*
* throw java.lang.OutOfMemoryError: Java heap space
*
* < li>
* Second Try
* < /li>
* -Xloggc:gc.log
* -Xms512M
* -Xmx512M
*
* < /pre>
*/
public static void changeJVMArguments() {
long startTime = System.currentTimeMillis();
System.out.println("Start : " + startTime);
List< BigInteger> list = new ArrayList< BigInteger>(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
* 立即 释放无用的对象
* < pre>
* < li>
* Release Object
* < /li>
* < code>
* //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;
* < /code>
* < /pre>
*/
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
* 利用序列化和反序列化技术
* < li>Store Object < /li>
* < li>read Object< /li>
* < li>do something< /li>
*/
@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< BigInteger> list = new ArrayList< BigInteger>(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< BigInteger>(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< BigInteger> list = (List< BigInteger>) 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();
}
}
(IT专家网论坛)
- 1CTO解读企业数据安全管理与防护
- 2开源的艺术 安全市场的新突破点
- 3中小企业服务器虚拟化应用成本分析
- 4台式机无线联网将成为趋势
- 5PHP程序不适用大型系统之九大原因
- 6四大高招教你如何管好加密软件
- 7Windows系统紧急安全配置指南
- 8信息架构需要绿色整合
- 9漏洞!安全软件的致命软肋在哪里
- 10网友:从Windows转向UNIX的一些困惑
- 11全球救市计划可能推动高性能计算发展
- 12虚拟化是实现绿色IT的关键因素
- 133G传输很务实:MSTP沿用PTN跟进
- 14长春泛普OA平台提供了一种全新的工作方式
- 15云计算专家委员会成立 汇聚产学研用资源
- 16分析:虚拟化的可靠性遭遇挑战
- 17考核企业IT安全顾问的十道问题
- 18信息安全保障步入“面向服务”的时代
- 19OA办公软件-OA系统试用的目的和意义
- 20数据丢失防护 企业何去何从?
- 21以绿色的名义回归 瘦客户机卷土重来
- 22长春OA软件平台型的几大特征
- 23在线存储的增长将赶超过传统存储
- 24与你分享安全上网之“防毒八法”
- 25浅析计算机网络安全的六大指标
- 26摆脱网关制约轻松进行跨网访问
- 27优化企业网络应用 网络管理势在必行
- 28IDC:全球经济危机给云计算带来曙光
- 29Linux系统能搞定的20件事情
- 30太阳、风、海藻:未来数据中心的能源