performance - Java: Reusing vs Reallocating reference to container object? -
tl;dr: in java, better, reusing of container object or creating object every time , let garbage collector work
i dealing huge amount of data in java have following type of code structure:-
version1:
for(...){//outer loop hashset<integer> test = new hashset<>(); //some container for(...){ //inner loop working on above container data structure } //more operation on container defined above }//outer loop ends
here allocated new memory every time in loop , operations in inner/outer loop before allocating empty memory again.
now concerned memory leaks in java. know java has garbage collector instead of relying on should modify code follows:-
version2:
hashset<integer> test = null; for(...){//outer loop if(test == null){ test = new hashset<>(); //some container }else{ test.clear() } for(...){ //inner loop working on above container data structure } //more operation on container defined above }//outer loop ends
i have 3 questions:-
- which perform better, or there no definitive answer.
- will second version have more time complexity? in other other words clear() function o(1) of o(n) in complexity. didn't in javadocs.
- this pattern quite common, version more recommended one?
to opinion it's better use first approach. note hashset.clear
never shrinks size of hash-table. if first iteration of outer loop adds many elements set, hash-table become quite big, on subsequent iterations if less space necessary if won't shrinked.
also first version makes further refactoring easier: may later want put whole inner loop separate method. using first version can move hashset
.
finally note garbage-collection it's easier manage short-lived objects. if hashset
long-lived, may moved old generation , removed during full gc.
Comments
Post a Comment