1

If you Have one large function and have several variables in the function that make new objects. The objects are in heap memory until we are outside the scope of the function then they are marked for GC, right? so if you break the function into several small functions then the objects required for those helper functions get marked for GC before the calling function ends potentially clearing up memory for other things in the calling function, right?

Say I have the following function:

    public ArrayList<File> someProcessing()
    {
        ArrayList<File> files = new ArrayList<Files>();
        // Skipping getting the files for convenience
        
        // Do Something
        LargObject obj = new LargeObject();
        obj.doSomething(files);
        // Do something else
        DifferentLargObject obj2 = new DifferentLargeObject();
        obj2.doSomething(files);
        return files;
    }

Here obj and obj2 would stick around until we are no longer in the scope of someProcessing

if we made 2 more functions:

    private void foo(ArrayList<File> files)
    {
        // Do Something
        LargObject obj = new LargeObject();
        obj.doSomething(files);
    }
    
    private void bar(ArrayList<File> files)
    {
        // Do something else
        DifferentLargObject obj2 = new DifferentLargeObject();
        obj2.doSomething(files);
    }

and changed someProcessing to

 public ArrayList<File> someProcessing()
    {
        ArrayList<File> files = new ArrayList<Files>();
        // Skipping getting the files for convenience
        
        // Do Something
        foo(files);
        // Do something else
        bar(files);
        return files;
    }

After foo(), obj should get flagged for GC right? Therefor, potentially saving memory for later in the function someProcessing(). To clarify, I am simply asking if my assumption is correct. Also, I don't really have an issue like this. It's just a thought.

akuzminykh
  • 4,522
  • 4
  • 15
  • 36
Qwaddles
  • 72
  • 5
  • Hi @Qwaddles being available for garbage collection and being garbage collected are two separate events. - the former doesn't guarantee the later happens any time soon. The latter is a moving target - see https://stackoverflow.com/questions/2129044/java-heap-terminology-young-old-and-permanent-generations for lots of good information on how it has worked in the past. – Mr R Apr 12 '21 at 22:16
  • 1
    "Here obj and obj2 would stick around until we are no longer in the scope of someProcessing" Not necessarily. – Louis Wasserman Apr 12 '21 at 22:28
  • 2
    You are correct, moving the code to separate methods will guarantee that the `LargeObject` instance is *eligible* for GC sooner. Doesn't mean you will see any difference, because: 1) GC might not happen until later anyway, and 2) the JIT compiler might have figured out that the `LargeObject` instance is unused after the `doSomething()` call, so it might release the object instance early anyway. – Andreas Apr 12 '21 at 22:39
  • 1
    There is no general requirement to organize the code that way, see [Can java finalize an object when it is still in scope?](https://stackoverflow.com/q/24376768/2711488). Generally, objects are never “marked for GC”. That’s not how garbage collection works. In some rare cases using such methods may make a difference, but you can achieve the same using `{ … }` blocks or just `new LargeObject() .doSomething(files);` to avoid having unnecessary variables in scope. – Holger Apr 13 '21 at 09:36

0 Answers0