• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check how much memory is used by a program??  RSS feed

 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

I want to check how much memory is available before runny a piece of code and how much is available afterwards. What function do i need to use??.


Pooja
 
Eshwin Sukhdeve
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1) to get the maximum amount of memory that your Java application can use:
Runtime runtime = Runtime.getRuntime();
System.out.println("max memory: " + runtime.maxMemory() / 1024);

2)to get how much memory that JVM has allocated for your application
Runtime runtime = Runtime.getRuntime();
System.out.println("allocated memory: " + runtime.totalMemory() / 1024);

3)to get how much memory is being used by your application:
Runtime runtime = Runtime.getRuntime();
System.out.println("free memory: " + runtime.freeMemory() / 1024);

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can use Runtime class' totalMemory() and freeMemory() to compute your program's usability of JVM provided your program is only executed in that JVM.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eshwin Sukhdeve wrote:to get how much memory is being used by your application:

If other programs are being executed in the same JVM, it might include the usage of other programs too right?
 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you John and Eshwin ..
 
Eshwin Sukhdeve
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good question john.I am not sure..but i think it will include..because this function will give the available memory allocation...so it should check
how much free memory is there.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:If other programs are being executed in the same JVM, it might include the usage of other programs too right?

Normally, a JVM is executing only one program. If you start two Java programs on your computer, there will be two separate JVMs executing the two programs.

A good tool to monitor what Java programs are doing is VisualVM. It's included with Java 6 and newer. Type jvisualvm in a command prompt window to start it.
 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to use MemoryUsage methods to check how much memory is being used.. How can i do that???

Pooja
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!