[concurrency-interest] [Javamemorymodel-discussion] Fences.keepAlive

Boehm, Hans hans.boehm at hp.com
Tue Jan 20 14:20:39 EST 2009


I'd be inclined to somewhat simplify the example, and not use the constructor.  I'm also not sure I'd mention the word "native", since that seems to mislead people to believe that this is a native code issue.  Something like:

* class Resource {
*   static Resource[] externalResourceArray = ...
*   int myIndex;
*   Resource(...) {
*     myIndex = ...
*     externalResourceArray[myIndex] = ...;
*     ...
*   }
*   protected void finalize() {
*     externalResourceArray[myIndex] = null;
*     ...
*   }
*   void foo() {  // This might be the last use of this object.
*      int i = myIndex;
*      //  Without keepAlive, this object could be finalized
*      //  here, and externalResoureArray[i] could be
*      //  cleared, since the object itself may not otherwise
*      //  be needed; if there are no later accesses, only
*      //  the index i is still live.
*      bar(externalResourceArray[i]);
*      keepAlive(this);
*   }
* }

Hans

> -----Original Message-----
> From: javamemorymodel-discussion-bounces at cs.umd.edu 
> [mailto:javamemorymodel-discussion-bounces at cs.umd.edu] On 
> Behalf Of Doug Lea
> Sent: Monday, January 19, 2009 3:15 AM
> To: Jeremy Manson
> Cc: Concurrency-interest at cs.oswego.edu; 
> javamemorymodel-discussion at cs.umd.edu
> Subject: Re: [Javamemorymodel-discussion] 
> [concurrency-interest] Fences.keepAlive
> 
> Jeremy Manson wrote:
> > Oh, right, we always meant to do that.  Good idea!
> > 
> > I have two concerns with the javadoc:
> > 
> > First, I think it might be unclear to the reader that the problem 
> > arises in that example, because nothing is seen to call the 
> > constructor.
> 
> Can you (or anyone) think of a better short example?
> 
> > Second, I think it might be helpful to have a stronger statement of 
> > the problem in the Javadoc.  When I explain that this can happen to 
> > people, they are usually mystified and then shocked.  
> Something like 
> > "When a runtime can detect that no reference to an object 
> will ever be 
> > stored in the heap, the garbage collector can reclaim that 
> object at 
> > any time.  This can happen even if the object itself, of fields of 
> > that object, are still in use.  See Section 12.6.1 of the Java 
> > Language Specification for more details."  might be more complete.
> > 
> 
> Thanks! I added a light edit of this.
> 
> -DOug
> 
> 
> _______________________________________________
> Javamemorymodel-discussion mailing list
> Javamemorymodel-discussion at mcfeely.cs.umd.edu
> https://mailman.cs.umd.edu/mailman/listinfo/javamemorymodel-discussion
> 


More information about the Concurrency-interest mailing list