Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

jvm - Release java memory for OS at runtime.

Suppose I have a Swings Java Application, I set the min Heap is 64MB and max Heap 2GB, when the user start the application, the log in screen will be displayed, at this time the app uses 64MB, rights? From my Windows 7, I can see the java application is allocated 64MB from the Memory Resource Monitor of the OS (actually, it's more than 64MB because JVM need some memory for it's task).

After that the user does some very heavy job then the application uses 2G. Then the user log out the application, the log in screen is displayed again (the application is not closed yet). At this time the real memory that the application is using 64MB (suppose this is the perfect memory management application), but with the OS this application is still using 2G of RAM, I can see it on the resource monitor of the OS.

I want my application release the memory to the OS when it doesn't need to use a big memory. Can I do it at runtime with java app?

I mean when my application need to use 64MB of Ram, then the OS gives it 64MB only, when it need 2GB of ram then the OS gives it 2GB, after that it need 64MB of ram then the OS gives it 64MB only again, I don't want it waste 2000MB - 64MB = 1936MB.

Can I do that?

Thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have posted my test results over there. Basically, MaxHeapFreeRatio is not respected by every GC implementation, and, to make it worse, it seems to be necessary that there is enough heap activity in order to trigger it in a timely manner, ie. could be that you require 2 full GC runs to actually release the memory to the OS. And if you have a burst memory footprint of X GB, then you probably have to allocate 1 or 2 times of that amount in order to trigger the heap downsize. Or you call System.gc() manually.

If performance is not an issue, and memory footprint is all that counts, try:

-XX:UseSerialGC -Xms16M -Xminf=5 -Xmaxf=10

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...