以Java代碼的方式總結(jié)幾個典型的內(nèi)存溢出案例
一、圖示
我們先來看看今天要介紹哪些內(nèi)存溢出案例,這里總結(jié)了一張圖,如下所示。

二、定義主類結(jié)構(gòu)
首先,我們創(chuàng)建一個名稱為BlowUpJVM的類,之后所有的案例實驗都是基于這個類進(jìn)行。如下所示。
public class BlowUpJVM {
}
三、棧深度溢出
public static void testStackOverFlow(){
BlowUpJVM.testStackOverFlow();
}
棧不斷遞歸,而且沒有處理,所以虛擬機(jī)棧就不斷深入不斷深入,棧深度就這樣溢出了。
四、永久代內(nèi)存溢出
public static void testPergemOutOfMemory1(){
//方法一失敗
List<String> list = new ArrayList<String>();
while(true){
list.add(UUID.randomUUID().toString().intern());
}
}
打算把String常量池堆滿,沒想到失敗了,JDK1.7后常量池放到了堆里,也能進(jìn)行垃圾回收了。
然后換種方式,使用cglib,用Class把老年代取堆滿
public static void testPergemOutOfMemory2(){
try {
while (true) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(OOM.class);
enhancer.setUseCache(false);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return proxy.invokeSuper(obj, args);
}
});
enhancer.create();
}
}
catch (Exception e){
e.printStackTrace();
}
}
虛擬機(jī)成功內(nèi)存溢出了,那JDK動態(tài)代理產(chǎn)生的類能不能溢出呢?
public static void testPergemOutOfMemory3(){
while(true){
final OOM oom = new OOM();
Proxy.newProxyInstance(oom.getClass().getClassLoader(), oom.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(oom, args);
return result;
}
});
}
}
事實表明,JDK動態(tài)代理差生的類不會造成內(nèi)存溢出,原因是:JDK動態(tài)代理產(chǎn)生的類信息,不會放到永久代中,而是放在堆中。
五、本地方法棧溢出
public static void testNativeMethodOutOfMemory(){
int j = 0;
while(true){
Printer.println(j++);
ExecutorService executors = Executors.newFixedThreadPool(50);
int i=0;
while(i++<10){
executors.submit(new Runnable() {
public void run() {
}
});
}
}
}
這個的原理就是不斷創(chuàng)建線程池,而每個線程池都創(chuàng)建10個線程,這些線程池都是在本地方法區(qū)的,久而久之,本地方法區(qū)就溢出了。
六、JVM棧內(nèi)存溢出
public static void testStackOutOfMemory(){
while (true) {
Thread thread = new Thread(new Runnable() {
public void run() {
while(true){
}
}
});
thread.start();
}
}
線程的創(chuàng)建會直接在JVM棧中創(chuàng)建,但是本例子中,沒看到內(nèi)存溢出,主機(jī)先掛了,不是JVM掛了,真的是主機(jī)掛了,無論在mac還是在windows,都掛了。
溫馨提示,這個真的會死機(jī)的。
七、堆溢出
public static void testOutOfHeapMemory(){
List<StringBuffer> list = new ArrayList<StringBuffer>();
while(true){
StringBuffer B = new StringBuffer();
for(int i = 0 ; i < 10000 ; i++){
B.append(i);
}
list.add(B);
}
}
不斷往堆中塞新增的StringBuffer對象,堆滿了就直接溢出了。
八、測試案例完整代碼
public class BlowUpJVM {
//棧深度溢出
public static void testStackOverFlow(){
BlowUpJVM.testStackOverFlow();
}
//不能引起永久代溢出
public static void testPergemOutOfMemory1(){
//方法一失敗
List<String> list = new ArrayList<String>();
while(true){
list.add(UUID.randomUUID().toString().intern());
}
}
//永久代溢出
public static void testPergemOutOfMemory2(){
try {
while (true) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(OOM.class);
enhancer.setUseCache(false);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return proxy.invokeSuper(obj, args);
}
});
enhancer.create();
}
}
catch (Exception e){
e.printStackTrace();
}
}
//不會引起永久代溢出
public static void testPergemOutOfMemory3(){
while(true){
final OOM oom = new OOM();
Proxy.newProxyInstance(oom.getClass().getClassLoader(), oom.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(oom, args);
return result;
}
});
}
}
//本地方法棧溢出
public static void testNativeMethodOutOfMemory(){
int j = 0;
while(true){
Printer.println(j++);
ExecutorService executors = Executors.newFixedThreadPool(50);
int i=0;
while(i++<10){
executors.submit(new Runnable() {
public void run() {
}
});
}
}
}
//JVM內(nèi)存溢出
public static void testStackOutOfMemory(){
while (true) {
Thread thread = new Thread(new Runnable() {
public void run() {
while(true){
}
}
});
thread.start();
}
}
//堆溢出
public static void testOutOfHeapMemory(){
List<StringBuffer> list = new ArrayList<StringBuffer>();
while(true){
StringBuffer B = new StringBuffer();
for(int i = 0 ; i < 10000 ; i++){
B.append(i);
}
list.add(B);
}
}
}
到此這篇關(guān)于以Java代碼的方式列舉幾個典型的內(nèi)存溢出案例總結(jié)的文章就介紹到這了,更多相關(guān)Java內(nèi)存溢出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Swing中的表格(JTable)和樹(JTree)組件使用實例
這篇文章主要介紹了Java Swing中的表格(JTable)和樹(JTree)組件使用實例,本文同時講解了表格和樹的基本概念、常用方法、代碼實例,需要的朋友可以參考下2014-10-10
SpringBoot的@GetMapping路徑匹配規(guī)則、國際化詳細(xì)教程
這篇文章主要介紹了SpringBoot的@GetMapping路徑匹配規(guī)則、國際化,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
java實現(xiàn)的根據(jù)概率隨機(jī)中獎測試類
這篇文章主要介紹了java實現(xiàn)的根據(jù)概率隨機(jī)中獎測試類,結(jié)合完整實例形式詳細(xì)分析了java隨機(jī)數(shù)實現(xiàn)概率運算相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
springboot整合spring-data-redis遇到的坑
使用springboot整合redis,使用默認(rèn)的序列化配置,然后使用redis-client去查詢時查詢不到相應(yīng)的key.問題出在哪,怎么解決呢?下面小編給大家?guī)砹藄pringboot整合spring-data-redis遇到的坑,需要的的朋友參考下吧2017-04-04

