如何在组件的设计阶段防止内存泄漏?
我正在开发一个基于 Web 的组件,该组件将用 Java 开发。一部分是基于webservices,另一部分是Spring-MVC框架和Hibernate作为持久层。
我仍在设计过程中 - 准备用例、活动图和序列图。
我只是想知道 - 有什么具体方法可以确保设计本身不存在可能导致内存泄漏的东西。遵循正确的编码指南并进行代码审查可以确保程序中不存在内存泄漏,但设计级别又如何呢?我们是否有一些方法来设置上限,以便开发人员不必发挥他的想象力并防止泄漏。
感谢任何建议。
谢谢, 乌杰瓦尔
I am working on a web based component that would be developed in Java. A part of it is based on webservices, while the other has Spring-MVC framework and Hibernate for persistence layer.
I am still in the design process - preparing Use Cases, Activity diagrams and Sequence diagrams.
I was just wondering - What are specific ways to ensure that the design inherently does not have something that may cause a memory leak. Following proper coding guidelines followed by code review would ensure that memory leaks do not exist in the program, but what about design level. Do we have some ways of putting a cap so that developer does not have to run his imagination wild and there by protect against leaks.
Appreciate any suggestions.
Thanks,
Ujjwal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
大多数内存泄漏是由实现错误引起的,在 Java 中,大多数内存泄漏是由于对所使用的技术没有充分了解而导致的。 (例如 4 种不同类型的内部类)
唯一有帮助的方法是花费时间和金钱来提高开发人员的技能。 - 最佳读物 Joshua Bloch 的《Effective Java Second Edition》
另一个经常导致内存泄漏的领域是并发。 - 据我所知,处理这个问题(以及其他并发问题)的最佳方法是在实现它们之前定义哪些组件需要保存(以及哪些在单线程环境中运行)。如果它们需要实现线程保存,那么在实现之前定义它们到底是谁,确保它们是线程保存的(哪个函数和方法由什么保护)。
在设计中你能做的最后一件事是注意静态字段。
您认为可以通过设计做的另一件事是构建一个看门狗。如果您有(例如)一个动态列表,当列表中的某些项目丢失时,这不是一个大问题,并且您知道该列表可能是导致某些问题(线程或实现失败)的原因,那么实现一个如果列表变得比您预期的大得多,看门狗会清除列表。 (然后:修复导致问题的错误!) - 此类列表的一个示例可以是证券交易所 GUI 更新事件列表(在与股票无关的网站中)。如果一个课程更新没有发送到客户端,而是发送下一个(一秒后),那么丢失一个更新比整个服务器崩溃要好得多。
The most memory leaks occurs by implementation faults, in Java most based on an not sufficient understanding of the used technology. (for example 4 different kind of inner classes)
The only way that helps here is spend time and money to the developers skills. - Best reading Effective Java Second Edition by Joshua Bloch
An other area that causes often memory leeks is concurrency. - To my knowledge, the best way to deal with this (and other concurrency problems) is to define which components need to be tread save (and which run in a single thread environment) BEFORE implementing them. And if they need to be thread save implemented, then define BEFORE implementing who EXACTLY they are ensure that they are thread save (which function and method is guarded by what).
The last thing you can do in design, pay attention to static fields.
One other think you can do by design is to build an watchdog. If you have (for example) a dynamic list, where it is not a big problem when some items of the list get lost, and you know the list can be the cause for some trouble (failures in threading or implementation), than implement a watchdog that clear the list if it becomes much bigger than you ever expect. (And then: fix the bug that causes the problem!) - An example for such a list could be an stock exchange GUI update event list (in a not stock relevant web site). If one course update is not send to the client, but the next one (one second later), then the lost of one update is much better than crashing the whole server.
java中最常见的内存泄漏情况之一是缓存:
当连接关闭时,该映射没有被清除,从而出现内存泄漏。
回答您的问题,请提供使用 WeakHashMap 的指南( http://download.oracle.com/javase/6/docs/api/java/util/WeakHashMap.html )用于缓存而不是使用“普通”HashMap。
One of the most common case of memory leak at java is with caching:
When the connection is closed, this mapping is not cleared, having a memory leak.
Answering your question, put at your guide lines to use WeakHashMap ( http://download.oracle.com/javase/6/docs/api/java/util/WeakHashMap.html ) for caching rather than to use the "normal" HashMap.
您应该关心的不仅仅是内存。线程、文件和网络连接等资源也需要考虑。
我可以推荐您阅读面向模式的软件架构,第 3 卷:资源管理模式这本书。它包含一些需要考虑的有用模式。
It is not only memory you should be concerned about. Resources such as threading, files, and network connections are also to be considered.
I can recommend you to read the book Pattern-Oriented Software Architecture, Volume 3: Patterns for Resource Management. It contains some useful patterns to take into consideration.
我不相信你可以在设计阶段防止内存泄漏。然而,使用 JMeter/Grinder/Loadrunner 等传统负载测试工具可以帮助发现内存泄漏,以便在发布之前修复它们。
性能测试应该涉及到开发过程的每一步,就像单元测试和集成测试一样。
I don't believe you can prevent memory leaks in design phase. However using traditional load testing tools like JMeter/Grinder/Loadrunner and such can help discover memory leaks so they can be fixed before a release.
Performance testing should be involved in every step of the development process, just like unit testing and integration testing.
加载系统只是发现资源泄漏的挑战的一半。第二个挑战是拥有适当的监视器和诊断实用程序系统,以了解资源的使用情况以及潜在的影响。
最终,资源泄漏实际上分为四大类:内存、磁盘、网络和 CPU。无论您是查看应用程序代码、Java 或数据库等来宾操作系统,还是核心操作系统,您可以收集的所有指标都是这些项目的子集。
由于监控可能会给系统性能带来开销,工程人员往往会关注顶层统计数据,直到发现问题为止,然后在已识别的资源泄漏类别中为核心和来宾操作系统部署更深层次的监控,以找出问题所在可能的原因。
如果您正在寻找一些轻量级的深度诊断,可以帮助分析回到单元测试和组件组装阶段,然后在整个性能测试工作中使用相同的工具,那么我可以推荐该组织的解决方案,< a href="http://www.rtiperformance.com/" rel="nofollow">http://www.rtiperformance.com/。
James Pulley
主持人:SQAForums Loadrunner|WinRunner、GoogleGroups lr-loadrunner、YahooGroups LoadRunner|Advanced-LoadRunner、LinkedIn LoadRunner(所有者)| LoadRunnerbyTheHour(所有者)
Loading up the system is only one half of the challenge in uncovering the resource leaks. The second challenge is having a proper system of monitors and diagnostic utilities in place to understand how resources are being used, and potentially impinged.
Ultimately the resource leaks really fall into four broad categories: Memory, Disk, Network and CPU. Whether you are looking at your app code, a guest operating system like Java or a Database, or the core operating system, all of the metrics you can collect are subset of these items.
Because monitoring can have an overhead on the system performance engineering staff tend to go light at the top level stats until an issue is identified and then deploy deeper level monitoring in the class of the identified resource leak for both the core and guest OS's to find out the likely cause.
If you are looking for some lightweight deep diagnostics which can help in profiling all of the way back into the unit test and component assembly stages and then carry the same tools all the way through performance testing efforts then I can recommend solutions from this organization, http://www.rtiperformance.com/.
James Pulley
Moderator: SQAForums Loadrunner|WinRunner, GoogleGroups lr-loadrunner, YahooGroups LoadRunner|Advanced-LoadRunner, LinkedIn LoadRunner (owner)| LoadRunnerbyTheHour (owner)