本文介绍了Java 中的转义分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,JVM 使用转义分析来获得某些性能优化 类似于锁粗化和锁省略.我很感兴趣 JVM 是否有可能决定使用转义分析在堆栈上分配任何特定对象.

一些资源让我觉得我是对的.是否有 JVM 真正做到这一点?

解决方案

我不认为它对堆栈分配进行逃逸分析.例子:

public class EscapeAnalysis {私有静态类 Foo {私有整数 x;私有静态整数计数器;公共 Foo() {x = (++计数器);}}公共静态无效主(字符串 [] args){System.out.println("开始");for (int i = 0; i 

使用 -server -verbose:gc -XX+DoEscapeAnalysis:

开始[GC 3072K->285K(32640K),0.0065187 秒][GC 3357K->285K(35712K),0.0053043 秒][GC 6429K->301K(35712K),0.0030797 秒][GC 6445K->285K(41856K),0.0033648 秒][GC 12573K->285K(41856K),0.0050432 秒][GC 12573K->301K(53952K),0.0043682 秒][GC 24877K->277K(53952K),0.0031890 秒][GC 24853K->277K(78528K),0.0005293 秒][GC 49365K->277K(78592K),0.0006699 秒]10000000

据称 JDK 7 支持堆栈分配.

As far as I know the JVM uses escape analysis for some performance optimisations like lock coarsening and lock elision.I'm interested if there is a possibility for the JVM to decide that any particular object can be allocated on stack using escape analysis.

Some resources make me think that I am right. Is there JVMs that actually do it?

解决方案

I don't think it does escape analysis for stack allocation. example:

public class EscapeAnalysis {

    private static class Foo {
        private int x;
        private static int counter;

        public Foo() {
            x = (++counter);
        }
    }
    public static void main(String[] args) {
        System.out.println("start");
        for (int i = 0; i < 10000000; ++i) {
            Foo foo = new Foo();
        }

        System.out.println(Foo.counter);
    }
}

with -server -verbose:gc -XX+DoEscapeAnalysis:

start
[GC 3072K->285K(32640K), 0.0065187 secs]
[GC 3357K->285K(35712K), 0.0053043 secs]
[GC 6429K->301K(35712K), 0.0030797 secs]
[GC 6445K->285K(41856K), 0.0033648 secs]
[GC 12573K->285K(41856K), 0.0050432 secs]
[GC 12573K->301K(53952K), 0.0043682 secs]
[GC 24877K->277K(53952K), 0.0031890 secs]
[GC 24853K->277K(78528K), 0.0005293 secs]
[GC 49365K->277K(78592K), 0.0006699 secs]
10000000

Allegedly JDK 7 supports stack allocation.

这篇关于Java 中的转义分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:49