監(jiān)理公司管理系統(tǒng) | 工程企業(yè)管理系統(tǒng) | OA系統(tǒng) | ERP系統(tǒng) | 造價咨詢管理系統(tǒng) | 工程設(shè)計管理系統(tǒng) | 簽約案例 | 購買價格 | 在線試用 | 手機APP | 產(chǎn)品資料
X 關(guān)閉

幾個能解決內(nèi)存溢出異常的方案

申請免費試用、咨詢電話:400-8352-114

文章來源:泛普軟件

一 調(diào)整 虛擬機參數(shù)

二 立即 釋放無用的對象

三 利用序列化和反序列化技術(shù)

下面是具體的代碼實現(xiàn)

   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

* 調(diào)整 虛擬機參數(shù)

*

* < 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

* 利用序列化和反序列化技術(shù)

* < 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專家網(wǎng)論壇)

發(fā)布:2007-04-21 14:06    編輯:泛普軟件 · xiaona    [打印此頁]    [關(guān)閉]
相關(guān)文章:
長春OA系統(tǒng)
聯(lián)系方式

成都公司:成都市成華區(qū)建設(shè)南路160號1層9號

重慶公司:重慶市江北區(qū)紅旗河溝華創(chuàng)商務大廈18樓

咨詢:400-8352-114

加微信,免費獲取試用系統(tǒng)

QQ在線咨詢

泛普長春OA行業(yè)資訊其他應用

長春OA軟件 長春OA新聞動態(tài) 長春OA信息化 長春OA快博 長春OA行業(yè)資訊 長春軟件開發(fā)公司 長春門禁系統(tǒng) 長春物業(yè)管理軟件 長春倉庫管理軟件 長春餐飲管理軟件 長春網(wǎng)站建設(shè)公司