java 内存性能 - 奇怪的行为(或者这是正常的?)
你好呀 首先我想说我是一个初学者,但我正在开发一个非常小且简单的 Java 应用程序,这真的不应该引起一些重大问题。 我通过 Windows 任务管理器监视内存使用情况,并注意到在我的应用程序启动后,java.exe 使用了大约 70MB 的可用内存。所以我心里想,好吧,这可能有点大,但仍然没有我的电脑无法处理的。但当我尝试调整窗口大小时,真正奇怪的事情开始发生,内存使用量突然跃升至 80-90 MB,如果我继续拖动窗口,随机调整大小,它会不断增加内存使用量。我认为这与在窗口调整大小期间调用 GUI 组件上的重绘方法有关,因此我采取了一些可能导致某种内存泄漏的可疑组件,并从我的主窗口窗体中删除了它们,使我的程序几乎完全被剥离,但这个问题仍然存在。后来我注意到,如果我不断调整窗口大小,内存使用量会增加到 200-220 MB,然后停止这种不受控制的增长。 那么有人可以告诉我,考虑到java中的内存管理,这可能是正常行为吗?
Hi there
I would like to start by saying that i'm a beginner, but i'm working on a really small and simple Java app, that really shouldn't cause some major problems.
I was monitoring memory usage from windows task manager, and noticed that with my application started, java.exe was using about 70MB of available memory. So I thought to myself, ok, this probably is a little large, but still, nothing that my PC couldn't handle. But really strange thing started happening when i tried to resize my window, memory usage suddenly jumped to like 80-90 MB, and if i would continue dragging my window, randomly resizing, it kept increasing memory usage. I thought it has something to do with calling repainting methods on GUI components during window resize, so i took a few suspicious components that could cause some kind of memory leak, and deleted those from my mainwindow form, leaving my program almost completely stripped down, but this issue persisted. What i noticed later was that if i keep resizing my window, memory usage grows up to 200-220 MB, and then stops this uncontrolled growth there.
So can somebody tell me, could this be a normal behavior having in mind memory management in java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建的 Java 对象在使用完毕后不一定会被清除。相反,称为“垃圾收集器”的东西会定期在后台运行,查找孤立对象并删除它们,从而释放内存。
您的应用程序在调整窗口大小时可能会创建大量临时对象。尽管不再被任何对象(即孤儿)引用,这些对象仍会一直挂在垃圾收集器运行之前。
您可能会发现您的最大内存为 256M(默认值) - 当您接近最大内存时,垃圾收集器可能会被更频繁地调用,并且新对象的创建需要立即释放内存 - 因此内存徘徊低于 256M,因为创建/删除率由需求平衡。
这是完全正常的行为。
Java objects created are not necessarily cleaned up once they're finished with. Instead, something called the "garbage collector" periodically runs in the background looking for orphaned objects and deletes them, freeing up memory.
Your application is likely creating lots of temporary objects as it resizes your window. Although no longer being referenced by anything (ie orphans), these objects are hanging around until the garbage collector runs.
You'll probably find that your max memory is 256M (the default) - the garbage collector is probably being called more often as you approach your max memory and the creation of new objects requires memory to be freed up immediately - hence the memory hovering just under 256M as the creation/deletion rate is balanced by demand.
This is completely normal behaviour.
不,这种行为是完全正常的。 Java内存管理基于自动垃圾收集,这意味着未使用的内存在被垃圾收集之前会积累一段时间(因为这是一项大量的工作,所以你希望尽可能少地这样做。
所以JVM会倾向于使用它允许使用的大部分内存(最大堆大小) - 在具有多个 GB 可用内存的现代 PC 上,默认的最大堆大小将相当大,但是,如果您有一个您知道的小应用程序。不需要太多内存,您可以通过命令行选项 -Xmx 调整最大堆大小,例如
将堆限制为 64MB
No, this behaviour is perfectly normal. Java memory management is based on automatic garbage collection, which means that unused memory accumulates for a while before being garbage collected (because that is a significant amount of work, you want to do it as rarely as possible.
So the JVM will tend to use a large part of the memory it's allowed to use (the maximum heap size) - and on a modern PC with multiple GBs of memory available, the default maximum heap size will be pretty big. However, if you have a small app that you know won't need much memory, you can adjust the maximum heap size via the command line option -Xmx, for example
will restrict the heap to 64MB