3

As I read in the Sun Memory management white paper:

When stop-the-world garbage collection is performed, execution of the application is completely
suspended during the collection

So if a request happens while the garbage collector is running then how is it handled by the application? If the garbage collector takes too long will the application throw an exception? I have not come across such an issue but wanted to know is this possible and what exception gets thrown?

Natan Streppel
  • 5,552
  • 6
  • 31
  • 43
Jeets
  • 2,829
  • 6
  • 29
  • 47
  • I don't think there will be any exception that will be thrown. – codeMan Aug 27 '13 at 12:04
  • but if a network operation is going on there will be some sort of a network timeout for sure if it takes longer. so how is that handled – DevZer0 Aug 27 '13 at 12:05

2 Answers2

1

All (almost) Java garbage collectors has some sort of a Stop-the-world phase where all the Java threads are suspended waiting for a exclusive system operations to complete. This state is sometimes referred to as a safepoint.

The modern garbage collectors are concurrently running together with the applications threads, which means that the garbage collector perform its work at the same time as the application. During the garbage collector process there are phases where exclusive access memory is needed, the application threads goes into this safepoint state.

An exception is thrown if the garbage collector cannot recover enough memory to meet the application´s allocation demands.

One alternative to get rid of the stop-the-world garbage collections is to go for the Zing JVM with the C4 collector from Azul systems. The implementation has a low pause approach with no stop-the-world collections at all. Instead it is using a concurrent compacting approach with no stop-the-world phase.

Robert Höglund
  • 924
  • 9
  • 12
  • How long can a thread go between safepoints? If thread X is between safepoints and a garbage collection is required, would that require all other threads to stopped for as long as it took X to reach the next safepoint, regardless of how long collection would otherwise take? – supercat Sep 13 '13 at 17:49
0

This garbage collector is not in use anymore and replaced by better garbage collectors.

The stop-the-world garbage collector really stopped the complete application (all threads) and cleaned up the heap.

When the garbage collector would take too long (which almost never happens) then an Error would be thrown.

Incomming traffic on network sockets is buffered for the time the collector runs.

Uwe Plonus
  • 9,235
  • 4
  • 36
  • 47
  • An exception is thrown? I dint know that! which exception would that be @Uwe Plonus – codeMan Aug 27 '13 at 12:10
  • If any garbage collector cannot do it's task in a timely manner an `OutOfMemoryError` will be thrown. – Uwe Plonus Aug 27 '13 at 12:11
  • @UwePlonus: if the collector is a stop-the-world collector it doesn't matter how long it takes, you won't get an OutOfMemoryError since the program has been stopped and no allocation is taking place. You might get OutOfMemoryError if the garbage collection finishes its run but cannot reclaim any free space. – davmac Aug 27 '13 at 13:16
  • @davmac I had already several `OutOfMemoryError`s not caused by an exhausted heap space, e.g. `java.lang.OutOfMemoryError: GC overhead limit exceeded.` which is thrown if the default garbage collector from Java 7 takes too much time. – Uwe Plonus Aug 27 '13 at 13:21
  • @UwePlonus I think we both had it slightly wrong. You _can_ get an OutOfMemoryError if the garbage collection takes a long time, but only if it is failing to reclaim enough space. See for eg http://stackoverflow.com/questions/5839359/java-lang-outofmemoryerror-gc-overhead-limit-exceeded - I generalised when I said that you get the error if it cannot reclaim _any_ space. – davmac Aug 27 '13 at 13:35
  • "This garbage collector" - the question doesn't mention a specific garbage-collector. Even so, even CMS includes some stop-the-world phases and I can't think of any young-generation-collectors that don't do stop-the-world. – piet.t Sep 13 '13 at 06:34