Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Runtime determination of Heapsize  RSS feed

 
Anirudh Chandrakant
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to monitor the performance of my java application, but i want to do it from inside the program itself. For instance, i would like to determine at runtime if my program is consuming more than a specified threshold of memory. And if it is, i would like to change the behaviour of the program.

There are methods in the Runtime class, such as totalMemory(), maxMemory() and freeMemory(). But is there any way i can determine the memory used by my current program.

It would help even if i could determine the memory consumed by the current javaw.exe process, in an OS-independent way.

I searched on this forum, but could only find references to external tracking of memory consumed by the process.

Is there any way i could achieve this?

Thanks,
Anirudh
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't do that without native code.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are methods in the Runtime class, such as totalMemory(), maxMemory() and freeMemory(). But is there any way i can determine the memory used by my current program.


I must be missing something - what is it about those Runtime methods that is unsatisfactory? Surely looking at freeMemory, etc will give you an idea of how your program is behaving.

Bill
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those methods tell you about the Java heap, which is only one of the ways in which a Java program consumes memory. If the poster really does need to know the total memory consumed by the process in which the Java program is running, they can only do so with native code (or maybe by running an external program).
 
Anirudh Chandrakant
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Thank you for your replies.
I did find out one way of doing this and it works quite fine.

This is the new Instrumentation Framework with Java 5.0. There are Monitoring and Memory Management frameworks and i can achieve what i asked for by simply calling this method:
ManagementFactory.getMemoryMXBean().getHeapMemoryUsage()

Regards,
Anirudh
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how do the numbers from the ManagementFactory approach compare to the numbers you get from the Runtime methods?

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