views:

51

answers:

2

I have a list element which is populated via a query, this is then run through a for loop which sends this list to the front end, a jsp and prints them.

Wanted to know if by calling the list.clear() outside the for loop, it will clear the list object, so will this lead to better memory management? or should one use the weakhashmap for such activities?

In web applications using so many third party jar files and containers, profiling is a good way to know your memory usage, but even then while coding if there is a way to know the best approach to a particular situation it would be easier to adopt an approach. e.g. a mostly static site could benefit with hibernate or toplink type caching, but a site with couple of thousand entries in a day might require a different approach, wherein which objects to cache would make a lot of sense.

Any resources recommended which clearly define which approach to adopt for a particular situation would be greatly appreciated.

The advantage of java is choice, the disadvantage of java is choice!

+5  A: 

It's best not to try and second guess the GC or you may be surprised at the results. Keep it simple: when you're finished with something, dispose of your reference to it. Other than that, don't solve a problem until you have a problem.

cletus
If your list is not used anymore after the loop ends, Java will notice that it is eligible for GC. So will the items that populate it. So +1 with cletus, let de GC do its job :)
Olivier
+1  A: 

You definitely don't want to use a WeakHashMap in this case because elements could be removed before you are done with them. You can clear the collection, but in practice, if the collection is referenced only by local variables, it is not necessary.

Maurice Perry