问题描述
以下是我需要解决的情况。我碰到了两种解决方案。我需要维护从数据库获取的数据缓存,以便在Swing GUI中显示。
当我的JVM内存超过其分配内存的70%时,我需要警告用户过度使用。一旦JVM内存使用率超过80%,那么我必须停止所有的数据库查询并清理作为用户操作一部分获取的现有缓存并通知用户。在清理过程中,我将手动处理删除某些基于某些规则的数据并指示JVM进行GC。每当GC发生时,如果内存清理并达到分配内存的60%,则需要重新启动所有数据库处理并将控制权交还给用户。
为了检查JVM内存统计信息,我发现了以下两种解决方案。 Runtime.freeMemory() - 创建线程,每10秒运行一次并检查是否有空闲内存,如果内存超出了上述限制,必要的弹出窗口会隐藏用户,并会调用这些方法来暂停操作并释放内存。
MemoryPoolMXBean.getUsage() - Java 5引入了JMX以在运行时获取内存快照。在,JMX我不能使用阈值通知,因为它只会在内存达到/超过给定的阈值时通知。只有在MemoryMXBean中使用轮询方式并在一段时间内检查内存统计信息。在使用轮询的情况下,对于我来说这两种实现看起来都是一样的。
请建议这些方法的优点,以及是否有任何其他替代方法/使用方法的任何更正。
只是一个附注: Runtime.freeMemory() doesn' t表示剩余分配的内存量,它只是当前分配的内存中的空闲内存量(它最初小于VM配置使用的最大内存量),但随着时间的推移而增长。
启动虚拟机时,最大内存( Runtime.maxMemory())只是定义了虚拟机可能的内存上限分配(使用-Xmx VM选项配置)。
总内存( Runtime.totalMemory())是为VM进程分配的内存的初始大小(可使用-Xms VM选项配置),以及将在您每次分配的空间比当前空闲部分多( Runtime.freeMemory())时动态增长,直到它达到最大内存。
您感兴趣的指标是可用于进一步分配的内存:
long usableFreeMemory =运行时.getRuntime().maxMemory()
-Runtime.getRuntime()。totalMemory()
+ Runtime.getRuntime()。freeMemory()
或:
double usedPercent =(double)(Runtime .getRuntime()。totalMemory()
-Runtime.getRuntime()。freeMemory())/ Runtime.getRuntime()。maxMemory()
Following is the scenario i need to solve. I have struck with two solutions.
I need to maintain a cache of data fetched from database to be shown on a Swing GUI.Whenever my JVM memory exceeds 70% of its allocated memory, i need to warn user regarding excessive usage. And once JVM memory usage exceeds 80%, then i have to halt all the database querying and clean up the existing cache fetched as part of the user operations and notifying the user. During cleanup process, i will manually handle deleting some data based up on some rules and instructs JVM for a GC. Whenever GC occurs, if memory cleans up and reaches 60% of the allocated memory, I need to restart all the Database handling and giving back control to the user.
For checking JVM memory statistics i found following two solutions. Could not able to decide which is best way and why.
Runtime.freeMemory() - Thread created to run every 10 seconds and check for the free memory and if memory exceeds the limits mentioned, necessary popups will intimate user and will call the methods to halt the operations and freeing up the memory.
MemoryPoolMXBean.getUsage() - Java 5 has introduced JMX to get the snapshot of the memory at runtime. In, JMX i cannot use Threshold notification since it will only notify when memory reaches/exceeds the given threshhold. Only way to use is Polling in MemoryMXBean and check the memory statistics over a period.
In case of using polling, it seems for me both the implementations are going to be same.
Please suggest the advantages of the methods and if there are any other alternatives/any corrections to the methods using.
Just a side note: Runtime.freeMemory() doesn't state the amount of memory that's left of allocating, it's just the amount of memory that's free within the currently allocated memory (which is initially smaller than the maximum memory the VM is configured to use), but grows over time.
When starting a VM, the max memory (Runtime.maxMemory()) just defines the upper limit of memory that the VM may allocate (configurable using the -Xmx VM option).The total memory (Runtime.totalMemory()) is the initial size of the memory allocated for the VM process (configurable using the -Xms VM option), and will dynamically grow every time you allocate more than the currently free portion of it (Runtime.freeMemory()), until it reaches the max memory.
The metric you're interested in is the memory available for further allocation:
long usableFreeMemory= Runtime.getRuntime().maxMemory() -Runtime.getRuntime().totalMemory() +Runtime.getRuntime().freeMemory()
or:
double usedPercent=(double)(Runtime.getRuntime().totalMemory() -Runtime.getRuntime().freeMemory())/Runtime.getRuntime().maxMemory()
这篇关于在Java中查找内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!