【发布时间】:2013-09-20 05:35:48
【问题描述】:
我的系统出现内存泄漏,我尝试通过尽快释放内存来优化它。
- 这是对finalize方法的好用吗?
-
我在哪里捕获“Throwable”对象?
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 ... // .....}
【问题讨论】:
-
Java Finalize method call 的可能重复项
-
不,像这样编写
finalize()方法不是一个好主意。不要这样做,而是使用分析器找出导致内存泄漏的原因,然后解决问题。 -
不要尝试优化,而是修复内存泄漏!