Android Memory Recycling

Source: Internet
Author: User

Yesterday, my friend asked me if a Java local object is calling JNI, if the Java layer does not reference it, the object will not be referenced by the JNI layer, causing a memory leak. I thought about it for a moment and said no. It was simple to think, c there is no similar to Java-like memory recycling mechanism, the Java layer into the JNI value is passed, will not cause the reference to be generated. It's more complicated than you think, and it's not.

According to this idea, if the Java Layer object is not counted when it is referenced by JNI, the JNI layer produces a wild pointer when the object is GC. That is not the case.

First the GC is the heap space that occurs in the process scope, and if the objects in the heap are not referenced, they are collected by GC. The JNI layer also creates objects on the heap, which can still be GC.

Memory recycling in Java can be represented.

  

If the object in the heap is not referenced by a stack, that is, the red area is to be reclaimed. In general, the GC will be used as long as the object is not referenced directly or indirectly by GC root.

When a JNI call is generated, the JVM's memory distribution looks like this.

It has two stacks, one is the Java stack and the other is the native stack. When a Java calls JNI, the stack frame layout actually looks like this.

This is the stack frame, which shows that the thread first calls two Java methods, and the second Java method calls a local method, which causes the virtual machine to use a local method stack. The local method stack in the diagram is displayed as a contiguous memory space. Suppose this is a C-language stack with two C functions, all of which are represented by a gray block enclosed in dashed lines. The first C function was called by the second Java method as a local method, and the C function called the second C function. The second C function is then called by the second Java method as a local method, and the C function calls the second C function. The second C function then uses the local method interface to callback a Java method (the third Java method). Finally, the Java method calls a Java method (he becomes the current method in the diagram).

Just said that if the object is not referenced directly or indirectly by GC root, it will be GC. What exactly is GC root?

The JVM makes and recycles objects that do not have a root reference, that is, objects that cannot be traced from the root object.
The root object of the JVM garbage collection has the following scope:
1. The object referenced in the stack is referenced in the local variable table in the stack frame, and the real object is in the heap
2. Object referenced by class static property in method area perm, and object referenced by constant
3. Object referenced by Jni (native method) in the local method stack

So the JNI layer, when the C function calls the C function to pass the Jobject object, the object's reference is pressed into the local method stack as a parameter, and the reference is generated from the local method stack (one of the GC root), so it will not be GC. The same Java and JNI layers are always referenced by GC root and are not recycled by GC when they call each other.

Android Memory Recycling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.