0

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) ?

5
  • 2
    Oops, no I was wrong. We don't have enough code here to tell if this is a memory leak. It's probably not (I disagree with your reviewer) but we'd need more code and also the context (external specification) for this class to tell for sure. Commented Apr 28, 2021 at 16:56
  • 3
    I don't see how this could ever cause a leak, surely the GC would clear up the frames when the instance of Example has no references? Commented Apr 28, 2021 at 17:02
  • 1
    Yes exactly. Ole's answer below could be correct, but we lack any actual context from the OP to say if it is or is not correct. Commented Apr 28, 2021 at 17:14
  • Also @markspace even if there was a leak, wouldn't it be classified as a leak of Example, not a leak of frames? Commented Apr 28, 2021 at 17:30
  • Hi Guys, I made an Edit with details. I need to have a similar code in Java which is same as Splice() in Node Js . Please suggest Commented May 7, 2021 at 1:25

2 Answers 2

2

Just make sure that you remove unused frames from your list. The implementation does remove references to the removed Long objects so that if there are no other references to them, the garbage collector can take care of them if needed. Then there will be no memory leak.

For exmample here is the implementation of one of the remove methods:

public E remove(int index) {
    rangeCheck(index);

    modCount++;
    E oldValue = elementData(index);

    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // clear to let GC do its work

    return oldValue;
}

You notice the line:

    elementData[--size] = null; // clear to let GC do its work
Sign up to request clarification or add additional context in comments.

Comments

0

I think that stopTimeFrame() should remove element from timeFrames list, because you only get element from there and it still exists.

For example:
timeFrames.removeIf(tf -> tf.equals(startTimeStamp));.

If your Collection has duplicated elements - all of them will be deleted if they met criteria.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.