Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one programatically determine how much memory is being retained due to java reference?

Tags:

java

I'm not asking how much memory a single reference takes up. I'm wondering about in the inverse sense of garbage collection.

i.e. given reference X, how much memory is that reference not allowing the garbage collector to free.

can one do this?

Obviously if one can, and if one has a set of references (A B and C) then it's likely that there will be some overlap in memory being retained (i.e. A and B might both have some connection to the same bit of memory), that's ok.

the point would be, to try and figure out why a memory leak is happening. If one has a program with long lived objects, one can programatically figure out which of these objects is growing and then rinse and repeat on said objects.

so is it possible?

like image 211
spotter Avatar asked Feb 27 '26 21:02

spotter


1 Answers

MemoryMeasurer looks interesting, but I think the question is asking about the memory you don't know about - ie; memory is unexpectedly bloated and you're looking for clues why.

VisualVM has a memory profiler that will tell you what kind of object is being retained, which is a good place to start. I don't know of any tools that reverse this and tell you what objects are retaining them.

like image 65
ddyer Avatar answered Mar 02 '26 12:03

ddyer