Java中是否有任何已发布的功能通知应用程序中的内存不足(或通知它已达到预定义级别)?
我想知道是否可以在某处注册一个监听器(或类似的东西)?我知道Runtime类中的内存方法.我可以自己创建一个计划任务检查剩余内存,但我想知道是否已经有一个现有的解决方案.
我不这么认为,但我正在寻找确认.
对于记录
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); NotificationEmitter emitter = (NotificationEmitter) mbean; NotificationListener listener = new NotificationListener() { @Override public void handleNotification(Notification notif,Object handback) { String notifType = notif.getType(); if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) || notifType.equals(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) { // Retrieve the memory notification information CompositeData cd = (CompositeData) notif.getUserData(); MemoryNotificationInfo info = MemoryNotificationInfo.from(cd); MemoryUsage mu = info.getUsage(); System.out.println("Maximum memory = " + mu.getMax()); System.out.println("Used memory = " + mu.getUsed()); } } }; emitter.addNotificationListener(listener,null,null);
解决方法
我相信你可以使用
MemoryMXBean为内存使用阈值设置一个监听器.示例代码在javadoc链接中提供.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。