I'm cleaning up some code that's started throwing java.lang.OutOfMemoryError
in Production.
The problematic area has a couple of methods that process large collections, e.g.:
public void doSomething(Collection<Widget> targets) {
... do some stuff using TARGETS ...
... do some other stuff NOT using TARGETS ...
}
public void unsuspectingCaller() { // this code may be added in the future
Collection<Widget> myCollection;
... some stuff ...
doSomething(myCollection);
... other stuff ...
kaboom(myCollection); // walks into maintenance trap!
}
The code is blowing up and running out of memory in ... do some other stuff NOT using TARGETS ...
I can fix reduce the memory consumption (allowing early GC) by adding a targets.clear()
in between the two blocks.
But, I do not want to set a trap for future maintainers who might not be aware that the collection is cleared.
Is there an idiomatic way to declare doSomething()
to make it clear, or even compiler verifiable, that the caller of doSomething()
is not supposed to continue using the collection after doSomething()
has been called?
Aucun commentaire:
Enregistrer un commentaire