Reputation: 21
How can I determine the memory usage for referenced objects only in java? ie. exclude "dead" objects from memory usage during the running of a Java application.
I want to display this information and trigger an alert if it reaches a certain threahold. I also want to use this to measure how much memory is taken up when a file is imported into my application.
My application consists of many processes that all run at the same time of which one of them imports files into memory and then into a database. if I measure memory usage using the Runtime.getRuntime.freeMemory or MemoryPoolMXBean and with all the other processes running, memory usage goes up and up because of these processes and because the GC isn't running "in real time" the memory usage depicts dead objects as well as referenced ones. This is not a clear indication for me as to what is taking up memory at the time.
Is there a way to determine memory used by referenced objects only at any time?
Upvotes: 2
Views: 641
Reputation: 25028
You can look into JConsole
and see if that suits your need.
There is also VisualVM
.
They let you monitor the app but I am not sure how you can do that in your own application to trigger an alarm once your memory is low.
Also, you can use WeakReference
and SoftReference
if you want objects to be garbage-collected quicker.
I found a good article on how to query the size of a Java Object. It is slightly long so I cannot post any of it here. However, here is the link: http://www.javamex.com/tutorials/memory/instrumentation.shtml Here is a SO question on the same topic determining java memory usage
Upvotes: 1