Just for my sanity, before an OutOfMemoryError is thrown, the garbage collector should do everything it can to free up more memory right? What I'm seeing now is that the JVM does a partial GC, throws an OutOfMemoryError and then decides to actually do a full GC which frees up enough RAM to get things working again.
Very annoying and I'm not sure what I can do about it. There are definitely no remaining references, but I still can't get that memory back.
Category: Code and Geek Stuff, Java
Ricky Clarkson says:
I believe this can happen when you are allocating a large amount of memory in a loop, so you’re not giving the GC chance to clean up – it does run at a lower priority than your code, usually.
You can probably fix this by calling System.gc() sometimes, although even if that works it isn’t intended to be reliable.
Are you allocating large blocks (10s of megabytes) at a time? Perhaps if you allocate smaller blocks at a time the GC would behave differently. Also, it may be worth tinkering with the command line VM options.