How do you detect low memory situations within the java virtual machine?

I've been getting some OutOfMemory errors lately in my application. Is it possible to detect ahead of time when the virtual machine is running low on memory? In other words to preemptively deal with OutOfMemory errors before they actually occur?


Asked by: Emma673 | Posted: 21-01-2022






Answer 1

Java (as of Java 5) now has a standard JMX bean that can be used to receive low memory notification. See java.lang.management.MemoryMXBean.

Answered by: Daryl707 | Posted: 22-02-2022



Answer 2

I suggest that a better question is "Why is my application running out of memory?"

You may need to increase the memory available to the JVM at startup; You may have a memory leak (objects that aren't getting released when they're no longer needed.)

More information on the application might also be helpful

  • Does it run continuously, 24x7x365?
  • Does it run once and exit?

Using performance monitoring/profiling tools to find the cause of the memory problem is really your best bet.

Answered by: Leonardo109 | Posted: 22-02-2022



Answer 3

Have a look at the freeMemory method of Runtime (see http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html for details)

In short: do

Runtime.getRuntime().freeMemory()

Answered by: Lucas131 | Posted: 22-02-2022



Similar questions

java - Using Different Hibernate User Types in Different Situations

I am using Hibernate + JPA as my ORM solution. I am using HSQL for unit testing and PostgreSQL as the real database. I want to be able to use Postgres's native UUID type with Hibernate, and use the UUID in its String representation with HSQL for unit testing (since HSQL does not have a UUID type). I am using ...


java - In what situations is static method a good practice?

I have read the following discussions: Should private helper methods be static if they can be static , and Should all methods be static if their class has no member variables It seems that people in general would accept static methods, but ar...


java - What are 3 possible situations in which static methods might be included in classes?

I got this question for homework, and all I can think of is to return a subclass of an abstract superclass. Thanks!


c# - What situations does a Monostate pattern model?

I know what both a Singleton or a Monostate are and how to implement them. Although I can see many uses for a Singleton, I can't imagine a situation where I would want to let the user create as many instances of my class although in reality only one really exists behind the scenes. Can anybody help me here? I know that for several reasons one should stay away from both patterns, but in theory, what kind of problems...


java - Make Ant use a different build class in different situations?

We use Ant to build a Java web application. The application will compile and run on both Tomcat 6 and Tomcat 5.5. However, the build process is slightly different depending on which version of Tomcat you have installed: 5.5 keeps jar files that the build needs in $CATALINA_HOME/common/lib and $CATALINA_HOME/server/lib, while 6.0 keeps them all in $CATALINA_HOME/lib.


java - Log messages lost in few specific situations

I am using java.util.logging to do all the logging of my application. Until recently, I was using the logging facility without any specific configuration. Everything worked as expected, all the logs were visible in the console (stderr) Now, I wanted to customize the configuration for my logs. I want the logs to be displayed on the console, but I want them to be written in a file, too. I came up...


When are the situations when those brackets < > are used in Java?


java - will deadlock occur in these situations?

will deadlock occur in these Java situations 1- synchronized(obj) { obj.syncMethod(); // the method signature: public synchronized void syncMethod() {...} } 2- synchronized(obj) { if (condition) throw new Exception(); // deadlock because obj lock is not released? // do other stuff } Thank you.


java - In what situations would you prefer an abstract class without methods?

I am going through some Java code and I see lots of abstract classes which contain nothing in them. For eg. something like this - public abstract class Processor { } They have concrete implementation classes though. In what situations would such abstract classes make sense?


java - How to write a unit test in situations where it is obvious by "looking" that the test passed?

Sometimes, I encounter situations where all I need to test is whether the program's execution reaches a certain point without any exceptions being thrown or the program being interrupted or getting caught in an infinite loop or something. What I don't understand is how to write a unit test for that. For instance, consider the following "unit test" - @Test public void testProgramExecution(...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top