I understand that GC gets triggered when a new object allocation fails or System.gc() is called. Every GC algorithm suggests that as a first step the GC thread will suspend all the application threads so that they won't affect the GC activity.
But I would like to understand how GC suspends all the running threads? I mean is there any safe points defined by JVM, for example, memory allocation (new object creation) or method invocation, and when application thread reaches these safe points they will be blocked against a GC lock. Is it true? If so, then how about an application thread that does only a simple computation as follows (I know in reality this will never happen), will it ever get suspended?
while(true) {
a = a + s;
s = s + a;
// some computation that doesn't touch any JVM safe points
}
In these cases, does GC activity carry on without suspending these application threads (and suspend/block later when they try to cross a safe point, for example object allocation)?
But i believe, GC always waits for these application threads to enter the safe points and suspends them before proceeding. Is my assumption true?