I have a scenario as below :
class Example {
private List<Long> timeFrames; // Line 2
private Example() {
this.timeFrames = new ArrayList<>();
}
public static Example getExample() { return new Example(); }
...
public Integer startTimeFrame() {
Long startTimeStamp = new Date().getTime();
this.timeFrames.add(startTimeStamp);
return this.timeFrames.indexOf(startTimeStamp);
}
public Long stopTimeFrame(Integer timeFrameIdentifier){
Long startTimeStamp = this.timeFrames.get(timeFrameIdentifier);
return new Date().getTime() - startTimeStamp;
}
}
Now, during the code review my architect has given the following comment at Line No. 2 - "This is potentially be the cause of memory leak because you never clear the elements"
How can we handle this ?
EDIT :
I have updated the Java Code . Actually we have code in Node Js , which we are converting into Java .
In the Node Js code , we have the "stopTimeFrame()" method as below :
public stopTimeFrame(timeFrameIdentifier: number): number {
const startTimeStamp = this.timeFrames.splice(timeFrameIdentifier, 1)[0]
return new Date().getTime() - startTimeStamp;
}
So, In Node Js code, they are using 'Splice()' method. I don't have much knowledge on Node Js . So I just googled what is the usage of splice() in Node Js.
As per documentation (w.r.t the above code ), the splice() method adds new items at position 'timeFrameIdentifier' and remove 1 item.
So, I think my Reviewer meant this when he said I am not clearing the elements.
Can you please help me how can we convert the "stopTimeFrame()" method in Java so its functionality is same as in Node Js (where its using splice() to remove an item everytime) ?
frameswhen the instance ofExamplehas no references?Example, not a leak offrames?