JavaMemoryModel: Are 'final' fields cause the GC to be more efficient?

From: David Detlefs - Sun Microsystems Labs BOS (david.detlefs@sun.com)
Date: Sun Apr 24 2005 - 11:58:06 EDT


Doron --

That's an interesting idea, but not one that any Java GC that I know
of takes advantage of.

And I think there are some difficulties in the details. The JMM talks
about the view of memory that one mutator thread may see as a result
of actions by other threads. For example, if a constructor
initializes a final field, and then the constructed object is made
reachable by other threads, those threads will see the initialized
value for the final field. (Assuming, if I remember the JMM
correctly, that the constructor doesn't allow its "this" to escape.)

The GC is not like a mutator thread in this sense: it gets to see the
thread's thread-local state. So if I have

  class Foo {
    final public Bar b;
    Foo() {
      Bar bb = new Bar();
      b = bb;
    }
  }
  ...
  Foo f = new Foo();

A GC might occur at the allocation of the Bar in the Foo constructor.
This GC might "promote" the Foo. Now the Foo is in the old
generation, the Bar is allocated in the young, and the initialization
of the final field creates an old-to-young pointer, which would need
to be scanned in the next young-gen collection.

So at the very least, you'd have to take some extra steps to make your
suggestion a valid optimization, steps which become surprisingly
complicated (IMHO).

-- 
=============================================================================
Dave Detlefs                           http://www.sunlabs.com/people/detlefs/
Sun Microsystems Laboratories                           david.detlefs@sun.com
1 Network Drive, Burlington, MA 01803-0902                     (781)-442-0841

------------------------------- JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:01:09 EDT