Skip to content

Conversation

Aresxue
Copy link

@Aresxue Aresxue commented Sep 25, 2025

purpose

Added reflection analysis function to analyze all the methods corresponding to sun.reflect.GeneratedMethodAccessor to assist in troubleshooting insufficient metaspace problem.

usage

1.reflect-analysis
2.reflect-analysis reflect-analysis-result.csv

When it is a relative path, the file will be generated in a temporary directory.

Example

count,refName,names
3,org.springframework.boot.admin.SpringApplicationAdminMXBean#getProperty,GeneratedMethodAccessor23,GeneratedMethodAccessor22,GeneratedMethodAccessor24
2,javax.management.remote.rmi.RMIConnection#invoke,GeneratedMethodAccessor20,GeneratedMethodAccessor21
1,cn.ares.boot.demo.user.request.UserRequest#getId,GeneratedMethodAccessor1
1,com.sun.management.OperatingSystemMXBean#getProcessCpuLoad,GeneratedMethodAccessor40
1,javax.management.ObjectName#readObject,GeneratedMethodAccessor4
1,javax.management.remote.rmi.RMIConnection#close,GeneratedMethodAccessor25
1,javax.management.remote.rmi.RMIConnection#getAttribute,GeneratedMethodAccessor8
1,javax.management.remote.rmi.RMIConnection#getConnectionId,GeneratedMethodAccessor15
1,javax.management.remote.rmi.RMIServer#newClient,GeneratedMethodAccessor14
1,org.springframework.boot.actuate.env.EnvironmentEndpoint$PropertyValueDescriptor#getOrigin,GeneratedMethodAccessor28
1,org.springframework.boot.actuate.env.EnvironmentEndpoint$PropertyValueDescriptor#getOriginParents,GeneratedMethodAccessor29
1,org.springframework.boot.actuate.env.EnvironmentEndpoint$PropertyValueDescriptor#getValue,GeneratedMethodAccessor27
1,org.springframework.boot.actuate.web.mappings.servlet.DispatcherServletMappingDescription#getDetails,GeneratedMethodAccessor33
1,org.springframework.boot.actuate.web.mappings.servlet.DispatcherServletMappingDescription#getHandler,GeneratedMethodAccessor31
1,org.springframework.boot.actuate.web.mappings.servlet.DispatcherServletMappingDescription#getPredicate,GeneratedMethodAccessor32

…orresponding to sun.reflect.GeneratedMethodAccessor to assist in troubleshooting insufficient metaspace problem.
@Aresxue Aresxue changed the title Feat: reflect analysis feat: reflect analysis Sep 25, 2025
@Aresxue
Copy link
Author

Aresxue commented Sep 25, 2025

@hengyunabc PTAL

@hengyunabc hengyunabc requested a review from Copilot September 25, 2025 12:42
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a new reflection analysis feature to help troubleshoot insufficient metaspace problems by analyzing sun.reflect.GeneratedMethodAccessor classes and their corresponding methods.

  • Added a new reflect-analysis command that can output results to console or CSV file
  • Implemented reflection analysis using ASM bytecode analysis to extract method invocation information
  • Created supporting infrastructure including models, views, and utilities for the new feature

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
MapUtil.java Added utility class for optimized HashMap capacity calculation
ResultViewResolver.java Registered the new ReflectAnalysisView for rendering results
ReflectAnalysisView.java View class for formatting reflection analysis output
ReflectAnalysisModel.java Model class containing reflection analysis data
ReflectAnalysisCommand.java Main command implementation with ASM bytecode analysis
BuiltinCommandPack.java Added ReflectAnalysisCommand to available commands

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@hengyunabc
Copy link
Collaborator

麻烦解析下原理,怎么工作的,解决哪些问题。

@Aresxue
Copy link
Author

Aresxue commented Sep 25, 2025

麻烦解析下原理,怎么工作的,解决哪些问题。

解决的问题

在某些情况下(比如动态加载类后未正确卸载)会不断地产生sun.reflect.GeneratedMethodAccessor对象导致元空间(通过JVM参数限定了大小)不足,从而不断地触发FullGc导致服务不稳定甚至不可用,此时需要知道大量的sun.reflect.GeneratedMethodAccessor所对应的方法才能知道问题出在哪里。

原理

1.查找当前JVM中所有的sun.reflect.GeneratedMethodAccessor类;
2.遍历这些类使用asm分析出它们所对应的方法,一个sun.reflect.GeneratedMethodAccessor的示例:

package sun.reflect;

import cn.ares.boot.demo.user.request.UserRequest;
import java.lang.reflect.InvocationTargetException;
import sun.reflect.MethodAccessorImpl;

public class GeneratedMethodAccessor1
extends MethodAccessorImpl {
    /*
     * Loose catch block
     */
    public Object invoke(Object object, Object[] objectArray) throws InvocationTargetException {
        UserRequest userRequest;
        block5: {
            if (object == null) {
                throw new NullPointerException();
            }
            userRequest = (UserRequest)object;
            if (objectArray == null || objectArray.length == 0) break block5;
            throw new IllegalArgumentException();
        }
        try {
            return userRequest.getId();
        }
        catch (Throwable throwable) {
            throw new InvocationTargetException(throwable);
        }
        catch (ClassCastException | NullPointerException runtimeException) {
            throw new IllegalArgumentException(super.toString());
        }
    }
}

3.根据类+方法名作统计计数;
4.将结果输出至终端或文件;

@Aresxue
Copy link
Author

Aresxue commented Sep 28, 2025

@hengyunabc PTAL

@hengyunabc
Copy link
Collaborator

感觉是一个非常小众的场景,直接 heapdump 应该也可以分析出来。 或者直接用 classloader 命令来查看有哪些 classloader,再看它们加载的类列表。

@Aresxue
Copy link
Author

Aresxue commented Oct 14, 2025

感觉是一个非常小众的场景,直接 heapdump 应该也可以分析出来。 或者直接用 classloader 命令来查看有哪些 classloader,再看它们加载的类列表。

确实是一个偏小众的需求,但通过classloader查看只能查看到sun.reflect.GeneratedMethodAccessor这一层,sun.reflect.GeneratedMethodAccessor背后所对应的方法是无法统计的,heapdump是可以分析出来的但这个过程也并不是很简单,Arthas是否有计划实现一个插件机制将一些非核心的指令迁移过去并更方便用户自己扩展与贡献。

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.

2 participants