监理公司管理系统 | 工程企业管理系统 | OA系统 | ERP系统 | 造价咨询管理系统 | 工程设计管理系统 | 签约案例 | 购买价格 | 在线试用 | 手机APP | 产品资料
X 关闭
长春OA行业资讯

当前位置:工程项目OA系统 > 泛普各地 > 吉林OA系统 > 长春OA系统 > 长春OA行业资讯

几个能解决内存溢出异常的方案

申请免费试用、咨询电话:400-8352-114

文章来源:泛普软件

一 调整 虚拟机参数

二 立即 释放无用的对象

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

下面是具体的代码实现

   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专家网论坛)

发布:2007-04-21 14:06    编辑:泛普软件 · xiaona    [打印此页]    [关闭]
长春OA系统
联系方式

成都公司:成都市成华区建设南路160号1层9号

重庆公司:重庆市江北区红旗河沟华创商务大厦18楼

咨询:400-8352-114

加微信,免费获取试用系统

QQ在线咨询

泛普长春OA行业资讯其他应用

长春OA软件 长春OA新闻动态 长春OA信息化 长春OA快博 长春OA行业资讯 长春软件开发公司 长春门禁系统 长春物业管理软件 长春仓库管理软件 长春餐饮管理软件 长春网站建设公司