在运行时释放OS的java内存。 [英] Release java memory for OS at runtime.

查看:294
本文介绍了在运行时释放OS的java内存。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Swings Java应用程序,
我设置最小堆是64MB,最大堆2GB,
当用户启动应用程序时,将显示登录屏幕
,此时该应用程序使用64MB,权利?
从我的Windows 7中,我可以看到java应用程序从操作系统的内存资源监视器中分配了64MB
(实际上,它超过64MB,因为JVM需要一些内存来完成它的任务)。

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).

之后,用户做了一些非常繁重的工作,然后应用程序使用2G。
然后用户注销应用程序,再次显示登录屏幕(应用程序尚未关闭)。此时应用程序
使用64MB的实际内存(假设这是完美的内存管理应用程序),
但是操作系统这个应用程序仍在使用2G的RAM,我可以在操作系统的资源监视器。

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.

我希望我的应用程序在不需要使用大内存时将内存释放到操作系统。我可以在运行时使用java app吗?

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?

我的意思是当我的应用程序需要使用64MB的Ram,
然后操作系统只给它64MB,
当它需要2GB内存然后操作系统给它2GB,
后需要64MB内存然后操作系统再给它64MB,
我不想浪费2000MB - 64MB = 1936MB。

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.

我可以这样做吗?

谢谢,

推荐答案


我希望我的应用程序在不需要使用大内存时将内存释放到操作系统。我可以在运行时使用java app吗?

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?

不,你不能。

在某些情况下,GC会根据自己的意愿将内存释放回操作系统,但我不知道任何允许应用程序告诉GC执行此操作的JVM。最重要的是,GC在这方面相当保守,因为......作为一般规则...... JVM将更有效地运行更多内存,并且不断向OS请求/回馈内存效率低下。

Under some circumstances, the GC will release memory back to the OS of its own accord, but I'm not aware of any JVM that allows an application to tell the GC to do this. And on top of that, the GC is rather conservative about doing this because ... as a general rule ... the JVM will operate more efficiently with more memory, and continually requesting / giving back memory to the OS is inefficient.

请注意,可以使用GC调整选项 -XX:MaxHeapFreeRatio 在GC返回内存之前指定自由堆的最大比率。但是,有并发症。例如,并非所有可用的GC都遵循此选项。如果你打算尝试这种方法,我建议你做一些研究......不要指望奇迹。

Note that the GC tuning option -XX:MaxHeapFreeRatio can be used to specify the maximum ratio of free to used heap before the GC will give memory back. However, there are complications. For example, not all available GCs respect this option. If you are going to try this approach, I suggest you do some research ... and don't expect miracles.

这篇关于在运行时释放OS的java内存。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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