Вы находитесь на странице: 1из 15

2/23/2019 java - Garbage Collection and Threads - Stack Overflow

Garbage Collection and Threads Ask Question

AFAIK when a GC
is doing its thing
21 the VM blocks all
running threads --
or at least when it
is compacting the
heap. Is this the
8 case in modern
implementions of
the CLR and the
JVM (Production
versions as of
January 2010) ?
Please do not
provide basic links
on GC as I
understand the
rudimentary
workings.

I assume global
locking is the case
as when
compaction occurs
references might
be invalid during
the move period,
and it seems
simplest just to
lock the entire
heap (i.e.,
indirectly by
blocking all
threads). I can
imagine more
robust
mechanisms, but
KISS often
prevails.

If I am incorrect
By using our site, you acknowledge myhave read and understand our Cookie Policy, Privacy Policy, and
that you
question would be
our Terms of Service.
answered by a
https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 1/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

simple explanation
of the strategy
used to minimise
blocking. If my
assumption is
correct please
provide some
insight on the
following two
questions:

1. If this is
indeed the
behaviour,
how do
heavyweight
enterprise
engines like
JBOSS and
Glassfish
maintain a
consistantly
high TPS rate
? I did some
googling on
JBOSS and I
was expecting
to find
something on
a APACHE
like memory
allocator
suited for web
processing.
2. In the face of
NUMA-esque
architectures
(potentially the
near future)
this sounds
like a disaster
unless the
processes are
CPU bound by
thread and
memory-
allocation.

java clr

memory-management

blocking
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
garbage-collection
our Terms of Service.

https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 2/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

edited Jan 18 '10 at 12:55


Joachim Sauer
236k 50 483 561

asked Jan 18 '10 at 11:15


Hassan Syed
13.6k 6 64 150

6 Answers

The answer is that


this depends on
16 the garbage
collection
algorithms used.
In some cases,
you are correct
that all threads are
stopped during
GC. In other
cases, you are
incorrect in that
garbage collection
proceeds while
normal threads
are running. To
understand how
GC's achieve that,
you need a
detailed
understanding of
the theory and
terminology of
garbage
collectors,
combined with an
understanding of
the specific
collector. It is
simply not
amenable to a
simple
explanation.

Oh yes, and it is
worth pointing out
that many modern
collectors don't
have a
compaction phase
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
per-se. Rather
our Terms of Service.
they work by
https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 3/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

copying live
objects to a new
"space" and
zeroing the old
"space" when they
are done.

If I am
incorrect my
question would
be answered
by a simple
explanation of
the strategy
used to
minimise
blocking.

If you really want


to understand how
garbage collectors
work, I
recommend:

"Garbage
Collection:
Algorithms for
Automatic
Dynamic
Memory
Management"
by Richard
Jones.
"The Garbage
Collection
Handbook:
The Art of
Automatic
Memory
Management"
by Richard
Jones,
Antony
Hosking and
Eliot Moss

... and beware that


finding accurate,
detailed, public
descriptions of the
internals of
production
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
garbage collectors
our Terms of Service. is not easy.

https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 4/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

(Though in the
case of the
Hotspot GC's, you
can look at the
source code ...)

EDIT: in response
to the OP's
comment ...

"It seems it is
as I thought --
there is no
getting around
the "stop the
world" part."

It depends. In the
case of the Java 6
Concurrent
Collector, there
are two pauses
during the marking
of the roots
(including stacks),
and then marking /
copying of other
objects proceeds
in parallel. For
other kinds of
concurrent
collector, read or
write barriers are
used while the
collector is
running to trap
situations where
the collector and
application
threads would
otherwise interfere
with each other. I
don't have my
copy of [Jones]
here right now, but
I also recall that it
is possible to
make the "stop the
world" interval
negligible ... at the
cost of more
expensive pointer
By using our site, you acknowledge
operationsthat you have read and understand our Cookie Policy, Privacy Policy, and
and/or
our Terms of Service.

https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 5/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

not collecting all


garbage.

ited Jun 4 '15 at 22:18


dpercy
144 2 9

swered Jan 18 '10 at 11:59


Stephen C
520k 70 577 936

(+1) will
definitely buy
that book. A
point I would
like to know
more on is what
happens after
the "copy to
new and update
procedure" ?
presumeably all
references are
updated by
blocking all
threads ? Or is
it an itterative
procedure ? –
Hassan Syed
Jan 18 '10 at
12:06

2 As much as I
know, even the
most concurrent
garbage
collectors "stop
the world" for
some of their
work, although
most tasks run
indeed
concurrently.
The CMS-
collector in the
JVM 6,
however, does
use all available
CPUs during
the stop-the-
world phase. –
edgar.holleis
Jan 18 '10 at
12:23

@edgar Thank
you, "CMS-
collector" usefull
search term -- I
will lookthat
By using our site, you acknowledge up the
you have read and understand our Cookie Policy, Privacy Policy, and
details when I
our Terms of Service. have time. It

https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 6/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

seems it is as I
thought -- there
is no getting
around the "stop
the world" part.
It would be
interesting to
have some
hypothesis' or
some yard-stick
numbers on
how these
global lock
phases effect
the TPS rates of
serious
production
servers (like the
ones I
mentioned in
the question). –
Hassan Syed
Jan 18 '10 at
12:36

About TPS: You


get it all wrong!
You can
generally trade
latency
(measured in
ms, lower is
better) against
throughput
(measured in
TPS, higher is
better). Low
latency
algorithms
generally have
lower
throughput than
the alternative:
high throughput
algorithms that
generally also
introduce higher
latencies. If
you're primarily
worried about
TPS, typical for
server-loads,
stop worrying
about the length
of the stop-the-
world phase. It
will be
comparatively
long, but not so
long as to
impact your
TPS. –
edgar.holleis
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
Jan 21 '10 at
our Terms of Service.
10:36

https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 7/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

@Hassan -
@edgar is right.
The way to
maximize TPS
is to use a GC
that has the
lowest total
overheads, not
the one that
minimizes
pause times. –
Stephen C Jan
21 '10 at 22:01

You are correct


that the garbage
2 collector will have
to pause all the
application
threads. This
pause time can be
reduduced with the
sun JVM by using
the concurrent
collector which
preforms some of
the work without
stopping the
application, but it
stll has to pause
the application
threads.

See here
http://java.sun.com
/javase/technologie
s/hotspot/gc/gc_tu
ning_6.html#par_g
c and here
http://java.sun.com
/javase/technologie
s/hotspot/gc/gc_tu
ning_6.html#cms
for details on how
the sun JVM
manages garbage
collection in
By using our site, you acknowledge theyou have read and understand our Cookie Policy, Privacy Policy, and
that
our Terms of Service. latest JVMs.

https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 8/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

For web
applications I don't
think this is an
issue. As the user
requests should
complete within a
small amount of
time < 1s any
temporary objects
allocated to service
the request should
not exit the young
generation
(providing it is
sized
appropriately)
where they are
cleaned up very
efficiently. Other
data with longer
lifecycles such as
user sessions will
hang around
longer and can
impact the time
spent on major GC
events.

On high TPS
applications a
common strategy
is to run multiple
instances of the
application server
either on the same
or separate
hardware using
session affinity and
load ballancing. By
doing this the
individual heap
size per JVM is
kept smaller which
reduced the pause
times for GC when
performing a major
collection. In
general the
database becomes
the bottle neck
rather than the
application or JVM.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
The closest you
our Terms of Service.
might find to the
https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 9/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

concept of a web
specific memory
allocator in in J2EE
is object/instance
pooling that is
performed by
frameworks and
application severs.
For example in
JBOSS you have
EJB pools and
database
connection pools.
However these
objects are usually
pooled because of
thier high creation
cost rather than
the garbage
collection
overhead.

ited Jan 18 '10 at 21:16

swered Jan 18 '10 at 12:40


Aaron
5,297 4 19 29

(+1) good
discussion -- I
distill that I need
to look at the
concurrent
minor cycle
collector to see
what it's effects
are. The
assumption is
that the TPS
characteristics of
a web-server
highly depend on
a requests
allocations
remaining as
young
generation
objects. –
Hassan Syed
Jan 18 '10 at
13:15

@Hassan Yes
the paragraph
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
about young
our Terms of Service. generation is an
assumption and
https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 10/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

will depend on
your application,
although you can
tune size of the
generations in
the JVM. The
point was you
should not worry
too much about
the temporary
objects created
while processing
request as the
current
generational
garbage
collectors are
very good at
cleaning these
up. Running
multiple JVMs
can help with the
longer lived
objects such as
user sessions as
they can be
distributed
between the
JVMs meaning
each JVM has
less to check
during a major
collection. –
Aaron Jan 18
'10 at 21:25

-1: GCs do not


have to pause all
threads (aka
"stop the world").
– Jon Harrop
Aug 15 '10 at
18:28

I believe IBM have


performed some
1 research towards
improving GC
performance in
multi-core systems
which includes
work on reducing
or eliminating the
'everything stop'
issue.

E.g. see: A
Parallel, that you have read and understand our Cookie Policy, Privacy Policy, and
By using our site, you acknowledge
our Terms of Service. Incremental and

https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 11/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

Concurrent GC for
Servers(pdf)

Or google
something like
"concurrent
garbage collection
ibm"

swered Jan 18 '10 at 13:02


redcalx
4,427 3 37 80

AFAIK when a
GC is doing its
1 thing the VM
blocks all
running threads
-- or at least
when it is
compacting the
heap. Is this the
case in modern
implementions
of the CLR and
the JVM
(Production
versions as of
January 2010)
?

Both Sun's Hotspot


JVM and
Microsoft's CLR
have concurrent
GCs that stop-the-
world only for short
phases (to get a
self-consistent
snapshot of the
global roots from
which all live data
are reachable) and
not for entire
collection cycles.
I'm not sure about
their
implementations of
compaction but
that is a very rare
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
occurrence.
our Terms of Service.

https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 12/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

If this is indeed
the behaviour,
how do
heavyweight
enterprise
engines like
JBOSS and
Glassfish
maintain a
consistantly
high TPS rate?

The latency of
those engines is
orders of
magnitude longer
than the time taken
to stop the world.
Also, latencies are
quoted as, for
example, 95th
percentile meaning
that the latency will
only be below the
quoted time span
95% of the time.
So compactions
are unlikely to
affect quoted
latencies.

swered Aug 17 '10 at 13:41


Jon Harrop
39.5k 10 144 247

There are a
number of GC
0 algorithms
available with
Java, not all of
which block all
running threads.
For example, you
can use -
XX:+UseConcMark
SweepGC which
runs concurrently
with the app (for
collection of the
tenured that you have read and understand our Cookie Policy, Privacy Policy, and
By using our site, you acknowledge
generation).
our Terms of Service.

https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 13/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

swered Jan 18 '10 at 11:49


Matthew Wilson
3,433 15 12

They all block


the running
threads
occasionally
when they need
to do global GC.
The concurrent
GC does try to
do most of its
work in a
concurrent
fashion though.
<java.sun.com/ja
vase/technologie
s/hotspot/gc/…>
– Paul Wagland
Jan 18 '10 at
12:52

a.k.a. (mostly)
Concurrent Mark
Sweep. The
(mostly) is
significant. –
Tom Hawtin - tack
Jan 18 '10 at
17:25

There are fully


concurrent GCs
for other JVMs. –
Jon Harrop Aug
15 '10 at 19:06

Current state of the


art garbage
0 collection for Java
still involves
occasional "stop
the world" pauses.
The G1 GC
introduced on Java
6u14 does most of
it's work
concurrently,
however, when
memory is really
low, and it needs to
compact the heap,
then it has to
ensure thatthat
By using our site, you acknowledge no-one
you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service. messes with the
heap underneath
https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 14/15
2/23/2019 java - Garbage Collection and Threads - Stack Overflow

it. This requires


that nothing else is
allowed to
proceed. To find
out more about the
G1 GC, look at the
presentations from
Sun.

swered Jan 18 '10 at 12:48


Paul Wagland
17.1k 7 44 67

-1: State-of-the-
art garbage
collection for
Java has been
fully concurrent
for years now.
There are hard
real-time JVMs
out there... –
Jon Harrop Aug
15 '10 at 18:30

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/2085544/garbage-collection-and-threads 15/15

Вам также может понравиться