0

In function header i have this situation:

Should i stay the "=null" in the end, or Java when exiting function do that itself?

My intention it to make this objects as soon as possible ready for garbage collection. This is a soft real time system.

public BulkDataResponse(Double closePrice, Integer closeTime, Integer cmd,
        String comments, Double commission, Integer digits,
        Integer errorCode, Integer expiration, String login,
        Integer magic, Double openPrice, Integer openTime,
        Integer positionOrder, Double profit, Double rateClose,
        Double rateMargin, Double rateOpen, Integer sourceId,
        Double stopLoss, Double swap, String symbol, Double takeProfit,
        Double taxes, Integer timeTick, Double volume) 
{
    super();
    this.closePrice = closePrice;
    this.closeTime = closeTime;
    this.cmd = cmd;
    this.comments = comments;
    this.commission = commission;
    this.digits = digits;
    this.errorCode = errorCode;
    this.expiration = expiration;
    this.login = login;
    this.magic = magic;
    this.openPrice = openPrice;
    this.openTime = openTime;
    this.positionOrder = positionOrder;
    this.profit = profit;
    this.rateClose = rateClose;
    this.rateMargin = rateMargin;
    this.rateOpen = rateOpen;
    this.sourceId = sourceId;
    this.stopLoss = stopLoss;
    this.swap = swap;
    this.symbol = symbol;
    this.takeProfit = takeProfit;
    this.taxes = taxes;
    this.timeTick = timeTick;
    this.volume = volume;

    closePrice = null;
    closeTime = null;
    cmd = null;
    comments = null;
    commission = null;
    digits = null;
    errorCode = null;
    expiration = null;
    login = null;
    magic = null;
    openPrice = null;
    openTime = null;
    positionOrder = null;
    profit = null;
    rateClose = null;
    rateMargin = null;
    rateOpen = null;
    sourceId = null;
    stopLoss = null;
    swap = null;
    symbol = null;
    takeProfit = null;
    taxes = null;
    timeTick = null;
    volume = null;
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
user502967
  • 327
  • 1
  • 3
  • 13
  • 1
    This is the job of the garbage collector, not yours. Also, if you want to mark the object as eligible for garbage collection, just set the variable as `null`, not its fields. – Luiggi Mendoza Sep 12 '13 at 08:06
  • *"Should i stay the "=null" in the end"* - No, I'm pretty sure it will have absolutely no effect, as you have assigned a reference to (I presume instance variables), which now have a strong reference to the values you have passed to the constructor. Even if the caller `null`s their references, so long as there is a strong reference to your `BulkDataResponse` instance, the values won't be GC'ed – MadProgrammer Sep 12 '13 at 08:07
  • possible duplicate of [Does assigning objects to null in Java impact garbage collection?](http://stackoverflow.com/questions/449409/does-assigning-objects-to-null-in-java-impact-garbage-collection) and http://stackoverflow.com/questions/2931170/does-variable-null-set-it-for-garbage-collection – paulsm4 Sep 12 '13 at 08:09

3 Answers3

1

If you are worried about garbage, I suggest you don't create so much in the first place. Use primitives instead of objects and about half your garbage will disappear. Also you don't need to clear a field which is about to go out of scope anyway. With any luck the JIT is smart enough to remove the code which does do anything.

This is a soft real time system.

For high frequency trading system my goal is to create less than one object per price movement on average. This leads to almost no garbage being produced or needing to be cleaned up.

I suggest you consider how you could change your code to remove garbage creation.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

It's rather pointless considering you have already just assigned the objects so other variables.

So whether you assign the parameters to null or not doesn't matter because you object's instance variables already point to the data your parameter variables already points to.

Oh and I almost forgot, what if the parameters you passed are already referenced somewhere as well? Basically your parameters will go out of scope after your constructive finishes execution so there is no effect to what you are doing. Well except to make your code look ugly... And may be posted in the daily WTF.

Thihara
  • 7,031
  • 2
  • 29
  • 56
0

The scope of the references to the objects is limited to the scope of the method. Therefore, calling null at the end of the method will not change anything.

In addition, it is usually good practice to have your arguments passed as final to show the intention of not re-affecting the references inside the method, thus making the code more readable. Doing so would actually prevent you from calling = null on the arguments.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118