EsotericSoftware / reflectasm

High performance Java reflection
BSD 3-Clause "New" or "Revised" License
1.53k stars 222 forks source link

MethodAccess.invoke takes a lot physical memory #76

Open yuebo opened 4 years ago

yuebo commented 4 years ago

I find when the Class contains more than 150 fields, and use MethodAccess.invoke to run the method, it would take a lot of physical memory, those memory is outside the heap and it cannot be tracked in java NMT. This will cause the docker or k8s oom kill.

Under jdk1.8.0_201, CentOS 7

import lombok.Data;

@Data
public class TestDomain {

    private String field1;
    private String field2;
    private String field3;
    private String field4;
    private String field5;
    private String field6;
    private String field7;
    private String field8;
    private String field9;
    private String field10;
    private String field11;
    private String field12;
    private String field13;
    private String field14;
    private String field15;
    private String field16;
    private String field17;
    private String field18;
    private String field19;
    private String field20;
    private String field21;
    private String field22;
    private String field23;
    private String field24;
    private String field25;
    private String field26;
    private String field27;
    private String field28;
    private String field29;
    private String field30;
    private String field31;
    private String field32;
    private String field33;
    private String field34;
    private String field35;
    private String field36;
    private String field37;
    private String field38;
    private String field39;
    private String field40;

    private Long field41;
    private Long field42;
    private Long field43;
    private Long field44;
    private Long field45;
    private Long field46;
    private Long field47;
    private Long field48;
    private Long field49;
    private Long field50;

    private Double field51;
    private Double field52;
    private Double field53;
    private Double field54;
    private Double field55;
    private Double field56;
    private Double field57;
    private Double field58;
    private Double field59;
    private Double field60;

    private String field61;
    private String field62;
    private String field63;
    private String field64;
    private String field65;
    private String field66;
    private String field67;
    private String field68;
    private String field69;
    private String field70;

    private String field71;
    private String field72;
    private String field73;
    private String field74;
    private String field75;
    private String field76;
    private String field77;
    private String field78;
    private String field79;
    private String field80;

    private String field81;
    private String field82;
    private String field83;
    private String field84;
    private String field85;
    private String field86;
    private String field87;
    private String field88;
    private String field89;
    private String field90;

    private String field91;
    private String field92;
    private String field93;
    private String field94;
    private String field95;
    private String field96;
    private String field97;
    private String field98;
    private String field99;
    private String field100;

    private String field101;
    private String field102;
    private String field103;
    private String field104;
    private String field105;
    private String field106;
    private String field107;
    private String field108;
    private String field109;
    private String field110;

    private String field111;
    private String field112;
    private String field113;
    private String field114;
    private String field115;
    private String field116;
    private String field117;
    private String field118;
    private String field119;
    private String field120;

    private String field121;
    private String field122;
    private String field123;
    private String field124;
    private String field125;
    private String field126;
    private String field127;
    private String field128;
    private String field129;
    private String field130;

    private String field131;
    private String field132;
    private String field133;
    private String field134;
    private String field135;
    private String field136;
    private String field137;
    private String field138;
    private String field139;
    private String field140;

    private String field141;
    private String field142;
    private String field143;
    private String field144;
    private String field145;
    private String field146;
    private String field147;
    private String field148;
    private String field149;
    private String field150;

    private String field151;
    private String field152;
    private String field153;
    private String field154;
    private String field155;
    private String field156;
    private String field157;
    private String field158;
    private String field159;
    private String field160;

    private String field161;
    private String field162;
    private String field163;
    private String field164;
    private String field165;
    private String field166;
    private String field167;
    private String field168;
    private String field169;
    private String field170;

    private String field171;
    private String field172;
    private String field173;
    private String field174;
    private String field175;
    private String field176;
    private String field177;
    private String field178;
    private String field179;
    private String field180;

}
@RunWith(JUnit4.class)
@Slf4j
public class TestDomainMethodAccess {
    @Test
    public void test() throws Exception{
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
        MethodAccess methodAccess = ReflectionUtil.getMethodAccess(TestDomain.class);

        for (int i = 0; i < 1000; i++) {
            TestDomain source = new TestDomain();
            for (int j = 0; j < 180; j++) {
                methodAccess.invoke(source, "getField" + (j + 1), new Object[0]);
            }
            Thread.sleep(100);
            System.out.println(""+ StringUtils.rightPad(""+(i+1),3)+"Call," +
                    "Free OS Mem "+StringUtils.leftPad(""+osmxb.getFreePhysicalMemorySize()/1024/1024,6)+"MB"+
                    ", JVM Mem "+StringUtils.leftPad(""+Runtime.getRuntime().totalMemory()/1024/1024,6)+"MB");
        }
    }
}

the result

Connected to the target VM, address: '127.0.0.1:53946', transport: 'socket'
1  Call,Free OS Mem   2114MB, JVM Mem    123MB
2  Call,Free OS Mem   2113MB, JVM Mem    123MB
3  Call,Free OS Mem   2101MB, JVM Mem    123MB
4  Call,Free OS Mem   2082MB, JVM Mem    123MB
5  Call,Free OS Mem   2081MB, JVM Mem    123MB
6  Call,Free OS Mem   2081MB, JVM Mem    123MB
7  Call,Free OS Mem   2081MB, JVM Mem    123MB
8  Call,Free OS Mem   2081MB, JVM Mem    123MB
9  Call,Free OS Mem   2081MB, JVM Mem    123MB
10 Call,Free OS Mem   2081MB, JVM Mem    123MB
11 Call,Free OS Mem   2081MB, JVM Mem    123MB
12 Call,Free OS Mem   2080MB, JVM Mem    123MB
13 Call,Free OS Mem   2080MB, JVM Mem    123MB
14 Call,Free OS Mem   2108MB, JVM Mem    123MB
15 Call,Free OS Mem   2108MB, JVM Mem    123MB
16 Call,Free OS Mem   2108MB, JVM Mem    123MB
17 Call,Free OS Mem   2108MB, JVM Mem    123MB
18 Call,Free OS Mem   2108MB, JVM Mem    123MB
19 Call,Free OS Mem   2108MB, JVM Mem    123MB
20 Call,Free OS Mem   2108MB, JVM Mem    123MB
21 Call,Free OS Mem   2121MB, JVM Mem    123MB
22 Call,Free OS Mem   2121MB, JVM Mem    123MB
23 Call,Free OS Mem   2121MB, JVM Mem    123MB
24 Call,Free OS Mem   2121MB, JVM Mem    123MB
25 Call,Free OS Mem   2121MB, JVM Mem    123MB
26 Call,Free OS Mem   2121MB, JVM Mem    123MB
27 Call,Free OS Mem   2121MB, JVM Mem    123MB
28 Call,Free OS Mem   2121MB, JVM Mem    123MB
29 Call,Free OS Mem   2121MB, JVM Mem    123MB
30 Call,Free OS Mem   2121MB, JVM Mem    123MB
31 Call,Free OS Mem   2121MB, JVM Mem    123MB
32 Call,Free OS Mem   2111MB, JVM Mem    123MB
33 Call,Free OS Mem   2112MB, JVM Mem    123MB
34 Call,Free OS Mem   2112MB, JVM Mem    123MB
35 Call,Free OS Mem   2112MB, JVM Mem    123MB
36 Call,Free OS Mem   2112MB, JVM Mem    123MB
37 Call,Free OS Mem   2112MB, JVM Mem    123MB
38 Call,Free OS Mem   2112MB, JVM Mem    123MB
39 Call,Free OS Mem   2112MB, JVM Mem    123MB
40 Call,Free OS Mem   2112MB, JVM Mem    123MB
41 Call,Free OS Mem   1845MB, JVM Mem    123MB
42 Call,Free OS Mem   1821MB, JVM Mem    123MB
43 Call,Free OS Mem   1788MB, JVM Mem    123MB
44 Call,Free OS Mem   2010MB, JVM Mem    123MB
45 Call,Free OS Mem   2010MB, JVM Mem    123MB
46 Call,Free OS Mem   2010MB, JVM Mem    123MB
47 Call,Free OS Mem   2010MB, JVM Mem    123MB
48 Call,Free OS Mem   2010MB, JVM Mem    123MB
49 Call,Free OS Mem   2010MB, JVM Mem    123MB
50 Call,Free OS Mem   2010MB, JVM Mem    123MB
51 Call,Free OS Mem   1509MB, JVM Mem    123MB
52 Call,Free OS Mem   1420MB, JVM Mem    123MB
53 Call,Free OS Mem   1318MB, JVM Mem    123MB
54 Call,Free OS Mem   1221MB, JVM Mem    123MB
55 Call,Free OS Mem   1124MB, JVM Mem    123MB
56 Call,Free OS Mem   1026MB, JVM Mem    123MB
57 Call,Free OS Mem   1026MB, JVM Mem    123MB
58 Call,Free OS Mem   1026MB, JVM Mem    123MB
59 Call,Free OS Mem   1026MB, JVM Mem    123MB
60 Call,Free OS Mem   1026MB, JVM Mem    123MB
61 Call,Free OS Mem    550MB, JVM Mem    123MB
62 Call,Free OS Mem    462MB, JVM Mem    123MB
63 Call,Free OS Mem    367MB, JVM Mem    123MB
64 Call,Free OS Mem    282MB, JVM Mem    123MB
65 Call,Free OS Mem    185MB, JVM Mem    123MB
66 Call,Free OS Mem     92MB, JVM Mem    123MB
67 Call,Free OS Mem     15MB, JVM Mem    123MB
68 Call,Free OS Mem     15MB, JVM Mem    123MB
69 Call,Free OS Mem     15MB, JVM Mem    123MB
70 Call,Free OS Mem     15MB, JVM Mem    123MB
71 Call,Free OS Mem    624MB, JVM Mem    123MB
72 Call,Free OS Mem   1586MB, JVM Mem    123MB
73 Call,Free OS Mem   1586MB, JVM Mem    123MB
74 Call,Free OS Mem   1586MB, JVM Mem    123MB
75 Call,Free OS Mem   1586MB, JVM Mem    123MB
76 Call,Free OS Mem   1586MB, JVM Mem    123MB
77 Call,Free OS Mem   1586MB, JVM Mem    123MB
78 Call,Free OS Mem   1586MB, JVM Mem    123MB
79 Call,Free OS Mem   1586MB, JVM Mem    123MB
80 Call,Free OS Mem   1586MB, JVM Mem    123MB
81 Call,Free OS Mem   2151MB, JVM Mem    123MB
82 Call,Free OS Mem   2165MB, JVM Mem    123MB
83 Call,Free OS Mem   2164MB, JVM Mem    123MB
84 Call,Free OS Mem   2163MB, JVM Mem    123MB
85 Call,Free OS Mem   2162MB, JVM Mem    123MB
86 Call,Free OS Mem   2162MB, JVM Mem    123MB
87 Call,Free OS Mem   2162MB, JVM Mem    123MB
88 Call,Free OS Mem   2162MB, JVM Mem    123MB
89 Call,Free OS Mem   2162MB, JVM Mem    123MB
90 Call,Free OS Mem   2162MB, JVM Mem    123MB
91 Call,Free OS Mem   2158MB, JVM Mem    123MB
92 Call,Free OS Mem   2158MB, JVM Mem    123MB
93 Call,Free OS Mem   2158MB, JVM Mem    123MB
94 Call,Free OS Mem   2154MB, JVM Mem    123MB
95 Call,Free OS Mem   2161MB, JVM Mem    123MB
96 Call,Free OS Mem   2161MB, JVM Mem    123MB
97 Call,Free OS Mem   2161MB, JVM Mem    123MB
98 Call,Free OS Mem   2161MB, JVM Mem    123MB
99 Call,Free OS Mem   2161MB, JVM Mem    123MB
100Call,Free OS Mem   2161MB, JVM Mem    123MB
Disconnected from the target VM, address: '127.0.0.1:53946', transport: 'socket'

Process finished with exit code 0