4

I have a function which runs for a long time and I declare and assign an object inside that function. Now from what I think I know this object will live in memory atleast for as long as this function is running and after that it will be available for garbage collection if no other object references it. Now, I want this object to be available for garbage collection before even function is completed running. In the coding mannger:

     public void foo(){
             String title;
             Object goo=getObject();  //getObject is some other function 
     //which returns Object and can be null and I want Object to be in memory from here
             if(goo!=null)title=goo.getString("title"); 
           //after the last line of code I want Object to be available for garbage 
    // collection because below is a long running task which doesn't require Object.

              for(int i=0;i <1000000;i++)     //long running task
        {
        //some mischief inside with title 
        }

            }

Now from the above task what I want Object to be available for garbage collection before the for loop starts. Can I do something like enclose two lines of code before for loop to be inside curly braces, If no what can I do to achieve this task ?

Kartik Watwani
  • 629
  • 8
  • 20
  • Is object heavy to reside in memory? Is this a reason you want to do this? – Sunil Singhal Oct 22 '17 at 08:18
  • I have asked the question in general, the reason is why to keep unnecessary objects in memory weather they are large or small. – Kartik Watwani Oct 22 '17 at 08:23
  • 1
    For small objects and low memory footprint and no memory crunch, you need not to care. Also, GC is non-deterministic. So, even assigning it to null doesn't assure that it will be collected right away. – Sunil Singhal Oct 22 '17 at 08:51

4 Answers4

4

The object will not stay around during the loop even if you do not do anything. Java will figure out that the object is not reachable once your code passes the point of last access.

A reachable object is any object that can be accessed in any potential continuing computation from any live thread. JVMs are pretty smart these days at figuring out reachability. If it sees that there is no code continuation that could possibly access goo during or after the loop, Java makes the object eligible for garbage collection after the point of last access. Even if the last access checker is very conservative, having no accesses to goo is sufficient to convince JVM that the object is no longer reachable.

See this Q&A for more details.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

After you have finished with the object, assigning null to goo will make it no longer reachable via goo. If there is nothing else keeping it reachable, it will then be eligible for garbage collection.

Arranging your code so that the goo variable goes out of scope might have the same effect, but that approach depends on the JIT compiler / GC's ablity to optimize away the tracing of out-of-scope variables. That is platform dependent.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

You could try assigning

goo = null;

after you get the information you need from it and gc may collect the object if nothing else references it.

For some other ideas see Marking an Object To Be Removed In The GC

user1675642
  • 747
  • 2
  • 5
  • 15
2

You can put the variable, function call and if test in a block. Like,

{ // <-- add this
    Object goo = getObject();
    if (goo != null) { // <-- just because you can omit braces, doesn't mean 
                       //     you should.
        title = goo.getString("title"); 
    }
} // <-- and this, and then goo is out of scope.
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • What is the reason that we should not omit braces when there is only one line of code for `if` statement ? – Kartik Watwani Oct 22 '17 at 08:26
  • 1
    @kartik omitting the braces makes it easier to introduce subtle (or not so subtle) bugs, especially if nested - but enough in general to recommend against the practice. – Elliott Frisch Oct 22 '17 at 08:29