I am creating a java program, in which some data is added to an arraylist.
Some of the processing is within a loop, and I am adding a new node to the list within the loop, performing operations using that node, and then deleting that node using arraylist.remove() just before end of an iteration of the loop. I am also running System.gc(); just before end of a loop iteration.
However when I took a memory dump, I found that the arraylist still held all values that I had used in different loop iterations… How do I erase all of those values from memory? And I want to do this just before the end of an iteration of loop, so that the arraylist never grows in size?
Java have garbage collector for a reason, and that reason is to programmer don’t have to think about removing object from memory. Only thing you can do (I don’t recommend it) is to force start garbage collector by
System.gc()method. But remember to do it twice because 1st time it will only prepare object to be removed by invoking itfinalize()method.