Java应用程序内存使用 [英] Java application memory usage

查看:133
本文介绍了Java应用程序内存使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写一个小的java应用程序(我的第一个!),目前只做了一些事情。目前,它运行Main类,它启动一个gui类(我编写的一个扩展JFrame的类,只包含一个JTextArea),一个通过大约40kb的BufferedInputStream加载本地文件的类,以及一个从一个类加载一个条目的类。 Java属性文件。

I have been writing a small java application (my first!), that does only a few things at the moment. Currently, it runs the Main class which launches a gui class (a class I wrote that extends JFrame that only contains a JTextArea), a class that loads a local file through a BufferedInputStream that is approximately 40kb, and class that loads a entry from a Java properties file.

一切都运行得非常好,但是,我正在看Windows任务管理器,我注意到一些让我觉得奇怪的事情。当我启动应用程序时,RAM使用量会在加载本地文件时跳转到大约40MB,并从中提取一些值以显示在JTextArea中,这对我来说是正常的,因为JVM,Java基类等等。但是,当应用程序完成加载文件时,它只是闲置,因为我目前还没有做任何其他事情。当它处于空闲状态时,只要窗口处于活动状态,应用程序的内存使用量就会每秒开始上升10-20kb。这让我很奇怪。如果我点击另一个程序使这个程序成为非活动窗口,内存仍会上升,但速度要慢得多(每3-5秒约10kb)。

Everything works wonderfully, however, I was watching the Windows task manager and I noticed something that struck me as odd. When I launch the application, the RAM usage jumps to about 40MB while it loads the local file and pulls a few values from it to display in the JTextArea, which seems normal to me because of the JVM, Java base classes, etc. At this point, however, when the application has finished loading the file, itmerely sits idle, as I currently don't have it doing anything else. While it is sitting idle, as long as the window is active, the application's memory usage starts climbing by 10-20kb every second. This strikes me as odd. If I click on another program to make this one the inactive window, the memory still rises, but at a much slower rate (about 10kb every 3-5 seconds).

我没有测试过它会走多远,但这让我觉得很奇怪。这是正常的Java行为吗?我想我的代码可能会泄漏内存,但我不确定如何。我确实关闭了我正在使用的BufferedInputStream,但我看不出还有什么会导致这种情况。

I have not tested to see how far it would go up, but this strikes me as very odd behavior. Is this normal Java behavior? I guess it is possible that my code could be leaking memory, but I'm not sure how. I did make sure to close the BufferedInputStream I am using, and I can't see what else would cause this.

如果我的解释不成功,我很抱歉感觉,但我很感激任何人可能有的见解和/或指示。

I'm sorry if my explanation doesn't make sense, but I would appreciate any insight and/or pointers anyone may have.

更新:

建议,我基本上将我的应用程序剥离到Main类,它只调用gui类。 gui类只扩展JFrame并设置窗口大小,关闭操作和可见属性。随着这些变化,内存仍然以10-20kb增长,但速度较慢。这与我收到的其他建议相结合,让我相信这只是Java。如果我发现其他任何有趣的东西,我将继续玩它并让大家知道。

Upon suggestion, I basically stripped my application down to the Main class which simply calls the gui class. The gui class only extends JFrame and sets the window size, close operation, and visible properties. With these changes, the memory still grows at 10-20kb, but at a slower rate. This, in conjuction with other advice I have received leads me to believe that this is just Java. I will continue to play with it and let you all know if I find out anything else interesting.

推荐答案

尝试监控堆使用情况使用jconsole而不是Windows任务管理器:

Try monitoring the heap usage with jconsole instead of the Windows task manager:


  • 使用-Dcom.sun.management.jmxremote选项启动应用程序,例如

java -Dcom.sun.management.jmxremote -jar myapp.jar


  • 从命令行启动 jconsole ,并连接到本地的pid你在最后一步开始的java进程。

  • 点击内存并观察堆内存(默认显示)

  • Launch jconsole from the command line, and connect to the local pid of the java process you started in the last step.
  • Click over to memory and watch heap memory (the default display)

如果你观看一段时间,随着时间的推移,你可能会得到一个锯齿的模式,但是当垃圾收集器运行时它会急剧下降。您可以尝试通过单击如此标记的按钮来建议垃圾收集。

If you watch for a while, you'll probably get a "sawtooth" pattern as the memory climbs over time, but then has sharp drop-offs when the garbage collector runs. You can try to "suggest" garbage collection by clicking the so-labelled button.

执行此操作时,内存使用量是否会下降到相同的最低级别,或者是几分钟内总体最小值是否会增加?如果最小使用量增加,那么您有内存泄漏。如果它总是返回到相同的最低水平,那么你没事。

When you do this, does the memory usage drop down to the same minimum level, or is the overall minimum increasing over the course of several minutes? If the minimum usage increases, then you have a memory leak. If it always returns to the same minimum level, then you're fine.

这篇关于Java应用程序内存使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆