Hi
I got runtime error as "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ". How can I solve this problem? Can anyone explain me why this problem occurs? Thanks in advance.
Hi
I got runtime error as "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ". How can I solve this problem? Can anyone explain me why this problem occurs? Thanks in advance.
Good initial pointers from , , , and . The next step is evidence-driven diagnosis rather than guessing. Deterministic data to collect when the "java.lang.OutOfMemoryError: Java heap space" occurs: a heap dump taken at the failure, a GC log covering the run, and one or more thread dumps. These artifacts make it possible to spot what objects retain most memory, whether GC is keeping objects alive, and whether many threads or native allocations are involved.
Common useful commands to produce artifacts (run from a matching JDK on the host where the JVM runs):
# create a binary heap dump
jmap -dump:live,format=b,file=heap.hprof <pid>
# get a JVM heap summary
jmap -heap <pid>
# capture a thread dump
jstack -F <pid> > threaddump.txt
# sample GC activity every second
jstat -gc <pid> 1000 Open the heap file with a heap analyzer (VisualVM, Eclipse Memory Analyzer) and inspect retained sizes / dominator tree to find the biggest retainers. Typical root causes not fully covered above include unbounded caches or maps, listeners or callbacks never removed, ThreadLocal values, classloader leaks in container redeploys, and large direct buffers or native allocations. Fixes generally mean removing unintended long-lived references, replacing unbounded caches with bounded caches, using weak/soft references where appropriate, and switching from bulk in-memory reads to streaming processing for very large inputs. Gathering the dumps and GC data first makes it clear whether the problem is a true leak or transient memory pressure.
Jump to Post— server_crash 64I would guess you're using recursion...maybe?
Recursion can cause this, and so can many other things. Explain what your program is doing, please.
Jump to Post— jwenting 1,905Recursion causes StackOverflow, not often OutOfMemory :)
You're probably trying to read in some very large file or otherwise reserving a lot of memory.
Figure out where you are doing that and try to find a way around it.
I would guess you're using recursion...maybe?
Recursion can cause this, and so can many other things. Explain what your program is doing, please.
Recursion causes StackOverflow, not often OutOfMemory :)
You're probably trying to read in some very large file or otherwise reserving a lot of memory.
Figure out where you are doing that and try to find a way around it.
Just adding to what the two above stated...
Look to see if you're creating lots of new objects. Memory Exceptions ocure when your java application uses all of its allowed memory. I believe the default setting is 64megs. My guess is that inside a loop (if, while, or a recursive loop) there are new objects being created. While this isn't bad, it could be the source of the problem.
Regards,
Nate
Hi everyone,
I thnk you will have to use -Xms and -Xmx as such
java -Xms<initial heap size> -Xmx<maximum heap size> You best bet is make your own mini application launcher to get around this problem.
Richard West
Unless there's a definite limit to the amount of memory the application will claim (so no errors or unforeseen conditions causing infinite loops for example) that will only delay the inevitable :)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.