微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java Runtime.getRuntime().freeMemory()问题

搜索并看到了一些线程,但没有一个解决我遇到的具体问题.

我试图使用Runtime.getRuntime().freeMemory(),Runtime.getRuntime().maxMemory()和Runtime.getRuntime().totalMemory()来监视我的内存使用情况.

在一次运行中,它表示在声明一个数组之前有2245273792个空闲字节,但程序在内存耗尽并在尝试声明一个1104674816字节的数组时崩溃:

Free memory (bytes): 2245273792 
    Maximum memory (bytes): 6710886400
    Total memory available to JVM (bytes): 5606211584
    OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000728280000,1104674816,0) Failed; error='Cannot allocate memory' (errno=12)

     There is insufficient memory for the Java Runtime Environment to continue.
     Native memory allocation (malloc) Failed to allocate 1104674816 bytes for committing reserved memory.
     An error report file with more information is saved as:

编辑:当分配不到一半的免费金额时,它怎么可能耗尽内存?耗尽内存对于这个程序来说非常糟糕;有没有办法可以保证我的分配不多于可用的?调用System.gc()会首先使可用/可用内存量更可靠吗?如果程序即将耗尽内存而不是让它崩溃,我可以抛出某种异常吗?

它崩溃的命令是:float [] [] A = new float [NUM_SEQ_TOTAL_P1] [NUM_SEQ_TOTAL_P2]; NUM_SEQ_TOTAL_P1 = 16009,NUM_SEQ_TOTAL_P2 = 16937.

编辑:根据评论者的要求粘贴错误日志的部分.

There is insufficient memory for the Java Runtime Environment to continue.
 Native memory allocation (malloc) Failed to allocate 1104674816 bytes for committing reserved memory.
 Possible reasons:
   The system is out of physical RAM or swap space
   In 32 bit mode,the process size limit was hit
 Possible solutions:
   Reduce memory load on the system
   Increase physical memory or swap space
   Check if swap backing store is full
   Use 64 bit Java on a 64 bit OS
   Decrease Java heap size (-Xmx/-xms)
   Decrease number of Java threads
   Decrease Java thread stack sizes (-Xss)
   Set larger code cache with -XX:ReservedCodeCacheSize=
 This output file may be truncated or incomplete.

  Out of Memory Error (os_linux.cpp:2769),pid=14802,tid=140058464728832

 JRE version: OpenJDK Runtime Environment (7.0_55-b14) (build 1.7.0_55-b14)
 Java VM: OpenJDK 64-Bit Server VM (24.51-b03 mixed mode linux-amd64 compressed oops)
 Failed to write core dump. Core dumps have been disabled. To enable core dumping,try "ulimit -c unlimited" before starting Java again


---------------  T H R E A D  ---------------

Current thread (0x00007f61f0071000):  VMThread [stack: 0x00007f61e6f9b000,0x00007f61e709c000] [id=14806]

Stack: [0x00007f61e6f9b000,0x00007f61e709c000],sp=0x00007f61e709a470,free space=1021k
Native frames: (J=compiled Java code,j=interpreted,Vv=VM code,C=native code)
V  [libjvm.so+0x917b35]  VMError::report_and_die()+0x175
V  [libjvm.so+0x468144]  report_vm_out_of_memory(char const*,int,unsigned long,char const*)+0x74
V  [libjvm.so+0x79a81b]  os::pd_commit_memory(char*,bool)+0x20b
V  [libjvm.so+0x7950df]  os::commit_memory(char*,bool)+0x1f
V  [libjvm.so+0x8053cb]  PSVirtualSpace::expand_by(unsigned long)+0x5b
V  [libjvm.so+0x7f519b]  PSOldGen::expand_by(unsigned long) [clone .part.80]+0x1b
V  [libjvm.so+0x7f535b]  PSOldGen::expand(unsigned long) [clone .part.81]+0xdb
V  [libjvm.so+0x7f6288]  PSOldGen::resize(unsigned long)+0x188
V  [libjvm.so+0x7fd2e3]  PSParallelCompact::invoke_no_policy(bool)+0x9c3
V  [libjvm.so+0x80350d]  PSScavenge::invoke()+0x17d
V  [libjvm.so+0x7b9b33]  ParallelScavengeHeap::Failed_mem_allocate(unsigned long)+0x63
V  [libjvm.so+0x919544]  VM_ParallelGCFailedAllocation::doit()+0x84
V  [libjvm.so+0x91d9e7]  VM_Operation::evaluate()+0x47
V  [libjvm.so+0x91c3b8]  VMThread::evaluate_operation(VM_Operation*)+0x318
V  [libjvm.so+0x91c85a]  VMThread::loop()+0x25a
V  [libjvm.so+0x91cc62]  VMThread::run()+0x72
V  [libjvm.so+0x7974e2]  java_start(Thread*)+0xf2

VM_Operation (0x00007f61f8b6b2e0): ParallelGCFailedAllocation,mode: safepoint,requested by thread 0x00007f61f000a000


---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x00007f61f00a1800 JavaThread "Service Thread" daemon [_thread_blocked,id=14812,stack(0x00007f61e680c000,0x00007f61e690d000)]
  0x00007f61f009f000 JavaThread "C2 CompilerThread1" daemon [_thread_blocked,id=14811,stack(0x00007f61e690d000,0x00007f61e6a0e000)]
  0x00007f61f009c000 JavaThread "C2 CompilerThread0" daemon [_thread_blocked,id=14810,stack(0x00007f61e6a0e000,0x00007f61e6b0f000)]
  0x00007f61f009a000 JavaThread "Signal dispatcher" daemon [_thread_blocked,id=14809,stack(0x00007f61e6b0f000,0x00007f61e6c10000)]
  0x00007f61f0077800 JavaThread "Finalizer" daemon [_thread_blocked,id=14808,stack(0x00007f61e6d99000,0x00007f61e6e9a000)]
  0x00007f61f0075800 JavaThread "Reference Handler" daemon [_thread_blocked,id=14807,stack(0x00007f61e6e9a000,0x00007f61e6f9b000)]
  0x00007f61f000a000 JavaThread "main" [_thread_blocked,id=14803,stack(0x00007f61f8a6c000,0x00007f61f8b6d000)]

Other Threads:
=>0x00007f61f0071000 VMThread [stack: 0x00007f61e6f9b000,0x00007f61e709c000] [id=14806]
  0x00007f61f00ac000 WatcherThread [stack: 0x00007f61e670b000,0x00007f61e680c000] [id=14813]

VM state:at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread:  ([mutex/lock_event])
[0x00007f61f0005ea0] ExpandHeap_lock - owner thread: 0x00007f61f0071000
[0x00007f61f0006520] Threads_lock - owner thread: 0x00007f61f0071000
[0x00007f61f0006a20] Heap_lock - owner thread: 0x00007f61f000a000

Heap
 PSYoungGen      total 1638400K,used 205885K [0x000000076a000000,0x0000000800000000,0x0000000800000000)
  eden space 819200K,0% used [0x000000076a000000,0x000000076a000000,0x000000079c000000)
  from space 819200K,25% used [0x00000007ce000000,0x00000007da90f408,0x0000000800000000)
  to   space 819200K,0% used [0x000000079c000000,0x000000079c000000,0x00000007ce000000)
 ParOldGen       total 3836416K,used 3836085K [0x000000063e000000,0x0000000728280000,0x000000076a000000)
  object space 3836416K,99% used [0x000000063e000000,0x000000072822d6b8,0x0000000728280000)
 PSPermGen       total 21504K,used 5549K [0x0000000633a00000,0x0000000634f00000,0x000000063e000000)
  object space 21504K,25% used [0x0000000633a00000,0x0000000633f6b518,0x0000000634f00000)

Card table byte_map: [0x00007f61f487d000,0x00007f61f56e1000] byte_map_base: 0x00007f61f16e0000

Polling page: 0x00007f61f8b7d000

Code Cache  [0x00007f61ed000000,0x00007f61ed270000,0x00007f61f0000000)
 total_blobs=653 nmethods=373 adapters=234 free_code_cache=46884Kb largest_free_block=47925312

Compilation events (10 events):
Event: 697.652 Thread 0x00007f61f009c000  394             playingAgent.tartanian7.PlayerAction::access$0 (5 bytes)
Event: 697.652 Thread 0x00007f61f009c000 nmethod 394 0x00007f61ed1f0150 code [0x00007f61ed1f0280,0x00007f61ed1f02d8]
Event: 729.745 Thread 0x00007f61f009f000  395             java.nio.Buffer::remaining (10 bytes)
Event: 729.745 Thread 0x00007f61f009f000 nmethod 395 0x00007f61ed1eff50 code [0x00007f61ed1f0080,0x00007f61ed1f00d8]
Event: 921.702 Thread 0x00007f61f009c000  396             playingAgent.riverSolver.DoylesGameRiver2::getRiverStrategy (451 bytes)
Event: 921.710 Thread 0x00007f61f009f000  397             playingAgent.tartanian7.PokerUtils::countRoundDelimiters (42 bytes)
Event: 921.714 Thread 0x00007f61f009f000 nmethod 397 0x00007f61ed1f8350 code [0x00007f61ed1f84a0,0x00007f61ed1f86b8]
Event: 921.720 Thread 0x00007f61f009f000  398             java.lang.String::startsWith (7 bytes)
Event: 921.724 Thread 0x00007f61f009f000 nmethod 398 0x00007f61ed108010 code [0x00007f61ed108160,0x00007f61ed108338]
Event: 922.063 Thread 0x00007f61f009c000 nmethod 396 0x00007f61ed23db90 code [0x00007f61ed23e6e0,0x00007f61ed244d78]

GC Heap History (10 events):
Event: 922.966 GC heap after
Heap after GC invocations=137 (full 23):
 PSYoungGen      total 1638400K,used 280234K [0x000000076a000000,34% used [0x00000007ce000000,0x00000007df1aa930,used 2558981K [0x000000063e000000,66% used [0x000000063e000000,0x00000006da301608,used 5548K [0x0000000633a00000,0x0000000633f6b068,0x0000000634f00000)
}
Event: 941.023 GC heap before
{Heap before GC invocations=138 (full 23):
 PSYoungGen      total 1638400K,used 1099434K [0x000000076a000000,100% used [0x000000076a000000,0x0000000633f6b258,0x0000000634f00000)
Event: 941.125 GC heap after
Heap after GC invocations=138 (full 23):
 PSYoungGen      total 1638400K,used 463312K [0x000000076a000000,56% used [0x000000079c000000,0x00000007b8474158,0x00000007ce000000)
  to   space 819200K,0% used [0x00000007ce000000,0x00000007ce000000,0x0000000800000000)
 ParOldGen       total 3836416K,used 2559185K [0x000000063e000000,0x00000006da334680,0x0000000634f00000)
}
Event: 971.960 GC heap before
{Heap before GC invocations=139 (full 23):
 PSYoungGen      total 1638400K,used 1282512K [0x000000076a000000,0x0000000634f00000)
Event: 971.996 GC heap after
Heap after GC invocations=139 (full 23):
 PSYoungGen      total 1638400K,used 130974K [0x000000076a000000,15% used [0x00000007ce000000,0x00000007d5fe7b78,used 2559301K [0x000000063e000000,0x00000006da351710,0x0000000634f00000)
}
Event: 972.098 GC heap before
{Heap before GC invocations=140 (full 23):
 PSYoungGen      total 1638400K,used 950174K [0x000000076a000000,0x0000000634f00000)
Event: 972.296 GC heap after
Heap after GC invocations=140 (full 23):
 PSYoungGen      total 1638400K,used 817538K [0x000000076a000000,99% used [0x000000079c000000,0x00000007cde60b18,used 2689286K [0x000000063e000000,70% used [0x000000063e000000,0x00000006e2241b50,0x0000000634f00000)
}
Event: 972.397 GC heap before
{Heap before GC invocations=141 (full 23):
 PSYoungGen      total 1638400K,used 1636738K [0x000000076a000000,0x0000000634f00000)
Event: 972.791 GC heap after
Heap after GC invocations=141 (full 23):
 PSYoungGen      total 1638400K,used 817782K [0x000000076a000000,99% used [0x00000007ce000000,0x00000007ffe9d998,used 3506825K [0x000000063e000000,91% used [0x000000063e000000,0x00000007140a2668,0x0000000634f00000)
}
Event: 972.791 GC heap before
{Heap before GC invocations=142 (full 24):
 PSYoungGen      total 1638400K,0x0000000634f00000)

Deoptimization events (10 events):
Event: 388.757 Thread 0x00007f61f000a000 Uncommon trap: reason=array_check action=maybe_recompile pc=0x00007f61ed200cd4 method=java.util.ComparableTimsort.mergeHi(IIII)V @ 234
Event: 425.767 Thread 0x00007f61f000a000 Uncommon trap: reason=array_check action=maybe_recompile pc=0x00007f61ed226240 method=java.util.ComparableTimsort.mergeHi(IIII)V @ 642
Event: 425.768 Thread 0x00007f61f000a000 Uncommon trap: reason=array_check action=maybe_recompile pc=0x00007f61ed226240 method=java.util.ComparableTimsort.mergeHi(IIII)V @ 642
Event: 451.013 Thread 0x00007f61f000a000 Uncommon trap: reason=array_check action=maybe_recompile pc=0x00007f61ed229d20 method=java.util.ComparableTimsort.mergeHi(IIII)V @ 404
Event: 451.013 Thread 0x00007f61f000a000 Uncommon trap: reason=array_check action=maybe_recompile pc=0x00007f61ed229d20 method=java.util.ComparableTimsort.mergeHi(IIII)V @ 404
Event: 518.661 Thread 0x00007f61f000a000 Uncommon trap: reason=array_check action=maybe_recompile pc=0x00007f61ed22c5c8 method=java.util.ComparableTimsort.mergeHi(IIII)V @ 509
Event: 518.661 Thread 0x00007f61f000a000 Uncommon trap: reason=array_check action=maybe_recompile pc=0x00007f61ed22c5c8 method=java.util.ComparableTimsort.mergeHi(IIII)V @ 509
Event: 576.223 Thread 0x00007f61f000a000 Uncommon trap: reason=array_check action=maybe_recompile pc=0x00007f61ed22399c method=java.util.ComparableTimsort.mergeLo(IIII)V @ 129
Event: 626.762 Thread 0x00007f61f000a000 Uncommon trap: reason=array_check action=maybe_recompile pc=0x00007f61ed22399c method=java.util.ComparableTimsort.mergeLo(IIII)V @ 129
Event: 805.388 Thread 0x00007f61f000a000 Uncommon trap: reason=array_check action=maybe_recompile pc=0x00007f61ed232884 method=java.util.ComparableTimsort.mergeHi(IIII)V @ 160

Internal exceptions (10 events):
Event: 95.779 Thread 0x00007f61f000a000 Threw 0x000000076e72f270 at /build/buildd/openjdk-7-7u55-2.4.7/build/openjdk/hotspot/src/share/vm/prims/jvm.cpp:1244
Event: 95.780 Thread 0x00007f61f000a000 Threw 0x000000076e732680 at /build/buildd/openjdk-7-7u55-2.4.7/build/openjdk/hotspot/src/share/vm/prims/jvm.cpp:1244
Event: 95.866 Thread 0x00007f61f000a000 Threw 0x000000076ea384b0 at /build/buildd/openjdk-7-7u55-2.4.7/build/openjdk/hotspot/src/share/vm/prims/jvm.cpp:1244
Event: 118.330 Thread 0x00007f61f000a000 Threw 0x0000000770f9b6f0 at /build/buildd/openjdk-7-7u55-2.4.7/build/openjdk/hotspot/src/share/vm/prims/jvm.cpp:1244
Event: 118.332 Thread 0x00007f61f000a000 Threw 0x0000000770fba0d8 at /build/buildd/openjdk-7-7u55-2.4.7/build/openjdk/hotspot/src/share/vm/prims/jvm.cpp:1244
Event: 118.528 Thread 0x00007f61f000a000 Threw 0x00000007713a1fc0 at /build/buildd/openjdk-7-7u55-2.4.7/build/openjdk/hotspot/src/share/vm/prims/jvm.cpp:1244
Event: 118.529 Thread 0x00007f61f000a000 Threw 0x00000007713a9628 at /build/buildd/openjdk-7-7u55-2.4.7/build/openjdk/hotspot/src/share/vm/prims/jvm.cpp:1244
Event: 118.533 Thread 0x00007f61f000a000 Threw 0x00000007713bea90 at /build/buildd/openjdk-7-7u55-2.4.7/build/openjdk/hotspot/src/share/vm/prims/jvm.cpp:1244
Event: 118.545 Thread 0x00007f61f000a000 Threw 0x00000007713e8238 at /build/buildd/openjdk-7-7u55-2.4.7/build/openjdk/hotspot/src/share/vm/prims/jvm.cpp:1244
Event: 118.546 Thread 0x00007f61f000a000 Threw 0x00000007713eb0f8 at /build/buildd/openjdk-7-7u55-2.4.7/build/openjdk/hotspot/src/share/vm/prims/jvm.cpp:1244

Events (10 events):
Event: 972.069 Executing VM operation: RevokeBias done
Event: 972.069 Executing VM operation: RevokeBias
Event: 972.069 Executing VM operation: RevokeBias done
Event: 972.069 Executing VM operation: RevokeBias
Event: 972.069 Executing VM operation: RevokeBias done
Event: 972.069 Executing VM operation: RevokeBias
Event: 972.069 Executing VM operation: RevokeBias done
Event: 972.069 Executing VM operation: RevokeBias
Event: 972.296 Executing VM operation: RevokeBias done
Event: 972.296 Executing VM operation: RevokeBias

Dynamic libraries:[omitted]


VM Arguments:
jvm_args: -Xmx7200m 
java_command: [omitted]
Launcher Type: SUN_STANDARD

Environment Variables: [omitted]

Signal Handlers:
SIGSEGV: [libjvm.so+0x918620],sa_mask[0]=0x7ffbfeff,sa_flags=0x10000004
SIGBUS: [libjvm.so+0x918620],sa_flags=0x10000004
SIGFPE: [libjvm.so+0x795e30],sa_flags=0x10000004
SIGPIPE: [libjvm.so+0x795e30],sa_flags=0x10000004
SIGXFSZ: [libjvm.so+0x795e30],sa_flags=0x10000004
SIGILL: [libjvm.so+0x795e30],sa_flags=0x10000004
SIGUSR1: SIG_DFL,sa_mask[0]=0x00000000,sa_flags=0x00000000
SIGUSR2: [libjvm.so+0x795cd0],sa_flags=0x10000004
SIGHUP: [libjvm.so+0x795eb0],sa_flags=0x10000004
SIGINT: [libjvm.so+0x795eb0],sa_flags=0x10000004
SIGTERM: [libjvm.so+0x795eb0],sa_flags=0x10000004
SIGQUIT: [libjvm.so+0x795eb0],sa_flags=0x10000004


---------------  S Y S T E M  ---------------

OS:Ubuntu 14.04 (trusty)
uname:Linux 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64
libc:glibc 2.19 NPTL 2.19 
rlimit: STACK 8192k,CORE 0k,NPROC 59937,NOFILE 4096,AS infinity
load average:1.80 1.67 1.50

/proc/meminfo:
MemTotal:        7693824 kB
MemFree:          634168 kB
Buffers:           12600 kB
Cached:           290332 kB
SwapCached:            0 kB
Active:          6799656 kB
Inactive:         151160 kB
Active(anon):    6647936 kB
Inactive(anon):      200 kB
Active(file):     151720 kB
Inactive(file):   150960 kB
Unevictable:           0 kB
mlocked:               0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                36 kB
Writeback:             0 kB
AnonPages:       6647924 kB
Mapped:            18848 kB
Shmem:               236 kB
Slab:              41348 kB
SReclaimable:      32536 kB
SUnreclaim:         8812 kB
KernelStack:         848 kB
PageTables:        17104 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     3846912 kB
Committed_AS:    6744548 kB
VmallocTotal:   34359738367 kB
VmallocUsed:       14888 kB
VmallocChunk:   34359719127 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:     7872512 kB
DirectMap2M:           0 kB


cpu:total 2 (16 cores per cpu,2 threads per core) family 6 model 62 stepping 4,cmov,cx8,fxsr,mmx,sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,aes,erms,ht,tsc

/proc/cpuinfo:
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 62
model name  : Intel(R) Xeon(R) cpu E5-2670 v2 @ 2.50GHz
stepping    : 4
microcode   : 0x415
cpu MHz     : 2500.058
cache size  : 25600 KB
physical id : 1
siblings    : 2
core id     : 0
cpu cores   : 1
apicid      : 32
initial apicid  : 32
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu de tsc msr pae cx8 apic sep cmov pat clflush mmx fxsr sse sse2 ss ht syscall nx lm constant_tsc rep_good nopl pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt tsc_deadline_timer aes rdrand hypervisor lahf_lm ida arat epb pln pts dtherm fsgsbase erms
bogomips    : 5000.11
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical,48 bits virtual
power management:

processor   : 1
vendor_id   : GenuineIntel
cpu family  : 6
model       : 62
model name  : Intel(R) Xeon(R) cpu E5-2670 v2 @ 2.50GHz
stepping    : 4
microcode   : 0x415
cpu MHz     : 2500.058
cache size  : 25600 KB
physical id : 1
siblings    : 2
core id     : 0
cpu cores   : 1
apicid      : 32
initial apicid  : 32
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu de tsc msr pae cx8 apic sep cmov pat clflush mmx fxsr sse sse2 ss ht syscall nx lm constant_tsc rep_good nopl pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt tsc_deadline_timer aes rdrand hypervisor lahf_lm ida arat epb pln pts dtherm fsgsbase erms
bogomips    : 5000.11
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical,48 bits virtual
power management:



Memory: 4k page,physical 7693824k(634168k free),swap 0k(0k free)

vm_info: OpenJDK 64-Bit Server VM (24.51-b03) for linux-amd64 JRE (1.7.0_55-b14),built on Apr 17 2014 20:08:07 by "buildd" with gcc 4.8.2

time: Thu May 29 20:30:58 2014
elapsed time: 979 seconds

解决方法

由于运行时报告2.245gig free,并在需要1.104gig时报告失败,因此请求应该成功是合乎逻辑的.但是当你需要1.104gig时,你真的需要一块连续的内存块.当连续块不可用时,JVM会报告OutOfMemory异常.

对堆碎片进行一些搜索.堆碎片是你的根本问题.

一种可能的解决方案是使用nio字节缓冲区和allocateDirect.
内存分配在JVM堆空间之外.

来自JavaDoc:

A direct byte buffer may be created by invoking the allocateDirect factory method of this class. The buffers returned by this method typically have somewhat higher allocation and deallocation costs than non-direct buffers. The contents of direct buffers may reside outside of the normal garbage-collected heap,and so their impact upon the memory footprint of an application might not be obvIoUs. It is therefore recommended that direct buffers be allocated primarily for large,long-lived buffers that are subject to the underlying system’s native I/O operations. In general it is best to allocate direct buffers only when they yield a measureable gain in program performance.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐