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 6033093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:26:00+00:00 2026-05-23T05:26:00+00:00

I am reading up on weak references in Java after seing a SO post

  • 0

I am reading up on weak references in Java after seing a SO post and realising I didn’t really know what they were.

The following code is from pp. 457, Chapter 17: “Garbage Collection and Memory” in “The Java Programming Language, Fourth Edition” by Arnold, Gosling and Holmes

import java.lang.ref.*;
import java.io.File;

class DataHandler {
    private File lastFile;        // last file read
    private WeakReference<byte[]> 
                         lastData;// last data (maybe)

    byte[] readFile(File file) {
        byte[] data;

        // check to see if we remember the data
        if file.equals(lastFile) {
            data = lastData.get();
            if (data != null)
                return data;
        }

        // don't remember it, read it in
        data = readBytesFromFile(file);
        lastFile = file;
        lastData= new WeakReference<byte[]>(data);
        return data;
    }
}

I am trying to understand, just for the exercise of it, if this code is thread safe, with the part of code I am focusing on being the lines

data = lastData.get();
if (data != null)
    return data;

My thinking is as follows: “data” is thread confined and is set to reference the referent of the “lastData” WeakReference. This creates a strong reference to the referent, hence even if after the null check, all other strong references to the referent outside readFile’s scope disappears (what’s the correct term ?) and even assuming the referent is not softly reachable, the garbage collector is not allowed to clear the weak reference thereby making the referent finalizable, because there still is the local strong reference to it from data. Hence if data is not null in the data != null line, it can not be null when returned in the following line. Correct ?

  • 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-23T05:26:01+00:00Added an answer on May 23, 2026 at 5:26 am

    I think the example code is not thread safe, but for different reasons than using the weak reference:

    The usage of the weak reference is fine, exactly for the reasoning you pointed out: The code creates a strong reference which is held in the data variable. Thus, the GC can’t collect the bytes and thus also the WeakReference will stay intact; so using this code in a single threaded application should be safe. The problems come with multiple threads:

    The access to the file and lastData fields is not synchronized, thus there is no guarantee that two Threads working with the readFile(..) method interact at all (which would be the unlikely “best” case). It is important to note that these fields must be accessed atomically, and if there is no other place where they are touched, the easiest fix would be to declare the readFile method synchronized. This would hurt performance rather badly, because the file reading would then happen inside the synchonized block, possibly causing bad contention.

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

Sidebar

Related Questions

After reading the books, surfing the nets regarding the type of references in Java,
Reading from MSDN: A delegate is a type that references a method. Once a
If my reading of Mike Ash's Zeroing Weak References writeup is correct, weak references
Reading this question I found this as (note the quotation marks) code to solve
Reading source code of sample projects, such as Beast and Bort, are recommended as
Reading this post has left me wondering; are nightly builds ever better for a
reading excel files from C# working well in 32 bit version server. It is
Reading Java's ConcurrentLinkedQueue Docs , I wonder why it is not possible for the
Reading some piece of code and I keep seeing this : public override bool
Reading a post about redis I saw this word: REPL . Can someone tell

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.