方法中未使用的局部变量是否在JVM中获取内存? [英] Do unused local variables in a method acquire memory in JVM?

查看:122
本文介绍了方法中未使用的局部变量是否在JVM中获取内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO 未初始化的原始实例中看到了这篇文章变量使用内存?

它声明在Java中,在没有初始化的情况下声明类级实例变量会花费内存吗?
例如:int i;如果我没有用i = 5初始化它,请使用任何内存;?

It states "In Java, does it cost memory to declare a class level instance variable without initializing it? For example: Does int i; use any memory if I don't initialize it with i = 5;?"

我的问题是在局部变量的情况下,说我有方法foo()

My question is what in case of local variables, say i have a method foo()

public int foo(){

  int x;

//Write code which does not use/initialize x
}

本地变量 x 会占用内存吗?

Will the local variable x occupy memory?

编辑

Jon的回答是

更新:对此进行更多研究,我发现此页面向我建议,虽然编译后的字节码意味着为x分配了空间,但它确实可以通过jvm进行优化。不幸的是,我找不到所执行优化的完整描述。特别是,关于编译的JVM文档章节没有提到从堆栈中删除未使用的变量。因此,除了进一步的发现之外,我的答案是它依赖于实现,但它似乎是任何自尊的编译器都会执行的那种优化。另外请注意,这是一个局部变量而不是字段并不重要 - 事实上,局部变量是最有可能被优化的变量,因为它们是最容易分析和消除的。 (正是因为它们是本地的)

UPDATE: Doing a bit more research on this, I find this page which suggests to me that, although the compiled bytecode implies that space is allocated for x, it may indeed be optimized away by the jvm. Unfortunately, I find no complete description of the optimizations performed. Particularly, the JVM documentation chapter on compiling does not mention removing unused variables from the stack. So, barring further discoveries, my answer would be that it's implementation-dependent, but it seems like the sort of optimization that any self-respecting compiler would perform. Notice too that it doesn't matter that much that this is a local variable rather than a field - in fact, local variables are the ones most likely to be optimized away, since they are the easiest to analyze and eliminate. (precisely because they are local)

让我们看看能否找到更多支持此证据的证据。

Let us see if can find more evidences which supports this.

推荐答案

这是一个值得用javap检查的问题。

This is the kind of question that's worth examining with javap.

public class Foo
{
public int bar(){

  System.out.println("foo");
    return 8;
  }
public int foo(){

  int x;
  System.out.println("foo");
    return 8;
  }
}

请注意foo()和bar之间的区别( )是一个声明局部变量x而另一个不声明。

Notice that the difference between foo() and bar() is that one declares a local variable x and the other does not.

现在看看jvm代码(使用 javap -v Foo 在你的机器上看到这个)

Now look at the jvm code (use javap -v Foo to see this on your machine)

  public int bar();
    descriptor: ()I
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String foo
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: bipush        8
        10: ireturn
      LineNumberTable:
        line 6: 0
        line 7: 8

  public int foo();
    descriptor: ()I
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String foo
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: bipush        8
        10: ireturn
      LineNumberTable:
        line 12: 0
        line 13: 8
}

有趣的是,逐行输出是相同的,但是bar的locals是1,而对于foo,它是2.所以看起来确实分配了空间对于x,即使编译器输出不使用它。

The interesting thing is that the line-by-line output is identical, but the locals for bar is 1, and for foo it's 2. So it looks like space is indeed allocated for x, even though the compiler output doesn't ever use it.

更新:对此进行更多研究,我发现这个页面向我建议,虽然编译后的字节码意味着为x分配了空间,但它确实可以通过jvm进行优化。不幸的是,我找不到所执行优化的完整描述。特别是关于编译没有提到从堆栈中删除未使用的变量。因此,除了进一步的发现之外,我的答案是它依赖于实现,但它似乎是任何自尊的编译器都会执行的那种优化。
另外请注意,这是一个局部变量而不是字段并不重要 - 实际上,局部变量是最有可能被优化的变量,因为它们最容易分析和消除。 (正因为他们是当地的)

UPDATE: Doing a bit more research on this, I find this page which suggests to me that, although the compiled bytecode implies that space is allocated for x, it may indeed be optimized away by the jvm. Unfortunately, I find no complete description of the optimizations performed. Particularly, the JVM documentation chapter on compiling does not mention removing unused variables from the stack. So, barring further discoveries, my answer would be that it's implementation-dependent, but it seems like the sort of optimization that any self-respecting compiler would perform. Notice too that it doesn't matter that much that this is a local variable rather than a field - in fact, local variables are the ones most likely to be optimized away, since they are the easiest to analyze and eliminate. (precisely because they are local)

这篇关于方法中未使用的局部变量是否在JVM中获取内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆