Notification of memory shortage in Java
Asked Answered
D

1

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);
Duenas answered 27/5, 2011 at 22:30 Comment(3)
@Andy That's what I go from reading through the Javadoc. Not fully tested, but the idea is there....Monagan
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.Dex
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.Dex
B
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.

Bigname answered 27/5, 2011 at 22:39 Comment(6)
+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. ;)Gentlemanfarmer
@Andrew I was thinking about suggesting the same thing, but then I saw the answer from @Andy. +1 for this answer.Sweaty
@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.Cowpuncher
@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!Gentlemanfarmer
I am glad I have asked this question, I was nearly going to reinvent the wheel, lol !Monagan
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#gdyrrCerell

© 2022 - 2024 — McMap. All rights reserved.