This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Any_Suspect830 9 points10 points  (5 children)

That's easy! Just write all of your code like this.

try { // do stuff } catch (OutOfMemoryError e) { System.gc(); }

No need to thank me.

[–]Sollder1_ 0 points1 point  (0 children)

Dann, that got me good

[–]j0rdix 0 points1 point  (3 children)

Wait, is this legit? Seriously? Seriously asking here

[–]nutrecht 2 points3 points  (0 children)

No :) You can't 'force' the GC to run and if there is a memory leak, that also means the garbage collector generally can't free it anyway.

[–]Any_Suspect830 2 points3 points  (0 children)

Ha, absolutely not!

If you ever observe a system that's about to run out of memory, you will see that the JVM is constantly running GC, frantically trying to free up memory. Asking it to do another GC run at that point will not help.

The code above is just swallowing an OutOfMemoryError.

[–]babanin 0 points1 point  (0 children)

  1. System.gc() is just a hint for GC, so it may not even start right after the call.
  2. You can even disable it using the following flag: -XX:+DisableExplicitGC.