当前位置:网站首页>JVM-参数配置详解
JVM-参数配置详解
2022-07-25 15:18:00 【文艺青年学编程】
1.常见参数配置
- -XX:+PrintGC 每次触发GC的时候打印相关日志
- -XX:+UseSerialGC 串行回收
- -XX:+PrintGCDetails 更详细的GC日志
- -Xms 堆初始值
- -Xmx 堆最大可用值
- -Xmn 新生代堆最大可用值
- -XX:SurvivorRatio 用来设置新生代中eden空间和from/to空间的比例.
- -XX:NewRatio 配置新生代与老年代占比 1:2
- 含以-XX:SurvivorRatio=eden/from=den/to
总结:在实际工作中,我们可以直接将初始的堆大小与最大堆大小相等,这样的好处是可以减少程序运行时垃圾回收次数,从而提高效率。
2.堆内存大小配置
使用示例: -Xmx20m -Xms5m
说明: 当下Java应用最大可用内存为20M, 初始内存为5M
// byte[] b = new byte[4 * 1024 * 1024]; // System.out.println("分配了4M空间给数组"); System.out.print("最大内存"); System.out.println(Runtime.getRuntime().maxMemory() / 1024.0 / 1024 + "M"); System.out.print("可用内存"); System.out.println(Runtime.getRuntime().freeMemory() / 1024.0 / 1024 + "M"); System.out.print("已经使用内存"); System.out.println(Runtime.getRuntime().totalMemory() / 1024.0 / 1024 + "M"); |
3.设置新生代比例参数
使用示例:-Xms20m -Xmx20m -Xmn1m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
说明:堆内存初始化值20m,堆内存最大值20m,新生代最大值可用1m,eden空间和from/to空间的比例为2/1
使用示例: -Xms20m -Xmx20m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
4.设置新生代与老年代比例参数
-XX:NewRatio=2
说明:堆内存初始化值20m,堆内存最大值20m,新生代最大值可用1m,eden空间和from/to空间的比例为2/1
新生代和老年代的占比为1/2
5.内存溢出与内存泄漏区别
Java内存泄漏就是没有及时清理内存垃圾,导致系统无法再给你提供内存资源(内存资源耗尽);
而Java内存溢出就是你要求分配的内存超出了系统能给你的,系统不能满足需求,于是产生溢出。
内存溢出,这个好理解,说明存储空间不够大。就像倒水倒多了,从杯子上面溢出了来了一样。
内存泄漏,原理是,使用过的内存空间没有被及时释放,长时间占用内存,最终导致内存空间不足,而出现内存溢出。
6.实战OutOfMemoryError异常
Java堆溢出
错误原因: java.lang.OutOfMemoryError: Java heap space 堆内存溢出
解决办法:设置堆内存大小 // -Xms1m -Xmx10m -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError
// -Xms1m -Xmx10m -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError List<Object> listObject = new ArrayList<>(); for (inti = 0; i < 10; i++) { System.out.println("i:" + i); Byte[] bytes = new Byte[1 * 1024 * 1024]; listObject.add(bytes); } System.out.println("添加成功..."); |
虚拟机栈溢出
错误原因: java.lang.StackOverflowError 栈内存溢出
栈溢出 产生于递归调用,循环遍历是不会的,但是循环方法里面产生递归调用, 也会发生栈溢出。
解决办法:设置线程最大调用深度
-Xss5m 设置最大调用深度
publicclass JvmDemo04 { privatestaticintcount; publicstaticvoid count(){ try { count++; count(); } catch (Throwable e) { System.out.println("最大深度:"+count); e.printStackTrace(); } } publicstaticvoid main(String[] args) { count(); } }
|
7内存溢出与内存泄漏区别
Java内存泄漏就是没有及时清理内存垃圾,导致系统无法再给你提供内存资源(内存资源耗尽);
而Java内存溢出就是你要求分配的内存超出了系统能给你的,系统不能满足需求,于是产生溢出。
内存溢出,这个好理解,说明存储空间不够大。就像倒水倒多了,从杯子上面溢出了来了一样。
内存泄漏,原理是,使用过的内存空间没有被及时释放,长时间占用内存,最终导致内存空间不足,而出现内存溢出。
边栏推荐
- Object.prototype. Hasownproperty() and in
- Rediscluster setup and capacity expansion
- Spark DF增加一列
- Process control (Part 1)
- 在网页上实现任意格式的音视频快速播放功能的开发总结。
- Yan required executor memory is above the max threshold (8192mb) of this cluster!
- Deployment and simple use of PostgreSQL learning
- C, c/s upgrade update
- [Android] recyclerview caching mechanism, is it really difficult to understand? What level of cache is it?
- MySQL installation and configuration super detailed tutorial and simple database and table building method
猜你喜欢

Nacos2.1.0 cluster construction

Spark提交参数--files的使用

Fast-lio: fast and robust laser inertial odometer based on tightly coupled IEKF

Spark SQL空值Null,NaN判断和处理

outline和box-shadow实现外轮廓圆角高光效果

Gbdt source code analysis of boosting

JS 同步、异步,宏任务、微任务概述

Outline and box shadow to achieve the highlight effect of contour fillet

【JS高级】js之正则相关函数以及正则对象_02

流程控制(上)
随机推荐
Spark SQL UDF function
打开虚拟机时出现VMware Workstation 未能启动 VMware Authorization Service
outline和box-shadow实现外轮廓圆角高光效果
任务、微任务、队列和调度(动画展示每一步调用)
RedisCluster搭建和扩容
The implementation process of inheritance and the difference between Es5 and ES6 implementation
Browser workflow (Simplified)
Boosting之GBDT源码分析
期货在线开户是否安全?去哪家公司手续费最低?
VS2010 add WAP mobile form template
@Scheduled source code analysis
Idea remotely submits spark tasks to the yarn cluster
CGO is realy Cool!
Es5 thinking of writing inheritance
MySQL之事务与MVCC
继承的实现过程及ES5和ES6实现的区别
The number of query results of maxcompute SQL is limited to 1W
Vscode plugin collection
spark分区算子partitionBy、coalesce、repartition
防抖(debounce)和节流(throttle)