简体   繁体   English

在Java Web应用程序中,清除所有使用的集合是否可以改善内存管理?

[英]In a java web applications, does clearing any collections used lead to better memory management?

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. 我有一个通过查询填充的list元素,然后通过for循环运行,该循环将该列表发送到前端,一个jsp并打印它们。

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? 想知道是否通过在for循环外调用list.clear()来清除列表对象,那么这会导致更好的内存管理吗? or should one use the weakhashmap for such activities? 还是应该将weakhashmap用于此类活动?

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. 在使用如此多的第三方jar文件和容器的Web应用程序中,性能分析是了解内存使用情况的好方法,但是即使在进行编码时,如果有某种方法可以了解针对特定情况的最佳方法,那么采用方法。 eg 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. 例如,大多数静态站点可能会受益于休眠或toplink类型的缓存,但是一天中有成千上万个条目的站点可能需要使用不同的方法,其中要缓存哪些对象非常有意义。

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! java的优点是选择,java的缺点是选择!

It's best not to try and second guess the GC or you may be surprised at the results. 最好不要尝试再次猜测GC,否则您可能会对结果感到惊讶。 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. 除此之外,直到你一个问题没有解决的一个问题。

You definitely don't want to use a WeakHashMap in this case because elements could be removed before you are done with them. 在这种情况下,您绝对不希望使用WeakHashMap,因为可以在完成处理之前将其删除。 You can clear the collection, but in practice, if the collection is referenced only by local variables, it is not necessary. 您可以清除集合,但是在实践中,如果仅通过局部变量引用集合,则没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM