我們先來看看GC是如何工作的
總體而言
通過參數優化虛擬機內存的參數如下所示
◆ Xms
初始Heap大小
◆ Xmx
java heap最大值
◆ Xmn
young generation的heap大小
◆ Xss
每個線程的Stack大小
上面是三個比較常用的參數
◆ XX:MinHeapFreeRatio=
Minimum percentage of heap free after GC to avoid expansion
◆ XX:MaxHeapFreeRatio=
Maximum percentage of heap free after GC to avoid shrinking
◆ XX:NewRatio=
Ratio of new/old generation sizes
◆ XX:NewSize=
Default size of new generation (in bytes) [
◆ XX:MaxNewSize=
Maximum size of new generation (in bytes)
◆ XX:SurvivorRatio=
Ratio of eden/survivor space size [Solaris amd
◆ XX:PermSize=
Initial size of permanent generation
◆ XX:MaxPermSize=
Size of the Permanent Generation
下面所說通過優化程序算法來提高內存利用率
看一段代碼
public List<PageData> parse(HtmlPage page) {
List<PageData> list = null;
try {
List valueList = page
if (valueList == null || valueList
return list;
}
//需要時才創建對象
list = new ArrayList<PageData>()
PageData pageData = new PageData()
StringBuilder value = new StringBuilder()
for (int i =
HtmlElement content = (HtmlElement) valueList
DomNodeList<HtmlElement> imgs = content
if (imgs != null && !imgs
for (HtmlElement img : imgs) {
try {
HtmlImage image = (HtmlImage) img;
String path = image
String format = path
String localPath =
File localFile = new File(localPath)
if (!localFile
localFile
image
}
image
localFile = null;
image = null;
img = null;
} catch (Exception e) {
}
}
//這個對象以後不會在使用了
imgs = null;
}
String text = content
value
valueList=null;
content = null;
text = null;
}
pageData
pageData
list
//這裡 pageData=null; 是沒用的
value=null;
//這裡可不能 list=null; 因為list是方法的返回值
} catch (Exception e) {
}
return list;
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26308.html