首页 > 解决方案 > IllegalArgumentException - 当主机仅包含 0B 物理内存时尝试分配 256MB 内存

问题描述

上下文:将 java 升级到 java 1.8.0_281 后,应用程序无法启动。在 java 代码中,我试图为堆外缓存分配一些内存,定义如下:

  <cache-template name="myCache">
    <expiry>
      <none/>
    </expiry>
   <resources>
     <heap unit="entries">100000</heap>
     <offheap unit="MB">256</offheap>
     <disk unit="MB">1000</disk>
   </resources> 
  </cache-template>

但我在启动时收到以下错误:

Caused by: java.lang.IllegalStateException: Cache 'myCache' creation in EhcacheManager failed.
    at org.ehcache.core.EhcacheManager.createCache(EhcacheManager.java:302) 
    at org.ehcache.core.EhcacheManager.createCache(EhcacheManager.java:252) 
    at org.ehcache.jsr107.Eh107CacheManager.createCache(Eh107CacheManager.java:201)

(......这里有更多堆栈跟踪......如果需要,我会提供它)

Caused by: org.ehcache.StateTransitionException: Attempting to allocate 256MB of memory when the host only contains 0B of physical memory
    at org.ehcache.core.StatusTransitioner$Transition.succeeded(StatusTransitioner.java:209) 
    at org.ehcache.core.EhcacheBase.init(EhcacheBase.java:569) 
    at org.ehcache.core.EhcacheManager.createCache(EhcacheManager.java:275) 
    at org.ehcache.core.EhcacheManager.createCache(EhcacheManager.java:252)
    at org.ehcache.jsr107.Eh107CacheManager.createCache(Eh107CacheManager.java:201) ~[ehcache-3.9.0.jar:3.9.0 97b6a73ef8644dc028234f166c90f951f60f099b]

(……)

看到 0B 物理内存很奇怪,因为根据我的检查,运行应用程序的 linux 服务器有足够的内存。我读到,在大多数情况下,交换的内存连接到堆外内存。所以我解决这个问题的第一个想法是增加交换内存,因为一开始它是0。但是这样做并重新启动服务器后,错误仍然存​​在。

使用的命令:free
结果:

             total       used       free     shared    buffers     cached
Mem:      30221268   27787476    2433792     546780     344464   22443644
-/+ buffers/cache:    4999368   25221900
Swap:      1023996          0    1023996

另一个提到,在另一个使用相同 java 版本的 linux 服务器上,应用程序运行良好并且启动良好。

我错过了什么吗?为什么我会收到这个错误?我还能检查什么?

欢迎任何帮助。

标签: javalinuxmemory-managementehcache

解决方案


推荐阅读