当前位置:网站首页>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内存溢出就是你要求分配的内存超出了系统能给你的,系统不能满足需求,于是产生溢出。
内存溢出,这个好理解,说明存储空间不够大。就像倒水倒多了,从杯子上面溢出了来了一样。
内存泄漏,原理是,使用过的内存空间没有被及时释放,长时间占用内存,最终导致内存空间不足,而出现内存溢出。
边栏推荐
- 延迟加载源码剖析:
- System. Accessviolationexception: an attempt was made to read or write to protected memory. This usually indicates that other memory is corrupted
- [comprehensive pen test] difficulty 4/5, classic application of line segment tree for character processing
- How to solve the problem of scanf compilation error in Visual Studio
- Idea远程提交spark任务到yarn集群
- outline和box-shadow实现外轮廓圆角高光效果
- MFC 线程AfxBeginThread基本用法,传多个参数
- Instance tunnel use
- Universal smart JS form verification
- 防抖(debounce)和节流(throttle)
猜你喜欢

Process control (Part 1)

NPM's nexus private server e401 E500 error handling record

node学习

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

Award winning interaction | 7.19 database upgrade plan practical Summit: industry leaders gather, why do they come?

推荐10个堪称神器的学习网站

一个程序最多可以使用多少内存?

Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection

Implement a simple restful API server

Spark partition operators partitionby, coalesce, repartition
随机推荐
Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
[comprehensive pen test] difficulty 4/5, classic application of line segment tree for character processing
ice 100G 网卡分片报文 hash 问题
Implementation of asynchronous FIFO
用setTimeout模拟setInterval定时器
Maxcompute SQL 的查询结果条数受限1W
My creation anniversary
Meanshift clustering-01 principle analysis
spark中saveAsTextFile如何最终生成一个文件
Scala111-map、flatten、flatMap
sql to linq 之存储过程偏
How to solve the problem of scanf compilation error in Visual Studio
Detailed explanation of lio-sam operation process and code
Recommend 10 learning websites that can be called artifact
记一次Spark报错:Failed to allocate a page (67108864 bytes), try again.
Gbdt source code analysis of boosting
Example of password strength verification
Automatically set the template for VS2010 and add header comments
深入:微任务与宏任务
Spark submission parameters -- use of files