詳解JVM棧溢出和堆溢出
一、棧溢出StackOverflowError
棧是線程私有的,生命周期與線程相同,每個(gè)方法在執(zhí)行的時(shí)候都會(huì)創(chuàng)建一個(gè)棧幀,用來存儲(chǔ)局部變量表,操作數(shù)棧,動(dòng)態(tài)鏈接,方法出口等信息。
棧溢出:方法執(zhí)行時(shí)創(chuàng)建的棧幀個(gè)數(shù)超過了棧的深度。
原因舉例:方法遞歸
【示例】:
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棧的大?。?code>-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 memoryk
orK
to indicate KB,m
orM
to indicate MB, andg
orG
to 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值比原來的小:
二、堆溢出OutOfMemoryError:Java heap space
堆中主要存放的是對(duì)象。
堆溢出:不斷的new
對(duì)象會(huì)導(dǎo)致堆中空間溢出。如果虛擬機(jī)的棧內(nèi)存允許動(dòng)態(tài)擴(kuò)展,當(dāng)擴(kuò)展棧容量無法申請(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
k
orK
to indicate kilobytes,m
orM
to indicate megabytes, andg
orG
to indicate gigabytes. The default value is chosen at runtime based on system configuration. For server deployments,-Xms
and-Xmx
are 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
-Xmx
option is equivalent to-XX:MaxHeapSize
.
設(shè)置-Xmx256M
后,輸入如下,比之前?。?/p>
到此這篇關(guān)于詳解JVM棧溢出和堆溢出的文章就介紹到這了,更多相關(guān)棧溢出和堆溢出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis 如何利用resultMap復(fù)雜類型list映射
這篇文章主要介紹了mybatis 如何利用resultMap復(fù)雜類型list映射的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07解決weblogic部署springboot項(xiàng)目步驟及可能會(huì)出現(xiàn)的問題
這篇文章主要介紹了解決weblogic部署springboot項(xiàng)目步驟及可能會(huì)出現(xiàn)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot整合Retry實(shí)現(xiàn)錯(cuò)誤重試過程逐步介紹
重試的使用場(chǎng)景比較多,比如調(diào)用遠(yuǎn)程服務(wù)時(shí),由于網(wǎng)絡(luò)或者服務(wù)端響應(yīng)慢導(dǎo)致調(diào)用超時(shí),此時(shí)可以多重試幾次。用定時(shí)任務(wù)也可以實(shí)現(xiàn)重試的效果,但比較麻煩,用Spring Retry的話一個(gè)注解搞定所有,感興趣的可以了解一下2023-02-02從0開始教你開發(fā)一個(gè)springboot應(yīng)用
這篇文章主要為大家介紹了從0開始開發(fā)一個(gè)springboot應(yīng)用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Java 實(shí)戰(zhàn)項(xiàng)目之精品養(yǎng)老院管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+mybatis+Vue+Mysql實(shí)現(xiàn)一個(gè)精品養(yǎng)老院管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11MybatisPlus中QueryWrapper常用方法總結(jié)
MyBatis-Plus是一個(gè)Mybatis增強(qiáng)版工具,在MyBatis上擴(kuò)充了其他功能沒有改變其基本功能,為了簡(jiǎn)化開發(fā)提交效率而存在,queryWrapper是mybatis plus中實(shí)現(xiàn)查詢的對(duì)象封裝操作類,本文就給大家總結(jié)了MybatisPlus中QueryWrapper的常用方法,需要的朋友可以參考下2023-07-07Java中鎖的實(shí)現(xiàn)和內(nèi)存語義淺析
這篇文章主要給大家介紹了關(guān)于Java中鎖的實(shí)現(xiàn)和內(nèi)存語義的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11