This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes How will I know how much memory can be used by my java application? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How will I know how much memory can be used by my java application?" Watch "How will I know how much memory can be used by my java application?" New topic
Author

How will I know how much memory can be used by my java application?

minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
After designing of java application, I want to know, how much memory can be used by my java application? How will I know it?


Minal Silimkar
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Wait. How much memory your program WILL use or how much memory you can 'allocate' for it?

If it's the second, you can configurate that through -Xms and -Xmx.
minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
If I execute my application, how much memory will get allocated by applications?
Lakshman Erukulla
Greenhorn

Joined: Sep 06, 2009
Posts: 2

You can create an instance for the Runtime class and use the totalmemory() method in it to know the total amount of memory in the Java virtual machine as well as you can use the freeMemory() method which returns the amount of free memory in the Java Virtual Machine. If there are any queries you can contact me here. Thank you.
Lakshman Erukulla
Greenhorn

Joined: Sep 06, 2009
Posts: 2
Runtime class is available in java.lang package
minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
Functions from class java.lang.Runtime
freeMemory() returns the amount of free memory in the Java Virtual Machine.
totalmemory() returns the total amount of memory in the Java virtual machine.
Lets take an example:

In above code class employee have two variables declared of type int and float. If I create object of class Employee one object will occupy total 8 bytes of memory. Similarly, if I designed whole application, can I directly calculate how much memory my whole application will occupy.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

minal silimkar wrote:If I create object of class Employee one object will occupy total 8 bytes of memory.

Plus overhead. Java classes are not like C structs which does have a known size that is simply the sizes of all its members added up.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
That means do I have to manually calculate total memory may be accupied by my application? There is no direct way to find out using java?
Lakshman E
Greenhorn

Joined: Jun 13, 2008
Posts: 4

Sorry for the delay minal silimkar . I think your problem has been resolved.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How will I know how much memory can be used by my java application?
 
Similar Threads
Reporting memory usage
How to check how much memory is used by a program??
How much momery I used?
Optimization Java Code - performance Vs memory
memory usage