# java虚拟机内存模型
# java虚拟机栈
## 栈帧的基本结构:局部变量表,操作数栈,动态连接方法,返回地址。
在java虚拟机规范中,定义了两种异常与占空间相关:StackOverflowError和OutOfMemoryflowError。
可以用-Xss设置虚拟机栈的大小
获取一下本地虚拟机栈的递归深度。
public classTestStack {
private intcount=0;
public voidrecursion() {
count++;
recursion();
}
@Test
public voidtestStack() {
try{
recursion();
}catch(Throwable e) {
System.out.print("deep of stack is "+count);
e.printStackTrace();
}
}
}
java.lang.StackOverflowError
at jvm.stack.TestStack.recursion(TestStack.java:11)
deep of stack is 11380
Process finished with exit code 0
使用-Xss1M,递归深度将会增加。
注意:函数调用的次数由栈的大小决定。对于一个函数而言,参数越多,内部局部变量越多,它的栈帧就越大,其能够嵌套的次数就会越少。
局部变量表的字对GC也有影响。如果一个局部变量被保存在局部变量表中,那么GC Root就能应用到这个局部变量所指向的内存空间。
@Test
private static void test1(){
{
byte[] b = new byte[6*1024*1024];
b = null;//@1
}
System.gc();
System.out.print("first explict gc over");
}
如果没有注释行存在
## -XX:+PrintGCDetails -Xmn12M -Xmx12M
[GC (System.gc()) --[PSYoungGen: 8386K->8386K(10752K)] 8386K->8394K(11264K), 0.0016463 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 8386K->6853K(10752K)] [ParOldGen: 8K->0K(512K)] 8394K->6853K(11264K), [Metaspace: 3388K->3388K(1056768K)], 0.0063653 secs] [Times: user=0.05 sys=0.00, real=0.01 secs]
first explict gc overHeap
PSYoungGen total 10752K, used 7432K [0x00000000ff480000, 0x0000000100000000, 0x0000000100000000)
eden space 9728K, 76% used [0x00000000ff480000,0x00000000ffbc21e0,0x00000000ffe00000)
from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
to space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
ParOldGen total 512K, used 0K [0x00000000ff400000, 0x00000000ff480000, 0x00000000ff480000)
object space 512K, 0% used [0x00000000ff400000,0x00000000ff400018,0x00000000ff480000)
Metaspace used 3420K, capacity 4500K, committed 4864K, reserved 1056768K
class space used 370K, capacity 388K, committed 512K, reserved 1048576K
从gc log中可以明确的看出,b所对应的内存空间没有被回收。
如果加上注释行@1.
[GC (System.gc()) [PSYoungGen: 8581K->888K(10752K)] 8581K->896K(11264K), 0.0009387 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 888K->736K(10752K)] [ParOldGen: 8K->0K(512K)] 896K->736K(11264K), [Metaspace: 3436K->3436K(1056768K)], 0.0076800 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
first explict gc overHeap
PSYoungGen total 10752K, used 1019K [0x00000000ff480000, 0x0000000100000000, 0x0000000100000000)
eden space 9728K, 2% used [0x00000000ff480000,0x00000000ff4c6d28,0x00000000ffe00000)
from space 1024K, 71% used [0x00000000ffe00000,0x00000000ffeb8048,0x00000000fff00000)
to space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
ParOldGen total 512K, used 0K [0x00000000ff400000, 0x00000000ff480000, 0x00000000ff480000)
object space 512K, 0% used [0x00000000ff400000,0x00000000ff4000a0,0x00000000ff480000)
Metaspace used 3444K, capacity 4500K, committed 4864K, reserved 1056768K
class space used 374K, capacity 388K, committed 512K, reserved 1048576K
明显已经回收了这块内存。
如果后续代码中右边梁可以服用变量b,gc也可以回收。
#在方法体内,无论变量所在的字是否被复用,变量是否被设置为null,方法一结束,栈帧就会被销毁,变量自然就会被收回。