詳解JVM棧溢出和堆溢出
一、棧溢出StackOverflowError
棧是線程私有的,生命周期與線程相同,每個(gè)方法在執(zhí)行的時(shí)候都會(huì)創(chuàng)建一個(gè)棧幀,用來(lái)存儲(chǔ)局部變量表,操作數(shù)棧,動(dòng)態(tài)鏈接,方法出口等信息。
棧溢出:方法執(zhí)行時(shí)創(chuàng)建的棧幀個(gè)數(shù)超過(guò)了棧的深度。
原因舉例:方法遞歸
【示例】:
public class StackError {
private int i = 0;
public void fn() {
System.out.println(i++);
fn();
}
public static void main(String[] args) {
StackError stackError = new StackError();
stackError.fn();
}
}
【輸出】:

解決方法:調(diào)整JVM棧的大小:-Xss
-Xss size
Sets the thread stack size (in bytes). Append the letter
Linux/x64 (64-bit): 1024 KBmacOS (64-bit): 1024 KBOracle Solaris/x64 (64-bit): 1024 KBWindows: The default value depends on virtual memorykorKto indicate KB,morMto indicate MB, andgorGto indicate GB. The default value depends on the platform:The following examples set the thread stack size to 1024 KB in different units:
-Xss1m
-Xss1024k
-Xss1048576This option is similar to
-XX:ThreadStackSize.
在IDEA中點(diǎn)擊Run菜單的Edit Configuration如下圖:

設(shè)置后,再次運(yùn)行,會(huì)發(fā)現(xiàn)i的值變小,這是因?yàn)樵O(shè)置的-Xss值比原來(lái)的小:

二、堆溢出OutOfMemoryError:Java heap space
堆中主要存放的是對(duì)象。
堆溢出:不斷的new對(duì)象會(huì)導(dǎo)致堆中空間溢出。如果虛擬機(jī)的棧內(nèi)存允許動(dòng)態(tài)擴(kuò)展,當(dāng)擴(kuò)展棧容量無(wú)法申請(qǐng)到足夠的內(nèi)存時(shí)。
【示例】:
public class HeapError {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
try {
while (true) {
list.add("Floweryu");
}
} catch (Throwable e) {
System.out.println(list.size());
e.printStackTrace();
}
}
}
【輸出】:

解決方法:調(diào)整堆的大?。?code>Xmx
-Xmx size
Specifies the maximum size (in bytes) of the memory allocation pool in bytes. This value must be a multiple of 1024 and greater than 2 MB. Append the letter
korKto indicate kilobytes,morMto indicate megabytes, andgorGto indicate gigabytes. The default value is chosen at runtime based on system configuration. For server deployments,-Xmsand-Xmxare often set to the same value. The following examples show how to set the maximum allowed size of allocated memory to 80 MB by using various units:-Xmx83886080
-Xmx81920k
-Xmx80mThe
-Xmxoption is equivalent to-XX:MaxHeapSize.
設(shè)置-Xmx256M后,輸入如下,比之前小:

到此這篇關(guān)于詳解JVM棧溢出和堆溢出的文章就介紹到這了,更多相關(guān)棧溢出和堆溢出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)Redis分布式鎖的三種方案匯總
setnx、Redisson、RedLock?都可以實(shí)現(xiàn)分布式鎖,從易到難得排序?yàn)椋簊etnx?<?Redisson?<?RedLock,本文為大家整理了三種方法的實(shí)現(xiàn),希望對(duì)大家有所幫助2023-11-11
如何使用try-with-resource機(jī)制關(guān)閉連接
這篇文章主要介紹了使用try-with-resource機(jī)制關(guān)閉連接的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java數(shù)據(jù)結(jié)構(gòu)與算法之棧(Stack)實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記第二篇,Java數(shù)據(jù)結(jié)構(gòu)與算法之棧Stack實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
教你利用springboot集成swagger并生成接口文檔
有很多小伙伴不會(huì)利用springboot集成swagger并生成接口文檔,今天特地整理了這篇文章,文中有非常詳細(xì)的代碼圖文介紹及代碼示例,對(duì)不會(huì)這個(gè)方法的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Java正則表達(dá)式匹配字符串并提取中間值的方法實(shí)例
正則表達(dá)式常用于字符串處理、表單驗(yàn)證等場(chǎng)合,實(shí)用高效,下面這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式匹配字符串并提取中間值的相關(guān)資料,需要的朋友可以參考下2022-06-06

