• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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

 
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you John and Eshwin ..
 
Eshwin Sukhdeve
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

we have syntax for finding  memory allocation for whole program (we can use -:Runtime.getRuntime().totalMemory())

but my question is -:
public class temp9 {

   public void Method1 () {
       for (int i = 0; i < 30000; i++) {
           System.out.print("");
       }
   }

how much memory that METHOD1 will take , while we execute this code.. is there any way to find it?

Thanks in advance
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

You can use a profiler to find out that kind of information. Most IDEs have a decent one built in.
 
reply
    Bookmark Topic Watch Topic
  • New Topic