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.