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

  • SEARCH
  • Home
  • 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 7551975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:38:23+00:00 2026-05-30T10:38:23+00:00

Good afternoon all, I was taught that when a function returns, The variables (within

  • 0

Good afternoon all,

I was taught that when a function returns, The variables (within the scope of that function) automatically go out of scope so we do not have to set them to null.

However, this doesn’t seem to be true.

I have a test code that creates a java.lang.ref.PhantomReference pointing to an instance of a java.lang.Object. The only strong reference to that object is within the scope of a function F.

In other words, when that function returns, there should no longer be any strong reference to that object, and the object should now be collectible by the the GC.

However, no matter how hard I try to starve the JVM of memory, the GC simply refuses to collect the object. What is surprising is that if I set the variable to null (obj = null;), the GC now collects the object.

What is the explanation behind this oddity?

public class Test {
    public static void main(String args[]) {
        // currently testing on a 64-bit HotSpot Server VM, but the other JVMs should probably have the same behavior for this use case
        Test test = new Test();
        test.F(new Object());
    }

    public <T> void F(T obj) {
        java.lang.ref.ReferenceQueue<T> ref_queue = new java.lang.ref.ReferenceQueue<T>();
        java.lang.ref.PhantomReference<T> ref = new java.lang.ref.PhantomReference<T>(obj, ref_queue); // if this line isn't an assignment, the GC wouldn't collect the object no matter how hard I force it to 
        obj = null; // if this line is removed, the GC wouldn't collect the object no matter how hard I force it to
        StartPollingRef(ref_queue);
        GoOom();
    }

    private <T> void StartPollingRef(final java.lang.ref.ReferenceQueue<T> ref_queue) {
        new java.lang.Thread(new java.lang.Runnable() {
            @Override
            public void run() {
                System.out.println("Removing..");
                boolean removed = false;
                while (!removed) {
                    try {
                        ref_queue.remove();
                        removed = true;
                        System.out.println("Removed.");
                    } catch (InterruptedException e) { // ignore
                    }
                }
            }
        }).start();
    }

    private void GoOom() {
        try {
            int len = (int) java.lang.Math.min(java.lang.Integer.MAX_VALUE, Runtime.getRuntime().maxMemory());
            Object[] arr = new Object[len];
        } catch (Throwable e) {
            // System.out.println(e);
        }
    }
}
  • 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-30T10:38:24+00:00Added an answer on May 30, 2026 at 10:38 am

    A standards-compliant JVM is never obligated to collect memory. That is to say, you cannot write a program whose correctness depends on a particular bit of memory being collected at a certain time: you can neither force the JVM to collect (even via System.gc()!) nor rely on it doing so.

    So, the behavior you’re observing cannot, definitionally, be wrong: you’re purposefully trying to make the environment do something it is under no onus to do.

    That all said, your issue is that your object has not gone out of scope. It is created in main, then passed – in the normal Java referential manner – to F. Until F returns, the T obj name is still a reference to your object.

    Make goOom static and put a call to it in main, and you should see the object get collected. But, then again, you might still not, and that wouldn’t be wrong…

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

Sidebar

Related Questions

Good afternoon all, I am building a function that takes a string as input,
Good Afternoon All, I have a wizard control that contains 20 textboxes for part
Good Afternoon All, I have written an SSIS 2005 package that contains a conditional
afternoon all. Iv'e come across some mathematical problems that im not too good at.
Good afternoon all Here is my scenario: I have user controls within a master
Good afternoon, I wish to have a script that will look for all files
Good afternoon all. I have a page that displays data in a gridview based
Good Afternoon All, I have two tables in my SQL Server 2005 DB, Main
Good afternoon, all. I'm not too hopeful for a yes here, but if anyone
Good afternoon all. I am aware that if we close() an java.io.OutputStream , it

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.