Java中存储的内存地址在哪里?我想要了解的是如何存储以下内容。我已经知道堆栈和堆栈之间的区别,但是试图挖掘比这更深的层。
int i = 5;
i
是否单独存储并5
分开存储,然后建立映射?
类似地,我们说对于32位版本,最多可以有4 GB RAM(实际上要少得多),所有这些内存块的内存地址都存储在哪里?
答案 0 :(得分:5)
我想要了解的是如何存储以下内容。
取决于int i = 5;
的位置:
如果它在一个方法中,因此i
是一个局部变量,那么5
将作为本地存储在堆栈中(i
不会“存储”在任何地方,字节码生成器只记得堆栈i
上的位置)。字节码具有与局部变量交互的特定指令,包括一些非常有效的专用无争论版本,例如iload_0
从“局部变量0”加载int
。 (那些经过iload_3
,然后有一个版本采用arg,iload
后跟一个索引。)
考虑这个课程:
public class Example {
public static final void main(String[] args) {
int i = 5;
System.out.println(i);
}
}
这是字节码(你可以通过编译然后做javap -c Example
得到这个),右边有注释:
public class Example {
// ...omitted constructor stuff...
public static final void main(java.lang.String[]);
Code:
// The `int i = 5;` line:
0: iconst_5 // Load the constant 5 onto the stack
1: istore_1 // Store it in local variable 1
// The `System.out.println(i);` line:
2: getstatic #2 // Get the static field java/lang/System.out:Ljava/io/PrintStream onto the stack
5: iload_1 // Load int variable 1 on the stack
6: invokevirtual #3 // Call java/io/PrintStream.println, which gets
// the stream and what to write from the stack
9: return
}
请注意堆栈的两种不同用途:“locals”位于堆栈的一部分,该方法为本地分配了自己(因为它事先知道有多少),然后是动态部分跟随它用于将信息传递给方法等。
如果它在类定义中,因此i
是类的实例字段,i
是Java保留的结构的一部分(在堆栈或堆上,有时在它们之间移动)对于实例。
Bytecode并没有真正揭示很多的光,但考虑这个类:
public class Example {
int i = 5;
public static final void main(String[] args) {
Example ex = new Example();
System.out.println(ex.i);
}
}
这是字节码:
public class Example {
// Here's the instance field
int i;
// ...omitted the constructor stuff...
public static final void main(java.lang.String[]);
Code:
// Create our Example instance and save it in a local variable
0: new #3
3: dup
4: invokespecial #4
7: astore_1
// The `System.out.println(ex.i)` line:
8: getstatic #5 // Get the java/lang/System.out:Ljava/io/PrintStream field
11: aload_1 // Get `ex` onto the stack
12: getfield #2 // Get the value of field `i` onto the stack from the instance we just put on the stack (pops the instance off)
15: invokevirtual #6 // Call java/io/PrintStream.println, which
// again gets the stream to write to and
// what to write from the stack
18: return
}
答案 1 :(得分:2)
Java中存储的内存地址在哪里?
在Java中,引用不仅仅是内存地址,而且由于压缩的oops,它们很少用于Java相关数据。
我想要了解的是如何存储以下内容。
值得记住的是,JVM是一个虚拟机,您可以说它是如何在字节代码中以名义存储的,但代码本身经历了多个优化阶段,并且每次都可以更改。
此外,JVM非常擅长优化代码,而这些代码在许多琐碎的情况下都没有做任何事情。无处可去,代码已被内联到任何地方。
我已经知道堆栈和堆之间的区别,但是试图挖掘比这更深的层。
int i = 5;
我是单独存储还是单独存储,然后建立映射?
在运行时,很有可能内联的值已经存在。但是,它可能位于堆栈,堆或两者上,具体取决于上下文。
类似地,我们说对于32位版本,最多可以有4 GB RAM(实际上要少得多),所有这些内存块的内存地址都存储在哪里?
在32位版本中,您只能使用可用作堆的最大连续区域。这仅限于Windows上大约1.4 GB和Unix上大约3 GB。
在64-it版本中,它通常使用32位引用,这些引用通过转换允许您在不使用Compressed Oops
的情况下最多可以处理32 GB(Java 7)和64 GB(Java 8)在大多数操作系统上寻址48位地址空间(受硬件和操作系统限制)