1

We have a java application where towards the last part of the codes we wrote these lines

Runtime runtime = Runtime.getRuntime();
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("\n\nUsed memory is bytes: " + memory);

What we notice is that over time the top shows increase in the memory(%) column for the application but the java memory(from runtime variables) values shows fluctuation up and down? So which should we follow to decide on exact memory usage? My intention is to identify if there is any memory leakage in my application?

2
  • Can you add more information? You've written enough get get a blurry concept picture, but not much more. It's important to note Java is a Virtual Machine, thus it will request memory from the OS and manage it for the application. Beyond that I don't know how the JVM handles memory management so I don't know if it's doing any tricks like over subscription or de-duplication.
    – Red Tux
    Aug 18, 2012 at 11:47
  • @RedTux what I am doing is cross checking the memory usage shown by top from linux and also the runtime.totalmemory usage. What I notice is that the top(Memory percentage) keep increasing and worries me. I would like to know if there is any problem in my codes. Hope I am clearer now?
    – user132638
    Aug 18, 2012 at 12:05

1 Answer 1

0

When JVM starts, It takes some memory from the operating system. This is the amount of memory you see in tools like ps and top.

Then your applications start using this memory : That's the memory you see with your code and tools like jstat.

So if you want to debug memory leak inside your app, you don't want to use top or ps.

17
  • so that means I am looking into wrong values. So is it normal for JVM to keep increasing? So what is right tool to know the health of my application in terms of memory usage? So are you suggesting to look for this value long memory = runtime.totalMemory() - runtime.freeMemory();?
    – user132638
    Aug 18, 2012 at 12:33
  • The correct tool is jstat.
    – user130370
    Aug 18, 2012 at 12:35
  • I google and found this link xmlandmore.blogspot.com/2012/07/…. So I was just trying this comamnd jstat -gcutil pgrep java but I get command not found. I am in the right directory of bin.
    – user132638
    Aug 18, 2012 at 12:50
  • Path to the jstat program depends on your os and the way you installed jdk. For example, if you install openjdk-6-jdk on debian based distro from package, try /usr/lib/jvm/java-6-openjdk/bin/jstat
    – user130370
    Aug 18, 2012 at 12:59
  • Mine is /usr/java/jdk1.7.0_03/bin and I can see the jstat file there too. So what else could be wrong.
    – user132638
    Aug 18, 2012 at 13:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .