Java内存管理:iOS风格的内存监控?

发布于 2025-01-08 05:23:14 字数 200 浏览 1 评论 0 原文

我很好奇,在Java中是否有任何方法可以根据需要释放内存——就像iOS中的UIApplicationDidReceiveMemoryWarningNotification一样?

例如,我的程序中可能有一个数组用作缓存结构,当JVM有内存压力(堆空间正在耗尽)时,我可以得到一些通知,通过强制GC释放一些内存并清理数组缓存?

如果您认为问题不清楚,请评论~

I am very curious that whether in Java there is any way to release the memory as needed - just as the UIApplicationDidReceiveMemoryWarningNotification in iOS?

For example, I may have an array used as a caching structure in my program, and when the JVM has memory pressure (heap space is draining), I can get some notification to release some memory by forcing GC and clean the array cache?

Please comment if you think the question is not clear to you~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天赋异禀 2025-01-15 05:23:14

GC 会在需要时自动运行,因此您无需为此发出通知。

对于内存不足时想要手动删除条目的缓存,可以使用 SoftReference。如果仅保留对缓存条目的软引用,则垃圾收集器将在需要更多内存时自动从缓存中删除条目。

已经有相当多的现有缓存实现可以为您处理这个问题(正确处理并不简单),例如在 Guava 库(参见 CacheBuilder)。

The GC is run automatically when needed, so you don't need a notification for this.

For caches where you want to manually remove entries when the memory is low, you can use SoftReferences. If you hold only a soft reference to a cache entry, the garbage collector will automatically remove entries from the cache if it needs more memory.

There are already quite a few existing cache implementations which handle this for you (its not trivial to get it right), for example in the Guava library (cf. CacheBuilder).

め可乐爱微笑 2025-01-15 05:23:14

在 Java 中,通常应该让 GC 为您处理内存。

如果您想要一个在内存压力时被清除的缓存,那么您应该使用基于软引用的缓存(这是弱引用)。如果一个对象仅被软引用引用,那么GC会在内存耗尽之前将其清除。

创建软引用缓存的一种简单方法是使用 Guava 的 缓存生成器

In Java you should generally just let the GC handle memory for you.

If you want a cache that will be cleared when there is memory pressure, then you should use one based on Soft References (which is a variant of the more general category of Weak References). If an object is referenced only by soft references, then the GC will clear it up before it runs out of memory.

An easy way to create soft reference caches is with Guava's CacheBuilder.

滴情不沾 2025-01-15 05:23:14

除了已经提到的 SoftReference 之外,您还可以简单地注册一个在内存使用率约为 95% 时触发的通知:

MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
NotificationBroadcaster emitter = (NotificationBroadcaster) mbean;
emitter.addNotificationListener(new NotificationListener() {
  public void handleNotification(Notification notification, Object handback) {
    System.out.println("Low memory notification");
}
}, null, null);
for (MemoryPoolMXBean memPool : ManagementFactory.getMemoryPoolMXBeans()) {
  if (memPool.isUsageThresholdSupported()) {
    MemoryUsage memUsage = memPool.getUsage();
    memPool.setUsageThreshold((long) (memUsage.getMax() * 0.95));
  }
}

Besides the already mentioned SoftReference you can simply register a notification that triggers e.g. at about 95% memory usage:

MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
NotificationBroadcaster emitter = (NotificationBroadcaster) mbean;
emitter.addNotificationListener(new NotificationListener() {
  public void handleNotification(Notification notification, Object handback) {
    System.out.println("Low memory notification");
}
}, null, null);
for (MemoryPoolMXBean memPool : ManagementFactory.getMemoryPoolMXBeans()) {
  if (memPool.isUsageThresholdSupported()) {
    MemoryUsage memUsage = memPool.getUsage();
    memPool.setUsageThreshold((long) (memUsage.getMax() * 0.95));
  }
}
青衫儰鉨ミ守葔 2025-01-15 05:23:14

Java VM 自行管理内存。
您所能做的就是对缓存对象进行空引用并调用
System.gc()

然后 Java 将清理堆空间。据我所知,您无能为力。

The Java VM manages the memory by itself.
All you can do is to make a null reference to your cache object and call
System.gc()

Java will then clean the heap space. As far as i know there is nothing more you can do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文