使用Java 7进行转义分析堆栈分配的资格

使用Java 7进行转义分析堆栈分配的资格,第1张

使用Java 7进行转义分析/堆栈分配的资格

EA是C2编译器根据它生成的IR进行分析的,因此您需要它才能在享受收益之前编译该方法。每个测试仅被调用一次,因此没有机会进行编译。热点内部Wiki中有关EA和C2
IR的详细信息(https://wiki.openjdk.java.net/display/HotSpot/Overview+of+Ideal、+C2%27s+high+level+intermediate+representation和https://wiki.openjdk.java.net/display/HotSpot/EscapeAnalysis)

这是一个试图显示影响的版本

import com.sun.management.ThreadMXBean;import java.lang.management.ManagementFactory;import java.util.ArrayList;import java.util.Iterator;public class EscapeAnalysisTest {    private static final long TIME_TO_TEST = 10L * 1000L; // 10s    static class Timestamp {        private long millis;        public Timestamp(long millis) { this.millis = millis;        }        public long getTime() { return millis;        }        public void setTime(long time) { millis = time;        }    }    public static void main(String[] args) {        System.out.println("****");        doIt();        System.out.println("****");        doIt();        System.out.println("****");        doIt();        System.out.println("****");        doIt();        System.out.println("****");    }    private static void doIt() {        final ThreadMXBean mxbean = (ThreadMXBean) ManagementFactory.getThreadMXBean();        final long tid = Thread.currentThread().getId();        long r = 0;        final long allocPre = mxbean.getThreadAllocatedBytes(tid);        r += test1();        long alloc1 = mxbean.getThreadAllocatedBytes(tid);        System.out.println("test1 - " + (alloc1 - allocPre));        r += test2();        final long alloc2 = mxbean.getThreadAllocatedBytes(tid);        System.out.println("test2 - " + (alloc2 - alloc1));        r += test3();        final long alloc3 = mxbean.getThreadAllocatedBytes(tid);        System.out.println("test3 - " + (alloc3 - alloc2));        r += test4();        final long alloc4 = mxbean.getThreadAllocatedBytes(tid);        System.out.println("test4 - " + (alloc4 - alloc3));        r += test5();        final long alloc5 = mxbean.getThreadAllocatedBytes(tid);        System.out.println("test5 - " + (alloc5 - alloc4));        r += test6();        final long alloc6 = mxbean.getThreadAllocatedBytes(tid);        System.out.println("test6 - " + (alloc6 - alloc5));        System.out.println(r);    }    public static long test1() {        long r = 0;        long start = System.currentTimeMillis();        while (System.currentTimeMillis() - start < TIME_TO_TEST) { r += new Timestamp(System.currentTimeMillis()).getTime();        }        return r;    }    public static long test2() {        ArrayList<Integer> l = new ArrayList<Integer>(1000);        for (int i = 0; i < 1000; ++i) { l.add(i);        }        long r = 0;        long start = System.currentTimeMillis();        while (System.currentTimeMillis() - start < TIME_TO_TEST) { for (Iterator<Integer> it = l.iterator(); it.hasNext(); ) {     r += it.next().longValue(); }        }        return r;    }    public static long test3() {        long r = 0;        long start = System.currentTimeMillis();        while (System.currentTimeMillis() - start < TIME_TO_TEST) { Timestamp ts = new Timestamp(System.currentTimeMillis()); ts.setTime(42); r += ts.getTime();        }        return r;    }    public static long test4() {        ArrayList<Integer> l = new ArrayList<Integer>(1000);        for (int i = 0; i < 1000; ++i) { l.add(i);        }        long r = 0;        long start = System.currentTimeMillis();        while (System.currentTimeMillis() - start < TIME_TO_TEST) { Iterator<Integer> it = l.iterator(); r += it.next().longValue(); r += it.next().longValue(); r += it.next().longValue(); r += it.next().longValue();        }        return r;    }    public static long test5() {        ArrayList<Integer> l = new ArrayList<Integer>(1000);        for (int i = 0; i < 1000; ++i) { l.add(i);        }        long r = 0;        long start = System.currentTimeMillis();        while (System.currentTimeMillis() - start < TIME_TO_TEST) { Iterator<Integer> it = l.iterator(); for (int i = 0; i < l.size(); ++i) {     r += it.next().longValue(); }        }        return r;    }    public static long test6() {        long r = 0;        long start = System.currentTimeMillis();        while (System.currentTimeMillis() - start < TIME_TO_TEST) { for (Timestamp ts = new Timestamp(System.currentTi());      ts.getTime() > 0;      ts.setTime(ts.getTime() + System.currentTimeMillis())) {     r += ts.getTime(); }        }        return r;    }}

当运行时会生成以下输出

-server -XX:CompileThreshold=1

****test1 - 109048test2 - 89243416test3 - 16664test4 - 42840test5 - 71982168test6 - 1400-5351026995119026839****test1 - 16432test2 - 85921464test3 - 16664test4 - 42840test5 - 66777600test6 - 13687844020592566674506****test1 - 48test2 - 18256test3 - 272test4 - 18264test5 - 18264test6 - 272-2137858376905291730****test1 - 48test2 - 18256test3 - 272test4 - 18264test5 - 18264test6 - 2723273987624143297143****

这里的一个危险是该方法的编译已从根本上改变了它,我没有尝试防止这种情况的发生,因此可能需要使用

LogCompilation
PrintCompilation
进行检查。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5428509.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存