當前位置:工程項目OA系統(tǒng) > 泛普各地 > 吉林OA系統(tǒng) > 長春OA系統(tǒng) > 長春OA行業(yè)資訊
幾個能解決內(nèi)存溢出異常的方案
一 調(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)論壇)
- 1CTO解讀企業(yè)數(shù)據(jù)安全管理與防護
- 2開源的藝術(shù) 安全市場的新突破點
- 3中小企業(yè)服務器虛擬化應用成本分析
- 4臺式機無線聯(lián)網(wǎng)將成為趨勢
- 5PHP程序不適用大型系統(tǒng)之九大原因
- 6四大高招教你如何管好加密軟件
- 7Windows系統(tǒng)緊急安全配置指南
- 8信息架構(gòu)需要綠色整合
- 9漏洞!安全軟件的致命軟肋在哪里
- 10網(wǎng)友:從Windows轉(zhuǎn)向UNIX的一些困惑
- 11全球救市計劃可能推動高性能計算發(fā)展
- 12虛擬化是實現(xiàn)綠色IT的關(guān)鍵因素
- 133G傳輸很務實:MSTP沿用PTN跟進
- 14長春泛普OA平臺提供了一種全新的工作方式
- 15云計算專家委員會成立 匯聚產(chǎn)學研用資源
- 16分析:虛擬化的可靠性遭遇挑戰(zhàn)
- 17考核企業(yè)IT安全顧問的十道問題
- 18信息安全保障步入“面向服務”的時代
- 19OA辦公軟件-OA系統(tǒng)試用的目的和意義
- 20數(shù)據(jù)丟失防護 企業(yè)何去何從?
- 21以綠色的名義回歸 瘦客戶機卷土重來
- 22長春OA軟件平臺型的幾大特征
- 23在線存儲的增長將趕超過傳統(tǒng)存儲
- 24與你分享安全上網(wǎng)之“防毒八法”
- 25淺析計算機網(wǎng)絡(luò)安全的六大指標
- 26擺脫網(wǎng)關(guān)制約輕松進行跨網(wǎng)訪問
- 27優(yōu)化企業(yè)網(wǎng)絡(luò)應用 網(wǎng)絡(luò)管理勢在必行
- 28IDC:全球經(jīng)濟危機給云計算帶來曙光
- 29Linux系統(tǒng)能搞定的20件事情
- 30太陽、風、海藻:未來數(shù)據(jù)中心的能源
成都公司:成都市成華區(qū)建設(shè)南路160號1層9號
重慶公司:重慶市江北區(qū)紅旗河溝華創(chuàng)商務大廈18樓