Reputation: 337
My system suffer from memory leak, i try to optimize it by releasing memory as soon as possible.
Where to i catch the "Throwable" object?
public class OrderSendBulkHandler extends PandaSoapHandler {
// <login,Data>
private HashMap<String,OrderSendBulkData> followersData = new HashMap<String,OrderSendBulkData>();
// <login,error_code>
private HashMap<String,BulkDataResponse> positionResponses = new HashMap<String,BulkDataResponse>();
private Position position;
private Float guruBalance;
private float partialRatio = -1;
private boolean ignorePosition = false;
@Override
protected void finalize() throws Throwable {
try {
followersData.clear();
followersData = null;
positionResponses.clear();
positionResponses = null;
position = null;
guruBalance = null;
}
catch (Exception e) {
e.printStackTrace();
}
super.finalize();
}
// more code of the class here ...
// .....
}
Upvotes: 1
Views: 3258
Reputation: 96
You should use the following strategy for tracking the memory leak:
Upvotes: 1
Reputation: 8473
Is this a good use of the finalize method?
No.Since here you are making the objects eligible for garbage collection in finalize().
But According to java docs
finalize() method Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
So you can use finally
block instead to make the objects eligible for garbage collection.
These are the SO questions worth reading
1.In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil?
2.Clean up code in finalize() or finally()?
Upvotes: 1
Reputation: 533482
Using finalize() method actually slows the release of memory. When you use finalize, the object has to be added to a queue and thus is not cleaned up until it is called in another thread which means delaying its actual clean up for at least one GC cycle.
If you don't use finalize() it can be cleaned up in one cycle, this included anything the object references as well.
The best way to reduce memory consumption is to use a memory profiler. I would look at reducing the load on the GC by creating less garbage and by reducing the amount of memory retained by looking at the breakdown of the heap esp looking at where objects are allocated.
If you don't have a commercial profiler I suggest getting Java Mission Control supported in Java 7 update 40.
Upvotes: 2
Reputation: 310869
No. Your finalize() method does exactly nothing relevant that wouldn't already happen by default.
Upvotes: 0
Reputation: 5538
Finalize is not the place to do anything related to code optimization for memory leak or so.
In fact thumb rule is that "Never use finalize for anything on which your program logic/success depends".
Upvotes: 4