In the following code
List<SomeObject> someObjectList = new ArrayList<SomeObject>();
do {
SomeObject someObject = new SomeObject();
someObjectList.add(someObject);
} while(some condition is met);
My Question
- When will someObject be grabage collected?
- Am I leaking memory here?
someObjectwill not be GC’d until your loop exits. Whether this is a memory leak or not is open to interpretation. If you expect it to be freed while still iterating, then you are leaking. If not, then you’re not.