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?

user132638
  • 151
  • 1
  • 2
  • 9
  • 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 '12 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 '12 at 12:05

1 Answers1

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.

  • 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 '12 at 12:33
  • The correct tool is jstat. –  Aug 18 '12 at 12:35
  • I google and found this link http://xmlandmore.blogspot.com/2012/07/jstat-java-virtual-machine-statistics.html. 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 '12 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 –  Aug 18 '12 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 '12 at 13:04
  • It's not in your PATH so, you have to add it or type /usr/java/jdk1.7.0_03/bin/jstat –  Aug 18 '12 at 13:05
  • Ok I am in the folder /usr/java/jdk.1.7_03/bin and run the command still gave me error. But when I run like this /usr/java/jdk1.7.0_03/bin/jstat -gcutil 29988 S0 S1 E O P YGC YGCT FGC FGCT GCT 99.73 0.00 25.54 83.22 43.05 376 0.717 34 0.851 1.568 it gives me the results. What PATH settings must I do ? – user132638 Aug 18 '12 at 13:09
  • Questions about PATH settings and how to run a program are not related to java nor memory management. Try getting documentation about basic unix/linux usage. If you encounter any problem, feel free to ask another question (maybe on superuser.com or linux/unix). –  Aug 18 '12 at 13:34
  • back to the original problem what are this values of jstat tell us I was reading them but how to interpret the memory usage or leakage scenario? It is about new generation and old generation right? – user132638 Aug 18 '12 at 13:41
  • There is a very good website (hard to find) about using jstat to troubleshot memory usage : http://prefetch.net/blog/index.php/2008/01/16/monitoring-garbage-collection-with-jstat/ –  Aug 18 '12 at 13:58
  • yes a bit less websites talk about it well I have google this site before I dont quite get concepts of S0C && S1C & S0U && S1U and the rest of the columns when should it indicate if there is a problem? This a total memory usage is it? – user132638 Aug 18 '12 at 14:12
  • When there is a memory problem, you see a lot of gc and full gc. And S0C S1C S0U and S1U usage wont decrease a lot if objects are still refenreced. –  Aug 18 '12 at 14:20
  • I see sometimes my S0 S1 goes up to 100 then goes to 0 so is that considered healthy? I wish to learn more about this is any more details description. Thank you Eric for your help. – user132638 Aug 18 '12 at 14:23
  • Yes it's considered healthy.You should see loads of full gc if there is memory leak, just before jvm crashes with "out of memory" exeption. –  Aug 18 '12 at 14:29
  • One you get there, try increasing jvm size => If jvm crashes with more memory, it's likely that there is memory leak somewhere in code. –  Aug 18 '12 at 14:30
  • So how about the other column what should be look out pattern for it? So can I conclude is this 2 columns dont change their value and keep increasing something is wrong? In my case I notice now the top memory is keep increasing I guess the JVM is preparing memory for itself? – user132638 Aug 18 '12 at 14:34
  • Got to go for a few hours, but there are tons of documentation about jvm meory usage, and stackoverflow might have good hints too. Good luck –  Aug 18 '12 at 14:40