简体   繁体   English

Java Swing应用程序冻结

[英]Java Swing application freezes out

We have a Java application which is kinda huge and requires too much memory, although I set the initial and maximum memory using Xm flags, but sometimes it freezes, and we get an OutOfMemoryError after 1-2 min (if we're lucky). 我们有一个Java应用程序,它很大,需要太多内存,尽管我使用Xm标志设置了初始和最大内存,但有时会冻结,并且在1-2分钟后会出现OutOfMemoryError (如果幸运的话)。

We need to restart the application if this happens. 如果发生这种情况,我们需要重新启动应用程序。

We run the program by a native c application which simply runs a command line. 我们通过仅运行命令行的本机c应用程序运行该程序。

Questions 问题

  1. What if I don't set the Xmx flag? 如果我没有设置Xmx标志怎么办?
  2. Does it mean unlimited memory usage? 这是否意味着无限的内存使用?
  3. How can I realize if the application is frozen in either C or Java? 我如何知道应用程序是用C还是Java冻结的?

What if I don't set the Xmx flag? 如果我没有设置Xmx标志怎么办?

Then the JVM defaults to a (typically modest) size of memory for the app. 然后,JVM默认将应​​用程序的内存大小设置为(通常为适度)。

Does it mean unlimited memory usage? 这是否意味着无限的内存使用?

No. 没有。

How can I realize if the application is frozen in either C or Java? 我如何知道应用程序是用C还是Java冻结的?

Fix the memory leak 1 . 修复内存泄漏1

Note that the question I answered used 'release' as opposed to 'realize'. 请注意,我回答的问题使用的是“发布”,而不是“实现”。 After careful consideration, I feel the answer should remain exactly the same. 经过仔细考虑,我认为答案应该完全相同。 Either 'releasing (memory)' or 'realizing (there is a problem)' is not going to actually fix the problem. “释放(内存)”或“意识到(存在问题)”都无法真正解决问题。 It is better either way to fix the actual problem (a memory leak) . 解决实际问题(内存泄漏)的任何一种方法都更好。


As an aside. 作为旁白。 I don't think the problem is in the C code, but a simple test (launch the app. from the command line) should reveal either way. 我不认为问题出在C代码中,但是一个简单的测试(从命令行启动应用程序)应该可以揭示这两种方法。 The reason I think the problem is the Java code is that the OutOfMemoryError is detected by a (presumably) fully functional JVM. 我认为这个问题是Java代码的原因OutOfMemoryError是由(大概)全功能的JVM检测。

If the C app. 如果是C app。 had used memory space that the JVM reserved for itself or the app. 使用了JVM为其本身或应用程序保留的内存空间 I guess the error would be very different, possibly along the lines of: JVM internal error.. 我猜想错误可能会大不相同,可能是由于以下原因: JVM internal error..

1. Fixing the memory leak 1.修复内存泄漏

There has been a range of good advice offered by other contributors, that I will revisit here. 其他贡献者还提供了一系列好的建议,我将在这里重新讨论。

  • @ChristianKullmann started it off by stressing the importance of memory profiling . @ChristianKullmann通过强调内存分析的重要性开始了它。
  • @KlasLindbäck followed it up with some excellent resources linked in a comment. @KlasLindbäck随后在评论中提供了一些出色的资源。
    • Java Heap Dump . Java堆转储
      "One way to find memory leaks is analyzing heap dumps. There are several ways to get a heap dump (not including 3rd party tools).." “发现内存泄漏的一种方法是分析堆转储。有多种方法来获得堆转储(不包括第三方工具)。”
    • Tips and tricks for analyzing Java virtual machine heap memory dumps . 分析Java虚拟机堆内存转储的提示和技巧
      "Memory dumps are a very useful feature of the JVM to analyze the contents of the memory at any given time, but their usage requires some experience, and in this post I will share with you some tips and tricks that I've learned over the years, so hopefully they will be useful to you too." “内存转储是JVM在任何给定时间分析内存内容的非常有用的功能,但是内存转储的使用需要一定的经验,在这篇文章中,我将与您分享一些我从中学到的技巧和窍门。年,所以希望它们对您也有用。”
  • @mKorbel rounded it out with some typical problems to look for: @mKorbel解决了一些典型的问题:
    *"search in code if there are close() all: *“如果有close()全部,则在代码中搜索:
    • JDBC, JDBC,
    • FileIO, FileIO,
    • Socket, 插座,
    • etc and in finally block, then you'd love re_engeneering too much"* 等等,在最后的方块中,那么您可能会想过多地进行工程改造” *

Running your application using C native code does not mean that you may be able to steer the whole memory usage by the JVM parameters. 使用C本机代码运行应用程序并不意味着您可以通过JVM参数来控制整个内存的使用。 Try running a memory monitoring for your system (eg TOP on Linux with shift+m for memory usage sorting) or better yet profile your application for memory. 尝试为系统运行内存监视(例如,在Linux上使用shift + m对TOP进行内存使用排序),或者更好地为内存配置应用程序。

Try to get a memory dump after running your application (there are some profiling tools out there, most IDEs provide some feature as well) to check your java objects. 运行应用程序后尝试获取内存转储(那里有一些性能分析工具,大多数IDE也提供某些功能)来检查Java对象。

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

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