Garbage Collection in Java - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Garbage Collection in Java

Description:

sets the ratio of the survivor space to the eden in the new object area. ... Users who do not have pause time problems (as seen by erratic application ... – PowerPoint PPT presentation

Number of Views:1276
Avg rating:3.0/5.0
Slides: 36
Provided by: 19921
Category:

less

Transcript and Presenter's Notes

Title: Garbage Collection in Java


1
Garbage Collection in Java
  • Toronto Java Users Group
  • September 3, 2002
  • Ethan Henry
  • Sitraka

2
What is a JVM?
  • The Java Virtual Machine has 2 primary jobs
  • Execute Code
  • Manage Memory
  • It also does stuff like managing locks
  • Most other functions are really not part of the
    JVM, but the library, like file network I/O

3
Memory Management
  • Allocate Memory from OS
  • Manage Java Allocations
  • including heap compaction
  • Remove Garbage Objects
  • Java Virtual Machine Specification 2.4.6

4
What is Garbage Anyhow?
  • An object is created in the heap and is
    garbage-collected after there are no more
    references to it. Objects cannot be reclaimed or
    freed by explicit language directives.
  • Objects become garbage when there are no more
    references to the object
  • This is not to suggest that garbage collectors
    have to be implemented using reference counting
    however...

5
Reachability
  • Objects become garbage when theyre no longer
    reachable from the root set
  • The root set consists of
  • static reference fields in classes
  • local references
  • Exact versus Non-Exact Garbage Collectors

6
Reachable Objects
  • Elements within the root set directly refer to
    objects within the heap of the JVM
  • Reference variables within those objects refer to
    further objects within the Heap (indirectly
    reachable from the Root Set)

Heap
Root Set
7
How is it implemented?
  • Sun Classic
  • Sun HotSpot
  • IBM

8
Sun Classic
  • Mark, Sweep Compact
  • Mark identify garbage
  • Sweep Find garbage on heap, de-allocate it
  • Compact collect all empty memory together
  • Eligibility for garbage collection is determined
    by walking across memory, determining
    reachability and then compacting the heap
  • Compaction is just copying the live objects so
    that theyre adjacent in memory
  • theres one large, contiguous block of free memory

9
The Pause
  • The main problem with classic mark, sweep and
    compact is that all other threads have to be
    suspended while the garbage collector runs
  • Pause time is proportional to the number of
    objects on the heap
  • not the amount of garbage

10
Sun HotSpot
  • Sun improved memory management in the Java 2 VMs
    (JDK 1.2 and on) by switching to a generational
    garbage collection scheme
  • The heap is separated into two regions
  • New Objects
  • Old Objects

11
New Object Region
  • The idea is to use a very fast allocation
    mechanism and hope that objects all become
    garbage before you have to garbage collect
  • The New Object Regions is subdivided into three
    smaller regions
  • Eden, where objects are allocated
  • 2 Survivor semi-spaces From and To

12
New Object Region
  • The Eden area is set up like a stack - an object
    allocation is implemented as a pointer increment
  • When the Eden area is full, the GC does a
    reachability test and then copies all the live
    objects from Eden to the To region
  • The labels on the regions are swapped
  • To becomes From - now the From area has
    objects

13
New Object Region
  • The next time Eden fills objects are copied from
    both the From region and Eden to the To area
  • Theres a Tenuring Threshold that determines
    how many times an object can be copied between
    survivor spaces before its moved to the Old
    Object region
  • Note that one side-effect is that one survivor
    space is always empty

14
Old Object Region
  • The old object region is for objects that will
    have a long lifetime
  • The hope is that because most garbage is
    generated by short-lived objects that you wont
    need to GC the old object region very often

15
Generational Garbage Collection
New Object Region
Old Object Region
Eden
SS1
SS2
Old
FirstGC
Eden
SS1
SS2
Old
Eden
SS1
SS2
Old
SecondGC
Eden
SS1
SS2
Old
Eden
SS1
SS2
Old
16
GC Output
  • Running the JVM with -verbosegc will show
    information like this
  • GC 1667K-gt1295K(1984K), 0.0101756 secs
  • GC 1807K-gt1434K(1984K), 0.0223998 secs
  • GC 1946K-gt1574K(2112K), 0.0116185 secs
  • Full GC 1574K-gt1574K(2112K), 0.0830561 secs
  • GC 3454K-gt2081K(4672K), 0.0495951 secs
  • GC 4001K-gt2599K(4672K), 0.0274256 secs
  • GC 4519K-gt3101K(5056K), 0.0308995 secs
  • Full GC 3101K-gt3101K(5056K), 0.1452472 secs
  • GC 7039K-gt4131K(9452K), 0.0777414 secs
  • GC 8227K-gt5174K(9452K), 0.0627538 secs
  • GC 9270K-gt6209K(10348K), 0.1125570 secs

17
Incremental Garbage Collection
  • -Xincgc
  • Sun also has an incremental collector that breaks
    that old-object region into smaller chunks and
    GCs them individually
  • Pause times are smaller but overall throughput is
    decreased
  • Inc GC 3566K-gt3950K(5120K), 0.0309922 secs
  • GC 4078K-gt3594K(5184K), 0.0264542 secs
  • Inc GC 3594K-gt3978K(5120K), 0.0272683 secs
  • GC 4106K-gt3627K(5120K), 0.0272381 secs
  • Inc GC 3627K-gt4011K(5056K), 0.0285464 secs
  • GC 4139K-gt3666K(5184K), 0.0281388 secs

18
Concurrent Garbage Collection
  • -Xconcgc
  • Concurrent GC allows other threads to keep
    running in parallel with the GC
  • Available in JDK 1.4.1
  • GC 1463K-gt1093K(2560K), 0.0089573 secs
  • GC 1093K(2560K), 0.0053470 secs
  • GC 1094K(2560K), 0.0092867 secs
  • GC 1604K-gt1228K(2560K), 0.0104823 secs
  • GC 1228K(2560K), 0.0062662 secs
  • GC 1234K(2560K), 0.0097820 secs
  • GC 1740K-gt1373K(2560K), 0.0115875 secs

19
Parallel GC
  • Future JVMs will include the ability to run GC on
    separate processors on a multi-processor machine
  • This is already available in BEAs JRockit JVM
  • http//www.jrockit.com

20
IBM
  • Improved single-heap mark, sweep compact
  • Uses parallel marking in JDK 1.3.0 and concurrent
    marking in JDK 1.3.1
  • IBMs JVM provides a much more detailed breakdown
    of whats happening during GC

21
IBM GC
  • After the IBM JVM GC has identified garbage it
    tries to free memory for the new allocation by
  • compacting allocated memory
  • finalizing pending dead objects
  • removing weakly reachable objects
  • increasing the heap space
  • for more on weak refs, see http//developer.java.s
    un.com/developer/technicalArticles/ALT/RefObj/

22
IBM GC
  • ltAF9 Allocation Failure. need 2064 bytes, 4096
    ms since last AFgt
  • ltAF9 managing allocation failure, action1
    (0/16086056) (34768/34768)gt
  • ltGC Tue Sep 03 180742 2002
  • ltGC(9) freed 2345744 bytes in 234 ms, 14 free
    (2380512/16120824)gt
  • ltGC(9) mark 226 ms, sweep 8 ms, compact 0
    msgt
  • ltGC(9) refs soft 0 (age gt 1), weak 0, final
    0, phantom 0gt
  • ltAF9 managing allocation failure, action3
    (2380512/16120824)gt
  • ltGC(9) need to expand mark bits for
    19659768-byte heapgt
  • ltGC(9) expanded mark bits by 53248 to 307200
    bytesgt
  • ltGC(9) need to expand alloc bits for
    19659768-byte heapgt
  • ltGC(9) expanded alloc bits by 53248 to 307200
    bytesgt
  • ltGC(9) expanded heap by 3538944 to 19659768
    bytes, 30 freegt
  • ltAF9 completed in 2974 msgt

23
IBM GC
  • ltAF20 Allocation Failure. need 24 bytes, 29793
    ms since last AFgt
  • ltAF20 managing allocation failure, action1
    (0/37167760) (1956200/1956200)gt
  • ltGC Tue Sep 03 180936 2002
  • ltGC(20) freed 20688256 bytes in 467 ms, 57 free
    (22644456/39123960)gt
  • ltGC(20) mark 453 ms, sweep 14 ms, compact 0
    msgt
  • ltGC(20) refs soft 0 (age gt 6), weak 0, final
    41, phantom 0gt
  • ltGC(20) stop threads time 405, start threads
    time 56gt
  • ltAF20 completed in 961 msgt

24
How To Tune GC
  • Not a simple topic
  • There are no universal magic values - every app
    is different
  • Things to tune
  • Memory Size
  • overall size, individual region sizes
  • GC parameters
  • Minimum/maximum of free heap,
  • Type of GC
  • single heap, generational, incremental,
    concurrent, parallel

25
Tuning Parameters - Both
  • -ms, -Xms
  • sets the initial heap size
  • -mx, -Xmx
  • sets the maximum heap size
  • -Xss
  • sets the size of the per-thread stacks
  • -Xminf 0-1, -XXMinHeapFreeRatio 0-100
  • sets the percentage of minimum free heap space -
    controls heap expansion rate
  • -Xmaxf 0-1, -XXMaxHeapFreeRatio 0-100
  • sets the percentage of maximum free heap space -
    controls when the VM will return unused heap
    memory to the OS

26
Tuning Parameters - Sun
  • -XXNewRatio
  • sets the ratio of the old and new generations in
    the heap. A NewRatio of 5 sets the ratio of new
    to old at 15, making the new generation occupy
    1/6th of the overall heap
  • defaults client 8, server 2
  • -XXNewSize, -XXMaxNewSize 1.3
  • -Xmn 1.4
  • sets the minimum and maximum sizes of the new
    object area, overriding the default calculated by
    the NewRatio
  • -XXSurvivorRatio
  • sets the ratio of the survivor space to the eden
    in the new object area. A SurvivorRatio of 6 sets
    the ratio of the three spaces to 116, making
    each survivor space 1/8th of the new object
    region
  • default 25

27
Tuning Parameters - Sun
  • -XXAggressiveHeap
  • This option instructs the JVM to push memory use
    to the limit the overall heap is around 3850MB,
    the memory management policy defers collection as
    long as possible, and (in some VMs) some GC
    activity is done in parallel. Because this option
    sets heap size, do not use in conjunction with
    the -Xms or -Xmx options

28
Tuning Parameters - IBM
  • -Xgcpolicyltoptthruput optavgpausegt
  • Setting gcpolicy to optthruput disables
    concurrent mark. Users who do not have pause time
    problems (as seen by erratic application response
    times) should get the best throughput with this
    option. Optthruput is the default setting.
  • Setting gcpolicy to optavgpause enables
    concurrent mark with its default values. Users
    who are having problems with erratic application
    response times caused by normal garbage
    collections can alleviate those problems at the
    cost of some throughput when running with the
    optavgpause option.

29
  • http//java.sun.com/docs/hotspot/gc
  • http//java.sun.com/docs/performance
  • http//www-106.ibm.com/developerworks/library/j-jt
    c/index.html

30
Loitering Objects
  • What wont the garbage collector clean up?

31
Loiterers
Allocated
Reachable
Live
32
Finding Loiterers
  • How do you identify these loitering objects?
  • You need to
  • identify specific use cases for your application
    that are important or that you suspect have
    problems
  • get a tool that lets you inspect whats on the
    heap before and after each use case
  • look at the difference - are those the objects
    you expect to see created?
  • are any unnecessary objects still being
    referenced?

33
Tools
  • See some reviews on http//www.javaperformancetuni
    ng.com/tools/index.shtml

34
Fixing Loiterers
  • Track down what objects are loitering
  • Track the instances to their allocation point
  • Decide when they should become unreachable and
    make sure it actually happens

35
End
Write a Comment
User Comments (0)
About PowerShow.com