1

I currently print out memory usage information in my application with the following code:

Runtime runtime = Runtime.getRuntime();

NumberFormat format = NumberFormat.getInstance();

long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();

errorLog.warn("free memory: " + format.format(freeMemory / 1024));
errorLog.warn("allocated memory: " + format.format(allocatedMemory / 1024));
errorLog.warn("max memory: " + format.format(maxMemory / 1024));
errorLog.warn("total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024));

The output looks along the lines of the following:

Free Memory: 3,471K
Allocated Memory: 29,572K
Max Memory: 253,440K
Total Free Memory: 227,339K

How does this relate to what I'm seeing in usage from the Task Manager for JVM which is 98,768K?? This is my first time using the Runtime class and checking these values. Looking for possible memory issues in an application that I wrote. I see the Task Mgr value slowly increase over time, but the Runtime class items do not. Though eventually the Task Mgr value will drop dramatically which I guess is the GC running.

Tacitus86
  • 1,314
  • 2
  • 14
  • 36

1 Answers1

1

Toral memory is:

The total allocated space reserved for the java process.

See also this answer: What is runtime total memory ?

However the Task manager reports the total memory that is consumed by the JVM in the machine.

Task manager reports the memory that the OS (windows in this case) has allocated to the windows process running the JVM while Runtime.getTotalMemory() reports the total memory allocated to the current java Process running the thread that executes the method. For every thread Runtime is an instance that"

"Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method." Runtime Javadoc

PS: If you run the programs in an IDE such as Eclipse you may notice big differences as the OS reports the total memory consumed by the IDE (Eclipse is a java program) plus the memory consumed by JVMs spawned to run java programs. Eclipse may start a different JVM instance to run a program. So running a Runtime in a spawned JVM that is used by eclipse reports the total memory inside it.

Spyros K
  • 2,480
  • 1
  • 20
  • 37
  • I would have expected the Total Memory to basically be the heap size, which it is. And the Free + Allocated to be about the same as what task mgr is reporting, which it's not even close. – Tacitus86 Feb 22 '18 at 20:10
  • Not all memory is heap memory, in general it can be broken down in Java Heap ,Stack And Permanent Space. But it can depend on the JVM. Different JVMs may use different memory allocation strategies. For example Java8 changed the permgen to metaspace. So memory wise running a java program in Oracle JRE8 is not the same as running it in Oracle JRE7. – Spyros K Feb 22 '18 at 20:17
  • I'm in JRE8 if that helps. On a Windows Embedded. – Tacitus86 Feb 22 '18 at 20:19
  • So if I have a threaded application, I would have to call Runtime in EACH thread and add them together to get the true memory usage? – Tacitus86 Feb 22 '18 at 20:27
  • 1
    Many threads may belong to the same Process. But a Thread belong to only one process. Threads belonging to the same process should report the same results. See also: https://stackoverflow.com/questions/25342089/processes-vs-threads-in-java#25342139 – Spyros K Feb 22 '18 at 20:33