简体   繁体   English

Java内存清理

[英]Java memory clean up

We have created the custom test-sutomation framework for our project. 我们已经为我们的项目创建了定制的测试总结框架。 This mimics the interaction (mainly json two way data flow in GET and POST request) between browser and server using HTTP client. 这模仿了使用HTTP客户端在浏览器和服务器之间的交互(主要是GET和POST请求中的json两种数据流方式)。

Here is hot it happens, We have some 1000 test cases automated. 这很热,我们有大约1000个自动测试用例。 Test case execution involves creation of tonnes of messages and hence tonnes of strings; 测试用例的执行涉及创建大量的消息,从而创建大量的字符串。 reading large files repeatedly and verifying the contents and off-course throwing everything after getting verification result. 重复读取大文件并验证内容,并在获得验证结果后将所有内容丢掉。 It goes for all the test cases, takes anywhere between 15 to 20 hours. 它适用于所有测试用例,大约需要15到20个小时。

The problem is that, we are stuck with OutOfMemory Error. 问题是,我们陷入了OutOfMemory错误。 Any Idea?; 任何想法?; on cleaning up the memory which is consumed after reading and parsing from large files 100MB - 250MB. 清理从100MB-250MB大文件读取和解析后消耗的内存。 There are lot of splits, replace, substring operations going on. 正在进行许多拆分,替换,子字符串操作。

Please, Can anybody provide tips,suggestions? 请,有人可以提供提示,建议吗?

-Thanks in advance -提前致谢

This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space. 当由于内存不足而无法分配对象时,Java虚拟机(JVM)会引发此错误,并且垃圾回收器无法释放一些空间。 The OutOfMemoryError objects are created by the JVM when suppression is disabled and/ir the stack trace is not writable. 当禁用抑制和/或堆栈跟踪不可写时,由JVM创建OutOfMemoryError对象。

Solution for OutOfMemoryError: 1. The most obvious solution to this error is to increase the available memory size for the Java Virtual Machine. OutOfMemoryError的解决方案: 1 .此错误最明显的解决方案是增加Java虚拟机的可用内存大小。 If your application requires more memory then, you shall grant it to your application. 如果您的应用程序需要更多的内存,则应将其授予您的应用程序。

2. Verify that your application does not store unnecessary information. 2.验证您的应用程序没有存储不必要的信息。 Store and maintain only those pieces of information required for the proper execution of your Java application. 仅存储和维护正确执行Java应用程序所需的那些信息。

3. You can use the availabe memory analyzer tools, in order to carefully observe the portions of memory occupied by your application. 3.您可以使用可用的内存分析器工具,以便仔细观察应用程序占用的内存部分。 Examples of such tools are the Eclipse Memory Analyzer( http://www.eclipse.org/mat/ ) and Java Heap Analysis Tool (jhat)( http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html ). 此类工具的示例包括Eclipse内存分析器( http://www.eclipse.org/mat/ )和Java堆分析工具(jhat)( http://docs.oracle.com/javase/6/docs/technotes/ tools / share / jhat.html )。

4. Try to configure your JVM to use more memory as shown before (-Xms750m -Xmx2048m -XX:MaxPermSize=1024m ). 4.尝试将JVM配置为使用更多的内存,如前所示(-Xms750m -Xmx2048m -XX:MaxPermSize = 1024m)。

5. Enable Garbage Collection logging (-Xloggc:/var/log/YOUR_APP/YOUR_APP-gc.log) and see how it behaves, how heap is growing. 5.启用垃圾收集日志记录(-Xloggc:/var/log/YOUR_APP/YOUR_APP-gc.log),并查看其行为以及堆的增长方式。 Probably you have a memory leak. 可能是内存泄漏。

6. If so, take a HeapDump, use YourKit to open it and look for objects that use the largest amount of memory. 6.如果是这样,请使用HeapDump,使用YourKit打开它,并查找使用最大内存量的对象。 Try to figure out why and fix it. 尝试找出原因并修复它。

7. You can call Garbage collector using: 7.您可以使用以下方式致电垃圾收集器:

System.gc();

When calling the Garbage collector method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. 在调用垃圾收集器方法时,建议Java虚拟机将精力花在回收未使用的对象上,以使它们当前占用的内存可用于快速重用。 When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects. 当控件从方法调用返回时,Java虚拟机将尽最大努力从所有丢弃的对象中回收空间。

The JVM decides when to execute it. JVM决定何时执行它。 In general if the JVM is about to throw an OutOfMemoryError, calling System.gc() won't prevent it. 通常,如果JVM要抛出OutOfMemoryError,则调用System.gc()不会阻止它。 Better investigate why you're leaking so much memory and clean it up along the way. 最好调查一下为什么您会泄漏这么多内存并一路清理。 But this does not mean that it'll be executed immediately. 但这并不意味着它将立即执行。

Read more about JVM Garbage Collection Tuning SE 6 HotSpot for details on how to tune garbage collection with Java SE 6. 阅读有关JVM Garbage Collection Tuning SE 6 HotSpot的更多信息,以获取有关如何使用Java SE 6调整垃圾收集的详细信息。

Hope this help you. 希望这对您有所帮助。

In general, you should make sure you maximize the objects available for garbage collection. 通常,应确保最大化可用于垃圾收集的对象。 One way to do this is declaring variables in the narrowest possible scope. 一种方法是在尽可能小的范围内声明变量。 (Eg: inside a try block, use method local variables) (例如:在try块中,使用方法局部变量)

You may also have to increase Java heap size . 您可能还必须增加Java堆大小

You can explicitly make the objects to null. 您可以将对象显式设置为null。

Even you can call System.gc(); 即使您可以调用System.gc();

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

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