Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 954867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:17:07+00:00 2026-05-16T00:17:07+00:00

I have been reading these slides about Java finalizers. In it, the author describes

  • 0

I have been reading these slides about Java finalizers. In it, the author describes a scenario (on slide 33) whereby CleanResource.finalize() could be run by the finalizer thread while CleanResource.doSomething() is still running on another thread. How could this happen?

If doSomething() is a non-static method, then to execute that method someone, somewhere must have a strong reference to it… right? So how could this reference get cleared out before the method returns? Can another thread swoop in and null out that reference? If that happened, would doSomething() still return normally on the original thread?

That’s all I really want to know, but for a really above-and-beyond answer, you can tell me why the doSomething() on slide 38 is better than the doSomething() on slide 29. Why is it sufficient to simply invoke this keepAlive() method? Wouldn’t you need to wrap the whole call to myImpl.doSomething() in a synchronized(this){} block?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:17:08+00:00Added an answer on May 16, 2026 at 12:17 am

    EDIT3:

    The upshot is that the finalizer and a regular method can be executed concurrently on the same instance. Here’s an explanation of how that can happen. The code is essentially:

    class CleanResource {
       int myIndex;
       static ArrayList<ResourceImpl> all;
    
       void doSomething() {
         ResourceImpl impl = all.get(myIndex);
         impl.doSomething();
       } 
    
       protected void finalize() { ... }
    }
    

    Given this client code:

    CleanResource resource = new CleanResource(...);
    resource.doSomething();
    resource = null; 
    

    This might be JITed to something like this pseudo C

    register CleanResource* res = ...; call ctor etc..
    // inline CleanResource.doSomething()
    register int myIndex = res->MyIndex;
    ResourceImpl* impl = all->get(myInddex);
    impl->DoSomething();
    // end of inline CleanResource.doSomething()
    res = null;
    

    Executed like that, res is cleared after the inlined CleanResource.doSomething() is done, so the gc will not happen until after that method has finished executing. There is no possibility of finalize executing concurrently with another instance method on the same instance.

    But, the write to res is not used after that point, and given that there are no fences, it can be moved earlier in the execution, to immediately after the write:

    register CleanResource* res = ...; call ctor etc..
    // inline CleanResource->doSomething()
    register int myIndex = res->MyIndex;
    res = null;    /// <-----
    ResourceImpl* impl = all->get(myInddex);
    impl.DoSomething();
    // end of inline CleanResource.doSomething()
    

    At the marked location (<—), there are no references to the CleanResource instance, and so it is eligible for collection and the finalizer method called. Since the finalizer can be called any time after the last reference is cleared, it is possible for the finalizer and the remainder of the CleanResource.doSomething() to execute in parallel.

    EDIT2: The keepAlive() ensures that the this pointer is accessed at the end of the method, so that the compiler cannot optimize away use of the pointer. And that this access is guaranteed to happen in the order specified (the synchronized word marks a fence that disallows re-ordering of reads and writes before/after that point.)

    Original Post:

    The example is saying that the doSomething method is called, and once called, the data referenced via the this pointer can be read early (myIndex in the example). Once the referenced data is read, the this pointer is no longer needed in that method, and the cpu/compiler might overwrite the registers/declare the object as no longer reachable. So, the GC could then concurrently call the finalizer at the same time as the object’s doSomething() method is running.

    But since the this pointer is not used, it’s hard to see how this will have any tangible effect.

    EDIT: Well, perhaps if there are cached pointers to the object’s fields that are being accessed via cache, computed from this before it was reclaimed, and the object is then reclaimed the memory references become invalid. There’s a part of me that has a hard time believing this is possible, but then again, this does seem to be a tricky corner case, and I don’t think there is anything in JSR-133 to prevent this happening by default. It’s a question of whether an object is considered to be referenced only by pointers to its base or by pointers to it’s fields as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 466k
  • Answers 466k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Jon Skeet is likewise unhappy with .Net time calculations, and… May 16, 2026 at 1:46 am
  • Editorial Team
    Editorial Team added an answer Are you calling the hello() function anywhere? If not, you… May 16, 2026 at 1:46 am
  • Editorial Team
    Editorial Team added an answer You need to somehow extend the object returned by Foo#bar… May 16, 2026 at 1:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.