As shown in the following code, a is a local variable in the stack frame. When a = null, because a acts as a GC Root, a is disconnected from the instance new Test(), so the object will be recycled.
publicclass Test { public static void main(String[] args) { Test a = new Test(); a = null; } }
2. The object referenced by the class static attribute in the method area
As shown in the following code, when the local variable a = null in the stack frame, because the object originally pointed to by a is disconnected from the GC Root (variable a), the object originally pointed to by a will be recycled. Because we assign a variable reference to s, s is a class static attribute reference at this time, acting as the GC Root, and the object it points to still lives!
public class Test { public static Test s; public static void main(String[] args) { Test a = new Test(); a.s = new Test(); a = null; } }
3. Objects referenced by constants in the method area
As shown in the following code, the object pointed to by the constant s is not recycled because the object pointed to by a is recycled
public class Test { public static final Test s = new Test(); public static void main(String[] args) { Test a = new Test(); a = null; } }
4. Object referenced by JNI in local method stack
This is a simple explanation for children's shoes who don't know what local methods are: the SO-called local method is an interface for Java to call non java code. This method is not implemented in Java and may be implemented by other languages such as C or Python. Java calls local methods through JNI, Local methods are stored in the form of library files (it is in the form of DLL file on WINDOWS platform and SO file on UNIX machine). By calling the internal method of local library file, Java can realize the close connection with local machine and call various interface methods at system level. I still don't understand? See the reference at the end of the article for a detailed introduction to the definition and use of local methods.
When calling a Java method, the virtual opportunity creates a stack frame and pushes it into the Java stack. When it calls a local method, the virtual opportunity keeps the Java stack unchanged and does not push a new frame into the Java stack frame. The virtual machine simply dynamically connects and directly calls the specified local method.
JNIEXPORT void JNICALL Java_com_pecuyu_jnirefdemo_MainActivity_newStringNative(JNIEnv *env, jobject instanceļ¼jstring jmsg) { ... // Cache class of String jclass jc = (*env)->FindClass(env, STRING_PATH); # Is that enough? No, not enough! Getting familiar with Ali's interview questions in previous years in advance must be of great help to the interview, but as a technical career, having solid technology in your hand is the most useful weapon for you to face the interviewer, which is the confidence radiated from the inside. When preparing for Ali, I spent the most time learning technology, accounting for 70% of all my learning plans. These are some learning notes and learning videos that I thought were very good during my study[You can click here for free](https://gitee.com/vip204888/java-p7)! Why should I write this article? In fact, I think learning can't stop. Sharing and discussing with you on the Internet can not only meet more people, but also expand my horizons and learn more technologies. I will continue to learn in the future csdn,Sharing technology on blogs, nuggets and other websites is also a way to learn. That's all for today. Thank you for your attention. I'll share more dry goods to you in the future! ![Ali fell off his horse and went ashore to the ant gold suit after making up this "Ali interview classic"](https://img-blog.csdnimg.cn/img_convert/f2fb91cfd0101c9e9a99e5065af8d275.png) ![Ali fell off his horse and went ashore to the ant gold suit after making up this "Ali interview classic"](https://img-blog.csdnimg.cn/img_convert/0728330c5fd4b9b8f13229d998cd1566.png) ![image.png](https://img-blog.csdnimg.cn/img_convert/2eef18ada22c85b877d8d300b13e1fc3.png) # mg-EqhVld4f-1628228835357)] [External chain picture transfer...(img-Vea7DvlX-1628228835359)] [External chain picture transfer...(img-1DMyZ4Os-1628228835361)] #