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