【问题标题】:Java Heap Space Issue while running Junits运行 Junits 时出现 Java 堆空间问题
【发布时间】:2016-11-24 06:39:28
【问题描述】:

我一直致力于将应用程序从 Spring 3 升级到 4 和 Hibernate 3 到 4。现在的确切版本: 弹簧:4.1.9 休眠:4.3.7

由于依赖关系,我还必须升级 Junit 版本,我们现在使用的是 4.12。

自从升级以来,我在大多数单元测试中都遇到了 java 堆空间异常:

net.sf.ehcache.CacheException: java.lang.OutOfMemoryError: Java heap space
    at net.sf.ehcache.CacheManager.init(CacheManager.java:361)
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:237)
    at net.sf.ehcache.CacheManager.create(CacheManager.java:856)
    at net.sf.ehcache.CacheManager.create(CacheManager.java:743)
    at net.sf.ehcache.CacheManager.getInstance(CacheManager.java:758)
    at com.rsa.csd.RsaBaseTestCase.setUp(RsaBaseTestCase.java:144)
    at junit.framework.TestCase.runBare(TestCase.java:139)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:234)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:133)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:114)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:188)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:166)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:101)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)
Caused by: java.lang.OutOfMemoryError: Java heap space
    at net.sf.ehcache.store.chm.ConcurrentHashMap$HashEntry.newArray(ConcurrentHashMap.java:226)
    at net.sf.ehcache.store.chm.ConcurrentHashMap$Segment.<init>(ConcurrentHashMap.java:316)
    at net.sf.ehcache.store.chm.SelectableConcurrentHashMap$MemoryStoreSegment.<init>(SelectableConcurrentHashMap.java:270)
    at net.sf.ehcache.store.chm.SelectableConcurrentHashMap$MemoryStoreSegment.<init>(SelectableConcurrentHashMap.java:262)
    at net.sf.ehcache.store.chm.SelectableConcurrentHashMap.createSegment(SelectableConcurrentHashMap.java:245)
    at net.sf.ehcache.store.chm.ConcurrentHashMap.<init>(ConcurrentHashMap.java:634)
    at net.sf.ehcache.store.chm.SelectableConcurrentHashMap.<init>(SelectableConcurrentHashMap.java:52)
    at net.sf.ehcache.store.MemoryStore.<init>(MemoryStore.java:146)
    at net.sf.ehcache.store.NotifyingMemoryStore.<init>(NotifyingMemoryStore.java:37)
    at net.sf.ehcache.store.NotifyingMemoryStore.create(NotifyingMemoryStore.java:49)
    at net.sf.ehcache.store.MemoryOnlyStore.create(MemoryOnlyStore.java:69)
    at net.sf.ehcache.Cache.initialise(Cache.java:1105)
    at net.sf.ehcache.CacheManager.initializeEhcache(CacheManager.java:1128)
    at net.sf.ehcache.CacheManager.addCacheNoCheck(CacheManager.java:1159)
    at net.sf.ehcache.CacheManager.addConfiguredCaches(CacheManager.java:708)
    at net.sf.ehcache.CacheManager.doInit(CacheManager.java:426)
    at net.sf.ehcache.CacheManager.init(CacheManager.java:352)
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:237)
    at net.sf.ehcache.CacheManager.create(CacheManager.java:856)
    at net.sf.ehcache.CacheManager.create(CacheManager.java:743)
    at net.sf.ehcache.CacheManager.getInstance(CacheManager.java:758)
    at com.rsa.csd.RsaBaseTestCase.setUp(RsaBaseTestCase.java:144)
    at junit.framework.TestCase.runBare(TestCase.java:139)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:234)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:133)

我尝试过在线搜索,但没有找到任何解决方案。

请让我知道我需要发布的任何配置文件。

【问题讨论】:

  • 正在使用哪个版本的 ehcache?我会更新以使用最新的 2.x 版本。那么,你有ehcache.xml 文件吗?
  • 我使用的是 ehcache 2.5。目前无法访问办公室笔记本电脑。所以将尽快共享文件

标签: java spring hibernate junit ehcache


【解决方案1】:

您转到运行配置,然后在参数下,您可以设置vm arguments。在参数选项卡中,将 VM 参数设置为 "-Xms64m -Xmx256m" 或更高取决于您的系统配置。

【讨论】:

  • 试过了,我什至将它设置为高达 4 GB,仍然得到相同的异常。
  • 你用的是什么ide?如果您使用的是 eclipse,请在 eclipse.ini 中增加分配给 eclipse 的内存。
  • 即使我从命令提示符运行测试用例,我也会遇到这个问题,所以不确定这是否是与 Eclipse 相关的问题。
  • 那么你将不得不看看堆哑巴。使用 VisualVM 读取堆转储文件并诊断问题。 VisualVM 是一个位于 JDK_HOME/bin/jvisualvm 中的程序。堆转储文件包含有关应用程序内存使用情况的所有信息。它允许您在堆中导航并查看哪些对象使用的内存最多,以及哪些引用阻止垃圾收集器回收内存。
  • 肯定会尝试并返回。谢谢
猜你喜欢
  • 2013-03-30
  • 1970-01-01
  • 2013-04-17
  • 2016-06-15
  • 2015-09-12
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
相关资源
最近更新 更多