Reputation: 5745
On interview I got a question - "How to help garbage collector when writing code?".
In my opinion GC works really good and we don't need to use anything more than basic good code practices, later added that closing resources in finally clause help GC for sure but generally got suprised with such question.
Is there a good reply? Do we need to do something incredible to help garbage collector?
Upvotes: 2
Views: 1201
Reputation: 19
Garbage collector is very efficent and dexterous in its work but we can enchance its's ability to collect garbage by pointing the variables and objects to null, which are not being used any more and there are no more references to them.
During file handling use the close()
and flush()
methods.
And in thread handling destroy the thread when not in use.
Upvotes: 1
Reputation: 43160
The question is phrased oddly. The GC does not need help with its work. It will work with whatever constraints are imposed on it or fail if the constraints cannot be met.
Of course the work can be altered, but this is not done out of a desire to ease the burden on the GC – it is not a human that can be bored by its work – but due to some ulterior motive, such as improving overall program performance.
These things are normally framed as latency, energy consumption, throughput or memory footprint optimizations. But those are not the only metrics a programmer cares about. Simplicity and code readability also matter.
Naive code can be more readable but less performant. If your goal is easy-to-read code then performing complex optimizations that reduce the GC load could be counter-productive and thus "helping the GC" is not a goal in itself.
Now if your goal is to improve some performance metrics then some optimizations also involve writing code that reduces the work done by the memory management (allocation + GC) subsystem. Some possible optimizations are avoiding finalizers, finding memory leaks, reducing unnecessary allocations, avoiding humongous Object[] arrays, tuning GC parameters, buying better hardware and reducing object lifetimes. Which optimization is applicable depends on the application and can best be figured out with profilers, GC logging and related
Upvotes: 1
Reputation: 6234
The Garbage Collector is a "task" that periodically scans your objects to detect what is not in use (in reality it is a mechanism to emulate a machine with an infinite memory).
For this reason, when you are holding a reference to an object instance you no longer need, you must set it to null
in order to let the Garbage Collector know that you are no longer interested in that instance (and possibly its descendants).
This does not mean that you have to set every variable to null
after use, but you have to watch for fields.
As Java does not have a dispose pattern (see here for more information), you have to design your APIs to mimic it: when you are finished using an object instance which holds a reference you want to free, you have to add an appropriate method to perform such action.
Consider the following example:
class MyClass1
{
int Field1;
int Field2;
int Field3;
int Field4;
}
class MyClass2
{
private MyClass1 m_MyReference;
public MyClass2()
{
m_MyReference = new MyClass1();
}
public void DoSomething()
{
// do something here that uses m_MyReference.
}
}
If an instance of MyClass2 is held but some other reachable instance of your program (e.g. a singleton, or some instance currently in the stack), you will never release the memory associated to MyClass1 as it is still referenced by m_MyReference
.
Is this bad? It depends on that MyClass2 and MyClass1 are truly doing.
If you know that MyClass2 could have a very long lifespan and you want to retain the reference to MyClass2 but to release the memory associated to MyClass1, you have to do something similar to the code below:
class MyClass2
{
private MyClass1 m_MyReference;
public MyClass2()
{
m_MyReference = new MyClass1();
}
public void DoSomething()
{
// do something here that uses m_MyReference.
}
public void Dispose()
{
m_MyReference = null;
}
}
This way you expose to the caller a way to signal that you are no longer holding a reference to instances you do not need.
Remember that simply assigning null
to a variable or field, does not automatically release the memory. The Garbage Collector is asynchronous and runs when it decides so.
Hope to have given the idea without going too deeper.
Upvotes: 2