Im doing a memory analysis of an existing java software. Is there a sql ‘group by’ equivalent in oql to see the count of objects with same values but different instances.
select count(*)
from java.lang.String s
group by s.toString()
I’d like to achieve a list of duplicated strings along with the number of duplicates. The purpose of this is to see the cases with large numbers so that they could be optimized using String.intern().
Example:
"foo" 100
"bar" 99
"lazy fox" 50
etc…
The following is based on the answer by Peter Dolberg and can be used in the VisualVM OQL Console:
It starts by using a
map()call over all String instances and for each String creating or updating an object in thecountsarray. Each object has astringand acountfield.The resulting array will contain one entry for each String instance, each having a
countvalue one larger than the previous entry for the same String.The result is then sorted on the
countfield and the result looks something like this:(in my test the String
"*null*"was the most common).The last step is to filter this using a function that returns true for the first occurrence of each String. It uses the
alreadyReturnedarray to keep track of which Strings have already been included.