Skip to content

[Fix #197]: Add Sample Objects query - #198

Open
kgibm wants to merge 2 commits into
eclipse-mat:masterfrom
kgibm:issue197
Open

kgibm wants to merge 2 commits into
eclipse-mat:masterfrom
kgibm:issue197

Conversation

@kgibm

@kgibm kgibm commented Sep 16, 2026

Copy link
Copy Markdown
Member
image image

Fixes #197

Signed-off-by: Kevin Grigorenko <kevin.grigorenko@us.ibm.com>

@jasonk000 jasonk000 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small changes here, and one bug. Direction is good.

@Argument(isMandatory = true)
public int number = 10000;

public enum SamplingType

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach of allowing many sample types.

finalObjectIds = Arrays.stream(objectIds).boxed().sorted((x, y) -> {
try
{
return Long.valueOf(snapshot.getRetainedHeapSize(y)).compareTo(snapshot.getRetainedHeapSize(x));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid extra boxing, we can use the form:

return Long.compare(snapshot.getRetainedHeapSize(y), snapshot.getRetainedHeapSize(x));

public SamplingType sample = SamplingType.RANDOM;

@Argument(isMandatory = true)
public int number = 10000;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is validated in UI? I think it needs to be checked as >= 0.

else if (sample == SamplingType.LAST)
{
finalObjectIds = new int[number];
System.arraycopy(objectIds, objectIds.length - number - 1, finalObjectIds, 0, number);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an off by one, eg:

[5 6 7 8], length = 4, number = 1, target = 3.

System.arraycopy(objectIds, objectIds.length - number - 1, finalObjectIds, 0, number);
System.arraycopy(objectIds, 4 - 1 - 1, finalObjectIds, 0, 1);
System.arraycopy(objectIds, 2, finalObjectIds, 0, 1);

So I think this will get us [7], but I expect we want [8]?

int[] objectIds = objects.getIds(listener);

int[] finalObjectIds = null;
if (objectIds.length < number)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be <= ? e.g length = 0, number = 0 ;; or length = 5, number = 5, could all match

}

@Test
public void sampleObjects() throws SnapshotException

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add extra test for the LAST.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And some more .. the '0', '-ve', and other boundary conditions.

}
}

public IResult execute(IProgressListener listener) throws Exception

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add a try/finally around the whole fn to unwrap and rethrow the wrapped SnapshotException? An extra method layer to wrap the query implementation can keep it tidy.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially tried that but I was surprised to find it didn't work because getRetainedHeapSize and getHeapSize throw the checked exception SnapshotException but sorted doesn't throw that exception so handling outside of sorted doesn't work. I'm still a bit curmudgeony and haven't fully mentally migrated to closures in Java but this seems like a huge oversight in the language that could have made closures much cleaner.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handling outside of sorted doesn't work

Good point!

huge oversight in the language that could have made closures much cleaner

Yes, checked exceptions are a headache. Basically we need to create a @FunctionalInterface single-method interface that has the SnapshotException in the signature.

(not critical for change but may be some inspiration!, LLM whipped it up)

@FunctionalInterface
private interface SnapshotLongFunction<T>
{
    long applyAsLong(T value) throws SnapshotException;
}
private static <T> Comparator<T> comparingSnapshot(SnapshotLongFunction<T> function,
                boolean reversed)
{
    return (left, right) -> {
        try
        {
            long l = function.applyAsLong(left);
            long r = function.applyAsLong(right);
            return reversed ? Long.compare(r, l) : Long.compare(l, r);
        }
        catch (SnapshotException e)
        {
            throw new RuntimeException(e);
        }
    };
}

Now we can call:

...
.sorted(comparingSnapshot(snapshot::getRetainedHeapSize, true))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Speaking of which, I'm not sure what the policy is for LLMs for Eclipse and/or MAT? I'm still a bit curmudgeonly with LLMs, too, so I haven't even looked into the policies yet

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found Eclipse AI guidelines here: https://www.eclipse.org/projects/handbook/#genai

Signed-off-by: Kevin Grigorenko <kevin.grigorenko@us.ibm.com>
@kgibm
kgibm requested a review from jasonk000 September 23, 2026 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Sample Objects query

2 participants