Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Heap Overflow, Forcing Garbage Collection

I've create a trie tree with an array of children. When deleting a word, I set the children null, which I would assume deletes the node(delete is a relative term). I know that null doesn't delete the child, just sets it to null, which when using a large amount of words it causes to overflow the heap.

Running a top on linux, I can see my memory usage spike to 1gb pretty quickly, but if I force garbage collection after the delete (Runtime.gc()) the memory usage goes to 50mb and never above that. From what I'm told, java by default runs garbage collection before a heap overflow happens, but I can't see to make that happen.

like image 572
Nicholas Avatar asked Jan 19 '26 08:01

Nicholas


2 Answers

(this too long for a comment)

Contrarily to popular belief you CAN really force a GC in Java but this is not done using System.gc(). The way to really force a GC is to use JVMTI's ForceGarbageCollection() call. Don't ask me more, I asked a question here and nobody found it interesting (no upvotes) and nobody could answer it, yet JVMTI's ForceGarbageCollection() is how a lot of Java programs like IntelliJ, NetBeans, VisualVM, Eclipse etc. do really force a GC:

Java: How do you really force a GC using JVMTI's ForceGargabeCollection?

Now... You probably do not want to do that and you probably do not want to hint the GC using the "no guarantee" System.gc() call.

At how many words do you start having problems? There are very compact data structure when you need to work with insane number of words. Are you sure you're using the correct data structure and are you sure you're not having leaks?

like image 111
SyntaxT3rr0r Avatar answered Jan 20 '26 21:01

SyntaxT3rr0r


Are you are referring to the memory not being freed to the OS - i.e. top and similar programs show that the Java process takes 1GB of memory? Even though Java's garbage collector frees the memory from its heap, it can still keep hold of the memory so that future allocations don't need to request for more memory from the OS.

To see how much heap space is actually used by the Java objects, use VisualVM or a similar Java-specific tool. If your machine has lots of memory, then the JVM will use it (IIRC, especially the Server VM is tuned to reserve more memory), but you can always limit it with the -Xmx and other JVM options.

like image 35
Esko Luontola Avatar answered Jan 20 '26 20:01

Esko Luontola