用mvn test运行test case时,报“java.lang.OutOfMemoryError: PermGen space”

本文介绍了解决Maven项目中使用JUnit测试时遇到的PermGenSpace内存溢出错误的方法。通过在pom.xml文件中配置maven-surefire-plugin插件来增加分配给JUnit测试的内存,成功解决了问题。
部署运行你感兴趣的模型镜像

在maven project中,test case的运行是少不了的。随着项目复杂度的增加,相应的test cases的数量也会随之增加。

 

突然有一天,用eclipse 内置的m2e插件Run as -> Maven test运行test cases,跑着跑着就报错如下:

“java.lang.OutOfMemoryError: PermGen space” error

 

Google了各种办法之后没有解决:

1.add 环境变量:

  Windows: set MAVEN_OPTS=-Xmx512m -XX:MaxPermSize=1024m

  Linux: export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=1024m"

2. adding -XX:MaxPermSize=1024M and -XX:PermSize=512M after -vmargs in the eclipse.ini

3. adding -XX:MaxPermSize=1024m and -XX:PermSize=512m to my Tomcat launch configuration

4. add VM arguments on my default JRE as following:Window -> Preferences -> Java -> Installed        JREs. Select the checked JRE/JDK and click edit, and set Default VM Arguments = -Xms128m -    Xmx1024m -XX:MaxPermSize=1024m

 

以上四种办法试了个遍,不知道是我打开的方式不对还是怎么回事儿,没有一个起作用,最后按照一篇博文(https://eureka.ykyuen.info/2010/03/02/maven-set-java-heap-memory-for-junit-in-maven-surefire-plugin/)中的设置,在pom.xml中添加参数如下:

< plugin >
     < groupId >org.apache.maven.plugins</ groupId >
     < artifactId >maven-surefire-plugin</ artifactId >
     < version >2.18.1</ version >
     < configuration >
         < argLine >-XX:MaxPermSize=1024m</ argLine >
     </ configuration >
</ plugin >

设置完了之后,再次运行mvn test or Run as ->Maven test,顺利跑完整个test。

博文中写到:

JUnit tests ignore the environment variable MAVEN_OPTS and the JVM settings in Run Configurations.

So if u have OutOfMemoryError when running unit test in Maven, you have to set the Java Heap Memory in the maven-surefire-plugin inside the pom.xml.

在Stackoverflow(http://stackoverflow.com/questions/2819853/setting-java-heap-space-under-maven-2-on-windows) 有个post写到:

Basically the JUnits fork off into their own environment and ignore the settings in MAVEN_OPTS. You need to configure surefire in your pom to add more memory for the JUnits.

 

参考链接:

http://stackoverflow.com/questions/3101128/java-lang-outofmemoryerror-permgen-space-in-maven-build

http://stackoverflow.com/questions/24708077/java-lang-outofmemoryerror-permgen-space-after-convering-project-to-maven

http://stackoverflow.com/questions/2819853/setting-java-heap-space-under-maven-2-on-windows

https://eureka.ykyuen.info/2010/03/02/maven-set-java-heap-memory-for-junit-in-maven-surefire-plugin/

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

### 解决IntelliJ IDEA启动项目`java.lang.OutOfMemoryError: GC overhead limit exceeded`错误 当遇到`java.lang.OutOfMemoryError: GC overhead limit exceeded`错误,表明垃圾回收器花费过多间尝试释放内存但仍无法完成清理工作。这通常意味着应用程序消耗了大量堆空间,并且剩余可用资源不足以支持其继续运行。 #### 调整IDEA JVM参数配置 为了缓解此问题,在IntelliJ IDEA中可以通过调整虚拟机选项来增加分配给项目的最大堆大小和其他相关设置: 1. 打开编辑自定义VM选项对话框: - Windows/Linux:点击 `Help | Edit Custom VM Options...` - macOS:点击 `IntelliJ IDEA | Preferences | Appearance & Behavior | System Settings | Memory Settings` 2. 修改文件中的参数以适应更大的内存需求。例如,可以将初始和最大堆尺寸设得更大一些[^1]: ```properties -Xms1024m # 设置最小堆大小为1GB -Xmx4096m # 设置最大堆大小为4GB -XX:ReservedCodeCacheSize=512m # 增加代码缓存预留量至512MB ``` 3. 如果仍然存在问题,则考虑进一步优化构建过程或减少不必要的依赖项加载,从而降低整体内存占用。 #### 配置Maven/Gradle构建工具的内存限制 对于基于Maven或Gradle管理依赖关系的应用程序来说,还需要确保这些构建工具有足够的内存来进行操作。可以在相应的配置文件里指定更高的内存限额: ##### Maven: 修改`.mvn/jvm.config` 文件 (如果不存在则创建),并加入以下内容[^4]: ```bash -Xms512m -Xmx2048m -XX:MaxPermSize=512M -XX:+CMSClassUnloadingEnabled ``` ##### Gradle: 编辑`gradle.properties` 文件, 添加如下属性: ```properties org.gradle.jvmargs=-Dfile.encoding=UTF-8 ``` 以上措施有助于提高开发环境下的性能表现以及解决问题的可能性。然而需要注意的是,具体数值应根据实际硬件条件和个人偏好适当调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值