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 ki开发者_运维知识库nd 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?
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.
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
java -Xmx64M main.class.name
will restrict the heap to 64MB
精彩评论