使用maven时,如何修改JVM的配置参数;maven命令执行时到底消耗多少内存?
maven是使用java启动的,因此依赖JVM,那么如何修改JVM参数?
MAVEN_OPTS
在系统的环境变量中,设置MAVEN_OPTS,用以存放JVM的参数,具体设置的步骤,参数示例如下:
MAVEN_OPTS=-Xms256m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256M
或者临时设置 export MAVEN_OPTS=-Xms256m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256M
在mvn中添加MAVEN_OPTS
找到Maven的安装目录,在bin目录下,编辑mvn.bat(linux下,mvn)
set MAVEN_OPTS=-Xms256m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256M
Vim mvn:

设置JVM最大堆heap内存为一个很小的值5m,如图,报错:GC overhead limit exceeded

查看详细信息:OOM了

关于下图标识的含义: 27M表示构建结束的时刻所使用的堆heap大小;535M表示构建结束时刻的分配的堆大小
分配的堆大小需要 > 使用的堆大小,不然就会内存溢出OOM


Here :
Final Memory: 47M/535M
47M is the used memory and 535M the current allocated memory (heap size) by the JVM at the end of the build.
But these information could not give you the peak value.
Why the used memory (43M) cannot be considered as the peak ?
- Because the peak may have occurred before the end of the build.
Why the heap size (636M) cannot be considered as the peak ?
Because the peak may be superior to the heap size if the GC reduced the heap size between the peak and the end of the build .
It may be the case for a very long build that performs intensive tasks at the beginning and lighter tasks then.Because the peak may be inferior to the heap size if the GC has increased the heap size until this value but the application never needs to consume as much as memory.
To get the memory peak during the execution of the maven build, monitor the Java application (maven execution) with any JVM tool designed for : JVisualVM or JConsole for example.
Here's an screenshot of a maven build of a Spring Boot application monitored by JVisualVM that shows that neither the heap size or the used size at the end of build are the peak value :

JVisualVM is the best as it can be used to monitor several processes at once. And when running the maven build, make sure to open(double click) the relevant processes generated under the Applicationwindow of the JVisualVM as the build processes are killed once after the built is completed making you unable to monitor the process. By opening the relevant process before it is being killed, will allow to monitor the build process well.
Yes, and if you have e.g. unit tests, the surefire plugin starts parallel an additional process without using MAVEN_OPTS for that.
maven的单元测试插件会另外起个进程,这个进程的堆大小不受MAVEN_OPTS的配置约束,所有说maven命令行执行,内存到底够不够,关键看两个参数,一个是主进程的堆heap的峰值peak;一个是单测插件surefire起的进程的所需堆heap的峰值peak。因为一个项目有多个模块moduls,那就得看最消耗heap的那个模块的峰值peak
如下图所示,会在某个时刻出现一个所需堆heap的极值,如果系统不能满足这个大小,就会OOM(out of memory)内存溢出


另外surefire插件到底可以jvm再拉出几个进程,以及每个进程的jvm配置是如何的,都是可以配置的,参考:http://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html
还有就是surefire可以不另起进程,这样减少内存的使用,见插件官网说明
至于配置几个并行进程,就要看设备内存已经项目执行时间要求。要么时间换空间,要么空间换时间。Choosing the right forking strategy and parallel execution settings can have substantial impact on the memory requirements and the execution time of your build system.
export MAVEN_OPTS=-Xmx512m这个参数的左右是,可以减少GC的压力
How do I interpret the "Final Memory" reported by Maven? "66M/188M" for
example, what does each of the figures mean?
In general the JVM only looks for more memory (up to -Xmx) when it needs
it, so consider that to be the peak requirement (not peak usage as the JVM
will always ask for more than usage on the basis that it needs room to
allocate objects)
If you run Maven with -Xmx2048m you will remove the pressing need for GC,
but the total memory might still only be 188MB as that may just have been
the largest amount of that 2048MB that the JVM needed at some point in time
during the build
参考:
1、http://grokbase.com/t/maven/dev/12bvc2s2bt/final-memory-reported-by-maven
2、https://stackoverflow.com/questions/45418317/how-to-find-the-peak-memory-usage-in-a-maven-build?answertab=votes#tab-top
3、http://maven.apache.org/surefire/maven-surefire-plugin/
使用maven时,如何修改JVM的配置参数;maven命令执行时到底消耗多少内存?的更多相关文章
- 深入理解JVM一配置参数
一.JVM配置参数分为三类参数: 1.跟踪参数 2.堆分配参数 3.栈分配参数 这三类参数分别用于跟踪监控JVM状态,分配堆内存以及分配栈内存. 二.跟踪参数 跟踪参数用于跟踪监控JVM,往往被开发人 ...
- jvm堆配置参数
1.-Xms初始堆大小默认物理内存的1/64(<1GB)(官方建议)2.-Xmx最大堆大小默认物理内存的1/4(<1GB)(官方建议),实际中建议不大于4GB3.一般建议设置 -Xms=- ...
- ThinkPHP5修改验证码的配置参数
当前使用的ThinkPHP的版本是5.0.24. 在模版试图中调用验证码生成函数:{:captcha_img()},或者<img src="{:captcha_src()}" ...
- 【Golang】创建有配置参数的结构体时,可选参数应该怎么传?
写在前面的话 Golang中构建结构体的时候,需要通过可选参数方式创建,我们怎么样设计一个灵活的API来初始化结构体呢. 让我们通过如下的代码片段,一步一步说明基于可选参数模式的灵活 API 怎么设计 ...
- Idea使用Maven搭建SpringMVC的HelloSpringMvc并配置插件Maven和Jetty
这篇博文只是纯粹的搭建一个SpringMVC的项目, 并不会涉及里面配置文件该写些什么. 只是纯粹的搭建一个初始的Hello SpringMVC的项目. 废话不多说,上图. 1. 打开IDEA 并且 ...
- Maven 工程下 Spring MVC 站点配置 (三) C3P0连接池与@Autowired的应用
Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 前两篇文章主要是对站点和数据库操作配置进行了演示,如果单 ...
- 大数据入门:Maven项目的创建及相关配置
目录 Maven项目的创建及相关配置 一.Maven的介绍 1.Maven是什么: 2.Maven作用: 3.Maven项目的目录结构: 4.Maven的三点坐标: 5.maven的pom文件: 6. ...
- Maven 工程下 Spring MVC 站点配置 (一)
最近,查找一些具体资料时,虽然会有很多,但是系统的却很少,尤其是对maven 下 spring mvc 站点搭建的配置,总是说的很多但让新手一目了然的步骤却少之又少. 对此闲暇时整理了一下,做了一套较 ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
随机推荐
- mysql执行语句汇总
插入select的数据 INSERT INTO `test1`( order_id, goods_id, goods_name, goods_sn, product_id, goods_number, ...
- How do I get started with Node.js
From: http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js Tutorials NodeSch ...
- servlet 生命周期 与 初始化
一. 生命周期 Servlet 通过调用 init () 方法进行初始化. Servlet 调用 service() 方法来处理客户端的请求. Servlet 通过调用 destroy() 方法终止( ...
- OpenFlow_tutorial_4_Create_a_Learning_Switch
一.环境搭建: 教程里提供的VM image需要梯子才能下载,好不容易下载下来,发现镜像很难用,各种安装问题,搞了好几天也解决不了.后来就自己搭环境,主要是安装Ryu. 1.首先下载相应的python ...
- swift不同地方 命名空间module iOS
Objective-C没有命名空间,为了避免冲突,Objective-C的类型一般都会加上两到三个字母的前缀,比如Apple保留的NS和UI前缀,各个系统框架的前缀,各个系统框架的前缀SK(Store ...
- crontab 每月最后一天执行命令
没有什么是解决不了的事情,如果有,只是我们的知识不够精通,学得不扎实 需求:有一个程序,需要在每个月的最后一天执行 例如:每个月的最后一天早上8:00 打印 dede 到 /tmp/test.txt ...
- 一个圆的移动 AE教程 速度曲线调节
AE里面速度的曲线调节 最终的小效果 两个关键点: 一:速度曲线调节 编辑速度图标,他的曲线是编辑速度的. 二:节点不要用贝塞尔曲线 编辑值图标,就是圆圈的x值y值的曲线.控制位置移动的. 选择一个节 ...
- [Luogu] P4910 帕秋莉的手环
题目背景 帕秋莉是蕾米莉亚很早结识的朋友,现在住在红魔馆地下的大图书馆里.不仅擅长许多魔法,还每天都会开发出新的魔法.只是身体比较弱,因为哮喘,会在咏唱符卡时遇到麻烦. 她所用的属性魔法,主要是生命和 ...
- 零基础入门学习Python(15)--格式化
前言 上节课我们介绍了字符串N多种奇葩方法的用法,但是我们唯独漏掉了format方法,那为何不把format方法和上节课的内容一起讲呢? 因为小甲鱼觉得format方法,跟今天的主题是如出一辙的,都是 ...
- 树莓派 -- 输入设备驱动 (key) 续1
测试 安装 input-utils pi@raspberrypi:~ $ sudo apt-get install input-utils Reading package lists... Done ...