Assigning “null” to objects in every application a

2020-04-05 19:23发布

  • Do you always assign null to an object after its scope has been reached?

  • Or do you rely on the JVM for garbage collection?

  • Do you do it for all sort of applications regardless of their length?

  • If so, is it always a good practice?

14条回答
霸刀☆藐视天下
2楼-- · 2020-04-05 19:56

I declare almost all of my variables as "final". I also make my methods small and declare most variables local to methods.

Since they are final I cannot assign them null after use... but that is fine since the methods are small the objects are eligible for garbage collection once they return. Since most of the variables are local there is less chance of accidentally holding onto a reference for longer than needed (memory leak).

查看更多
一夜七次
3楼-- · 2020-04-05 19:56

Assignin null to a variable does not implicitly mean it will be garbage collected right away. In fact it most likely won't be. Whether you practice setting variables to null is usually only cosmetic (with the exception of static variables)

查看更多
爷、活的狠高调
4楼-- · 2020-04-05 19:56

Assigning is not done to objects, it is done to variables, and it means that this variable then holds a reference to some object. Assigning NULL to a variable is not a way to destroy an object, it just clears one reference. If the variable you are clearing will leave its scope afterwards anyway, assigning NULL is just useless noise, because that happens on leaving scope in any case.

查看更多
smile是对你的礼貌
5楼-- · 2020-04-05 19:57

There was a class of memory leak bugs that happened regardless of whether I set the reference to null - if the library I was using was written in a language like C without memory management, then simply setting the object to null would not necessarily free the memory. We had to call the object's close() method to release the memory (which, of course, we couldn't do after setting it to null.)

It thus seems to me that the de facto method of memory management in java is to rely on the garbage collector unless the object/library you're using has a close() method (or something similar.)

查看更多
成全新的幸福
6楼-- · 2020-04-05 19:59

The one time I tend to use this practice is if I need to transform a large Collection in some early part of a method.

For example:

public void foo() {
  List<? extends Trade> trades = loadTrades();
  Map<Date, List<? extends Trade>> tradesByDate = groupTradesByDate(trades);
  trades = null; // trades no longer required.

  // Apply business logic to tradesByDate map.
}

Obviously I could reduce the need for this by refactoring this into another method: Map<Date, List<? extends Trade>>> loadTradesAndGroupByDate() so it really depends on circumstances / clarity of code.

查看更多
我命由我不由天
7楼-- · 2020-04-05 20:00

- Do you always assign null to an object after its scope has been reached?

No

- Or do you rely on the JVM for garbage collection?

Yes

- Do you do it for all sort of applications regardless of their length?

Yes

- If so, is it always a good practice?

N/A

查看更多
登录 后发表回答