java堆栈和方法返回的对象 [英] java stack and object returned by a method
问题描述
for语句
rs.getString(name)// rs是java.sql.ResultSet接口
字节码是:
41:aload 4
43:ldc#10; //字符串名称
45:invokeinterface#11,2; //接口方法java / sql / ResultSet.getString:(Ljava / lang / String;)Ljava / lang / String;
50:pop
在第45行返回的字符串对象由 rs .getString(name)
被压入堆栈,并且在第50行,返回对象(一个字符串对象)被强制关闭。
$ b $ 1)堆栈是否仅包含引用,该堆栈上有实际的对象,或者堆栈包含实际的String对象在弹出返回的字符串对象之后,它会被垃圾回收还是因为此方法的堆栈而释放内存?
(
<2>)
在第45行,通过rs.getString(name)将返回的字符串对象压入栈中
There 没有返回的字符串对象。有一个返回的字符串引用。
从概念上来说,至少,对象永远不会在堆栈中返回,永远不会返回通过。它只是 引用。
在弹出返回的字符串对象后,它会被垃圾收集还是它的内存释放为堆栈,此方法消失了?
当栈弹出时,引用这意味着如果没有其他强引用,字符串对象本身就有资格进行垃圾回收。
for statement
rs.getString("name") //rs is java.sql.ResultSet interface
bytecode is:
41: aload 4
43: ldc #10; //String name
45: invokeinterface #11, 2; //InterfaceMethod java/sql/ResultSet.getString:(Ljava/lang/String;)Ljava/lang/String;
50: pop
At line 45 returned string object by rs.getString("name")
is pushed onto stack and at line 50, return object (a string object) is poped up.
1)Does stack contain only reference to this returned string object with actual object on heap OR stack contains the actual String object??
2) and after poping up the returned string object, will it be garbage collected OR it's memory deallocated as stack for this method vanishes???
At line 45 returned string object by rs.getString("name") is pushed onto stack
There is no returned string object. There's a returned string reference.
Conceptually at least, objects are never on the stack, never returned, never passed. It's only ever a reference.
and after poping up the returned string object, will it be garbage collected OR it's memory deallocated as stack for this method vanishes?
The reference will be cleared when the stack pops, which means that the string object itself would be eligible for garbage collection if there are no other strong references to it.
这篇关于java堆栈和方法返回的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!