1 / 17

Garbage Collection

Garbage Collection. CSC 331 Object Oriented Programming Using Java. Overview. What, me worry? Garbage in Javaville. Different ways to collect garbage. Taking care of Java garbage. Tweaking Java garbage collection. Finalizing stuff. Credits. What, me worry?.

Télécharger la présentation

Garbage Collection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Garbage Collection CSC 331 Object Oriented Programming Using Java

  2. Overview • What, me worry? • Garbage in Javaville. • Different ways to collect garbage. • Taking care of Java garbage. • Tweaking Java garbage collection. • Finalizing stuff. • Credits.

  3. What, me worry? • What is garbage collection? • C++ makes me use destructors . . . • Java doesn’t. • Automatic garbage collection. • No need for destructors. • No memory leaks? • Can I cause garbage collection in Java? • If not, why be concerned?

  4. Starting garbage collection . . . • System.gc() • No guarantee that g.c. begins right away. • JVM starts g.c. . . . • Whenever JVM “wants” to . . . • No garbage to collect unless all references to one or more objects have been invalidated.

  5. Garbage in Javaville: a Metaphor “Imagine you live in a really small town (let's call it JavaVille). It has one main street, and, for fun, residents cruise the street, throwing candy wrappers and old computer parts out their car windows. This creates quite a mess, and the town has hired a full-time garbage collector to deal with the problem.”* .*Adapted from Holling, Greg, “J2SE 1.4.1 boosts garbage collection,” JavaWorld, March 2003

  6. Javaville stinks . . . • “Now, imagine you are the town garbage collector, or sanitation engineer. With your garbage truck, you pick up all the trash left on the street. However, your truck is so big that no other cars can travel on the main street while you're collecting garbage. This gives people something else to complain about—they can't do anything else while the garbage truck travels through town. You try to stay out of the way, but sometimes, certain vehicles must get through, like ambulances and fire trucks.”

  7. Javaville attacks the problem • “To alleviate the situation, the town council votes to add some new streets so people can go about their business even when the garbage truck is running. They also decide to buy more garbage trucks so garbage collection takes less time.”

  8. Javaville garbage collection - constrained by number of trucks and streets. • “This is a really useful metaphor for thinking about Java garbage collection, where multiple application threads (litterbugs) create garbage, and one thread (you) cleans it up. The application threads and the garbage collector share a single CPU (the main street), and all activity in the applications must stop while garbage collection is in progress (stop the world). You can make garbage collection less noticeable by adding more CPUs (streets) or more garbage collection (GC) threads (garbage trucks). Note that adding more GC threads (trucks) won't do you any good unless you have enough CPUs (streets) for them to work effectively in parallel.”

  9. How to collect garbage, according to Sun. • Sun JVM specs do not require garbage collection! • Thus, a JVM may not have it. • Example: JVM for a bare-bones smart card app. • However, most JVMs do have it. • The garbage collection algorithm is not defined. • This means that the timing and manner of g.c. can’t be predicted unless you know the specific JVM being used-and maybe not then.

  10. Different ways to collect garbage • Mark and sweep: Original in JDK 1.0 • Mark: Identify garbage objects. • Sweep: Reclaim memory used by the objects. • A “stop the world” technique • All threads stop until g.c. is complete, or • Until a higher-priority thread interrupts g.c.

  11. Different ways to collect garbage • Sun HotSpot VM approach • Split heap into 3 sections. • Permanent space – for JVM class and method objects. • Old object space – for longer-lived objects. • New object space – for newly created objects. • Three different g.c. techniques • P.s.: Incremental garbage collection. • Old o.s.: Copy compaction • New o.s.: Mark-compact. • All 3 are “stop the world” techniques.

  12. Taking care of Java garbage. • Three new algorithms in J2SE 1.4.1: • Parallel copying – optimized for multiple CPUs. • Parallel scavenging – optimized for large amounts of memory • Concurrent • 6 phases • 2 are “stop the world” • 4 are parallel techniques

  13. Tweaking Java garbage collection • Command-line switches can • select algorithms, • affect heap allocation. • How to select an algorithm: • If it ain’t broke, don’t fix it. • Some heuristics available. • Careful forethought, testing required for proper selection.

  14. Changes in garbage collection in J2SE 5.0: More tuning possible. • On server-class machines default g.c. is a parallel collector. • Can be overridden. • Maximum heap size is the smaller of 1 GB or 1/4 the available memory. • Formerly varied by platform. Can be overridden. • Upper limit of percentage of total time spent on g.c. can be specified (default: 98%). • Lower limit of space freed during g.c. as % of heap size can be set (default: 2%).

  15. Finalization • What is it? • finalize() • A method of Object. • Java specs: Before reclaiming memory for an object that has a finalizer, finalize () will be invoked. • So? • Use finalizers to free non-memory finite resources, such as sockets. • A backup; should be previously freed. • If you override finalize(), be sure to call super.finalize() as the last action in your method.

  16. Summary • Explanation of why g.c. is important to understand. • Used a metaphor to aid understanding of g.c. • Introduced different ways to collect garbage. • Described ways to tune g.c.. • Introduced concept of finalization. • Acknowledgements.

  17. Credits • java.sun.com/docs/books/tutorial/java/data/garbagecollection.html • java.sun.com/docs/books/tutorial/java/javaOO/objectclass.html • http://java.sun.com/j2se/1.5.0/docs/guide/vm/gc-ergonomics.html • Eckel, Bruce, Thinking in Java, 3d. ed., Prentice-Hall, 2003, 183-191. • Friesen, Jeff, “Trash Talk, Part I,” JavaWorld, December 2001. • Holling, Greg, “J2SE 1.4.1 boosts garbage collection,” JavaWorld, March 2003. • Jia, Xiaoping, Object-Oriented Software Development Using Java, 2d. ed., Addison-Wesley, 2003, 90-91. • Venners, Bill, “Object finalization and cleanup,” JavaWorld, June 1998.

More Related