Exception in thread "main" java.lang.OutOfMemoryError, How to find and fix? [duplicate]
I'm trying to programming a crossword creator. using a given dictionary txt file and a given pattern txt file. The basic idea is using DFS algorithm. the problem begin when the dictionary file is v-e-r-y big (about 50000 words). then i recive the :
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
i know that there is a part in my program that wastes memory, but i don't know where it is, how to find it and how to fix it
Does it really waste memory ? If you're loading a sizeable dictionary, then you may simply want to increase the JVM memory settings (the JVM has a maximum memory allocation - dependent on your platform, and configurable).
e.g.
$ java -Xmx512m ....
would increase the maximum memory allocation of the JVM to 512m.
If you think you have a memory leak (garbage collection not kicking in due to references not being released) then a profiler such as YourKit may be of use. Note that this isn't free, but the trial version may help you solve your problem.
To solve this problem ( in linux based os ) do following
1) increase memory (so that this problem don't come frequently ) by configuring "domain.xml" in
/glassfish/domain/domain1/config
search for
<jvm-options>-XX:MaxPermSize=
set it to higher value eg- 198m or 256m
2) kill the glassfish process to free the port on which it was running ( in my case it was 8686) open terminal (in linux based os) and type -
sudo netstat -npl | grep 8686
this will result in something like..
tcp6 0 0 :::8686 :::* LISTEN 3452/java
next use
kill -9 3452
to kill that process ( 3452 in this case )
Now try to start glassfish, it should start.
Some Times, I have seen people initialize variables in loop like
While(condition){
String s="tttt";
}
This should be avoided as it waste lot of memory.
This is a tricky problem. I've run into it once or twice, and increasing heap size doesn't help. Solving it with VM settings - you may even want to decrease the heap size (it once worked for me). You may also want to test different garbage collectors, I've had success using the G1 collector.
General advice on how to avoid this error is also hard (or so it seemed to me when I researched this matter to solve my own problems). High infant mortality is probably good, since young objects are cheaper to collect than old ones.
large dictionary...mmm.. is there an absolute requirement of storing that directly in the memory of the jvm?
A lazy chap like myself would store this in a database (in-memory even perhaps? - hypersonic for example), transfer the responsibility of searching through a list to the database while my program worked on creating interesting symmetric black and white square combinations :)
Just a thought though.
精彩评论