java useful use of garbage collector

127 views Asked by At

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;
}
3

There are 3 answers

0
Peter Lawrey On BEST ANSWER

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.

0
Thihara On

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.

0
Jean Logeart On

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.