user502967
user502967

Reputation: 337

java finalize method use

My system suffer from memory leak, i try to optimize it by releasing memory as soon as possible.

  1. Is this a good use of the finalize method?
  2. 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

Answers (5)

Sergey F
Sergey F

Reputation: 96

  1. No, it's not, JVM is good at traversing object graph and removing unused objects. Implementing your own finalize method only slows down performance.
  2. you don't have to

You should use the following strategy for tracking the memory leak:

  1. add the following property to your application -XX:+HeapDumpOnOutOfMemoryError (assuming you are using HotSpot JVM)
  2. load your application with tests of some kind so you eventually get OutOfMemoryError - after that you can find an .hprof file in app directory
  3. download http://www.eclipse.org/mat/
  4. open .hprof file in MAT tool, open Dominators tree and try to find the root cause of the issue

Upvotes: 1

Prabhaker A
Prabhaker A

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

Peter Lawrey
Peter Lawrey

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

user207421
user207421

Reputation: 310869

No. Your finalize() method does exactly nothing relevant that wouldn't already happen by default.

Upvotes: 0

Gyanendra Dwivedi
Gyanendra Dwivedi

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

Related Questions