大家好,我是冰河~~
作为程序员,多多少少都会遇到一些内存溢出的场景,如果你还没遇到,说明你工作的年限可能比较短,或者你根本就是个假程序员!哈哈,开个玩笑。
当初面试官让我现场手写内存溢出案例代码,我就以Java代码的方式列举了几个典型的内存溢出案例。今天分享给大家,希望大家在日常工作中,尽量避免写这些low水平的代码。
小伙伴们点赞,收藏,评论,走起呀
我们先来看看今天要介绍哪些内存溢出案例,冰河这里总结了一张图,如下所示。
说干就干,咱们开始吧!!
定义主类结构
首先,我们创建一个名称为BlowUpJVM的类,之后所有的案例实验都是基于这个类进行。如下所示。
publicclassBlowUpJVM{}
栈深度溢出
publicstaticvoidtestStackOverFlow(){BlowUpJVM.testStackOverFlow();}
栈不断递归,而且没有处理,所以虚拟机栈就不断深入不断深入,栈深度就这样溢出了。
永久代内存溢出
publicstaticvoidtestPergemOutOfMemory1(){//方法一失败List<String> list=newArrayList<String>();while(true){
list.add(UUID.randomUUID().toString().intern());}}
打算把String常量池堆满,没想到失败了,JDK1.7后常量池放到了堆里,也能进行垃圾回收了。
然后换种方式,使用cglib,用Class把老年代取堆满
publicstaticvoidtestPergemOutOfMemory2(){try{while(true){Enhancer enhancer=newEnhancer();
enhancer.setSuperclass(OOM.class);
enhancer.setUseCache(false);
enhancer.setCallback(newMethodInterceptor(){@OverridepublicObjectintercept(Object obj,Method method,Object[] args,MethodProxy proxy)throwsThrowable{return proxy.invokeSuper(obj, args);}});
enhancer.create();}}catch(Exception e){
e.printStackTrace();}}
虚拟机成功内存溢出了,那JDK动态代理产生的类能不能溢出呢?
publicstaticvoidtestPergemOutOfMemory3(){while(true){finalOOM oom=newOOM();Proxy.newProxyInstance(oom.getClass().getClassLoader(), oom.getClass().getInterfaces(),newInvocationHandler(){publicObjectinvoke(Object proxy,Method method,Object[] args)throwsThrowable{Object result= method.invoke(oom, args);return result;}});}}
事实表明,JDK动态代理差生的类不会造成内存溢出,原因是:JDK动态代理产生的类信息,不会放到永久代中,而是放在堆中。
本地方法栈溢出
publicstaticvoidtestNativeMethodOutOfMemory(){int j=0;while(true){Printer.println(j++);ExecutorService executors=Executors.newFixedThreadPool(50);int i=0;while(i++<10){
executors.submit(newRunnable(){publicvoidrun(){}});}}}
这个的原理就是不断创建线程池,而每个线程池都创建10个线程,这些线程池都是在本地方法区的,久而久之,本地方法区就溢出了。
JVM栈内存溢出
publicstaticvoidtestStackOutOfMemory(){while(true){Thread thread=newThread(newRunnable(){publicvoidrun(){while(true){}}});
thread.start();}}
线程的创建会直接在JVM栈中创建,但是本例子中,没看到内存溢出,主机先挂了,不是JVM挂了,真的是主机挂了,无论在mac还是在windows,都挂了。
温馨提示,这个真的会死机的。
堆溢出
publicstaticvoidtestOutOfHeapMemory(){List<StringBuffer> list=newArrayList<StringBuffer>();while(true){StringBufferB=newStringBuffer();for(int i=0; i<10000; i++){B.append(i);}
list.add(B);}}
不断往堆中塞新增的StringBuffer对象,堆满了就直接溢出了。
测试案例完整代码
publicclassBlowUpJVM{//栈深度溢出publicstaticvoidtestStackOverFlow(){BlowUpJVM.testStackOverFlow();}//不能引起永久代溢出publicstaticvoidtestPergemOutOfMemory1(){//方法一失败List<String> list=newArrayList<String>();while(true){
list.add(UUID.randomUUID().toString().intern());}}//永久代溢出publicstaticvoidtestPergemOutOfMemory2(){try{while(true){Enhancer enhancer=newEnhancer();
enhancer.setSuperclass(OOM.class);
enhancer.setUseCache(false);
enhancer.setCallback(newMethodInterceptor(){@OverridepublicObjectintercept(Object obj,Method method,Object[] args,MethodProxy proxy)throwsThrowable{return proxy.invokeSuper(obj, args);}});
enhancer.create();}}catch(Exception e){
e.printStackTrace();}}//不会引起永久代溢出publicstaticvoidtestPergemOutOfMemory3(){while(true){finalOOM oom=newOOM();Proxy.newProxyInstance(oom.getClass().getClassLoader(), oom.getClass().getInterfaces(),newInvocationHandler(){publicObjectinvoke(Object proxy,Method method,Object[] args)throwsThrowable{Object result= method.invoke(oom, args);return result;}});}}//本地方法栈溢出publicstaticvoidtestNativeMethodOutOfMemory(){int j=0;while(true){Printer.println(j++);ExecutorService executors=Executors.newFixedThreadPool(50);int i=0;while(i++<10){
executors.submit(newRunnable(){publicvoidrun(){}});}}}//JVM内存溢出publicstaticvoidtestStackOutOfMemory(){while(true){Thread thread=newThread(newRunnable(){publicvoidrun(){while(true){}}});
thread.start();}}//堆溢出publicstaticvoidtestOutOfHeapMemory(){List<StringBuffer> list=newArrayList<StringBuffer>();while(true){StringBufferB=newStringBuffer();for(int i=0; i<10000; i++){B.append(i);}
list.add(B);}}}
写在最后
如果你想进大厂,想升职加薪,或者对自己现有的工作比较迷茫,都可以私信我交流,希望我的一些经历能够帮助到大家~~
推荐阅读:
- 《奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些核心技能是你必须要掌握的!完整学习路线!!(建议收藏)》
- 《奉劝那些刚参加工作的学弟学妹们:这些计算机与操作系统基础知识越早知道越好!万字长文太顶了!!(建议收藏)》
- 《我用三天时间开发了一款老少皆宜的国民级游戏,支持播放音乐,现开放完整源代码和注释(建议收藏)!!》
- 《我是全网最硬核的高并发编程作者,CSDN最值得关注的博主,大家同意吗?(建议收藏)》
- 《毕业五年,从月薪3000到年薪百万,我掌握了哪些核心技能?(建议收藏)》
- 《我入侵了隔壁妹子的Wifi,发现。。。(全程实战干货,建议收藏)》
- 《千万不要轻易尝试“熊猫烧香”,这不,我后悔了!》
- 《清明节偷偷训练“熊猫烧香”,结果我的电脑为熊猫“献身了”!》
- 《7.3万字肝爆Java8新特性,我不信你能看完!(建议收藏)》
- 《在业务高峰期拔掉服务器电源是一种怎样的体验?》
- 《半小时实现Java手撸Http协议,爽!!(附完整源码,建议收藏)》
- 《毕业五年,年薪百万,只因我做对了这些事!!(建议收藏)》
最后,附上并发编程需要掌握的核心技能知识图,祝大家在学习并发编程时,少走弯路。
好了,今天就到这儿吧,小伙伴们点赞、收藏、评论,一键三连走起呀,我是冰河,我们下期见~~