2 Replies Latest reply: Jul 25, 2011 5:34 PM by 877647 RSS

    How Eden Space GC works?


      In this article http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html
      it is described that garbage collector is first executed on Eden survival space
      to eliminate the need of running the full GC.

      I am wondering how Mark Sweep algorithm can be applied to only the Eden subset
      of Java objects without touching other objects from other survival spaces.
      Because the idea of Mark Seep is to traverse everything from roots to find out
      unreferenced objects, but it means that we cannot leave with only Eden space traversal.

      Does anybody knows how it is done in reality? Is there some other algorithm used
      for Eden survival space which differs from the classical Mark Sweep?
      Reference counting?

        • 1. Re: How Eden Space GC works?
          The Concurrent Mark Sweep only cleans up tenured space.
          Objects to be retained in the Eden space are copied to the Survivor space. At that point the Eden space is empty.
          • 2. Re: How Eden Space GC works?
            Thanks for your answer. My question was a it about different thing.
            Sorry that I haven't formulated it fully. But I'll try now :)

            Let's for instance have the following code:

            *1. static Object[] array = new Object[1];*
            *3. void run() {*
            *5. Object newObject = new Object();*
            *7. array[0] = newObject;*
            *9. }*

            - First, we create a newObject in the line (5) which immediately puts
            it into the Eden space.
            - Then in the line 7 we put it into a static array (the array I assume is already in the Tenured space)

            Now let's imagine that Minor GC starts working. As per my understanding it should
            work only with objects in the Eden space. But since we have a reference from array
            the GC should know somehow about that. But how it can know without touching the array itself?

            I can imagine the following:
            - Either it traverses all roots (not only Eden space roots. Which is unlikely.)
            - Or it counts references when objects are created in the Eden space (Which causes some extra CPU time)
            - Or the newObject is automatically moved to Tenured space when line 7 is executed (Which also causes extra CPU time)
            - Something else???

            Could you please explain what exactly happens in this case?