GC statistics (was Re: big project ported)

Jeff Sturm jsturm@sigma6.com
Wed Oct 27 09:59:00 GMT 1999


Tom Reilly wrote:
> This is a shot in the dark but would it be possible to reduce the work
> the garbage collector has to do by allocating temporary objects that
> live within the scope of a method call on the stack?  It'd be
> interesting to see some statistics on what percentage of objects
> created in a typical Java program are temporary objects.  I'd suspect
> its pretty high.
> 
> Do any VM's do this?

In practice it is difficult, since you have to be sure the object cannot
live after the stack frame is destroyed.  In a method like:

  public void print() {
    StringBuffer sb = new StringBuffer();

    sb.append("one:");
    sb.append(1);
    sb.append("two:");
    sb.append(2);

    System.out.println(sb);
  }

the `sb' would be a nice candidate for stack allocation, but the
compiler has to be sure the println() method won't store the a reference
to `sb' anywhere.

Also, the potential gain is reduced if the char array allocated
internally by StringBuffer is not also stack allocated.

Another approach to improving allocation times is to pre-allocate a
private chunk of the heap to each thread to reduce contention among
threads.  Each thread can only allocate from its chunk.  When it
exhausts its preallocated chunk, it requests more.  I believe several
VM's use this technique.


-- 
Jeff Sturm
jsturm@sigma6.com


More information about the Java mailing list