7

Is there any delivered feature in Java notifying a memory shortage in an application (or notifying that it has reach a predefined level)?

I was wondering whether it was possible to register a listener (or something equivalent) somewhere? I know about the memory methods in the Runtime class. I could create a scheduled task checking remaining memory myself, but I am wondering whether there is already an existing solution.

I don't think so, but I am looking for a confirmation.

FOR THE RECORDS

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);
3
  • @Andy That's what I go from reading through the Javadoc. Not fully tested, but the idea is there.... May 28, 2011 at 21:18
  • Please note that this code will notify you when the memory usage threshold is exceeded... but you still have to set the threshold value. This has to be done for each existing memory pool.
    – walen
    Nov 23, 2018 at 10:05
  • Also, next time you find a possible solution for your problem, please post it as an answer instead of including the solution code in the question.
    – walen
    Nov 23, 2018 at 10:05

1 Answer 1

6

I believe you can set up a listener for a memory usage threshold using the MemoryMXBean. Sample code is provided in the javadoc link.

6
  • 1
    +1 I was going to suggest a try/catch on an OutOfMemoryError with some safety measures thrown in, but people always grumble at me for even mentioning the 'heresy' of catching an Error. ;) May 27, 2011 at 22:43
  • @Andrew I was thinking about suggesting the same thing, but then I saw the answer from @Andy. +1 for this answer.
    – CRM
    May 27, 2011 at 22:46
  • 1
    @Andrew, It is possible to catch OutofMemoryError (and I have done it before), but handling the Error well without interrupting the flow of the program is pretty difficult.
    – notnoop
    May 27, 2011 at 22:48
  • @notnoop: My strategy involves popping a JOptionPane after a large byte[] has been nulled, so it definitely 'interrupts the flow of the program' in terms of user input! May 27, 2011 at 22:58
  • 1
    You might also want to capture a heap dump when an OutOfMemoryError is thrown. On Hotspot, the -XX:+HeapDumpOnOutOfMemoryError switch does it. The dump can be imported into VisualVM and will help you in analyzing the problem later. oracle.com/technetwork/java/javase/memleaks-137499.html#gdyrr May 28, 2011 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.