If you don’t set xxx = null
Then you can think that the memory will never be reclaimed before the Application is terminated.
If you set null, it will be released during GC.
Why won’t it be recycled?
References to static objects are in the method area. The method area does not participate in GC.
Java Heap is divided into 3 areas
1.Young
2.Old old generation
3.Permanent persistent generation, also called Method Area in some JVM implementations, depending on the JVM implementation.
All class information and static variables of the class are in the persistent generation
When you create a new object and apply for memory, you first apply for memory in the young generation. If it fails, a Minor GC will be triggered.
Minor GC will only release the memory in the young generation and move the surviving and eligible objects from the young generation to the old generation.
If the memory is still insufficient after Minor GC, a Full GC will be triggered at this time.
Full GC will release unused objects (?) in the old generation and persistent generation
If it is a JAVA program, then static constants may be recycled together with the class itself during FULL GC.
(?) This depends on the specific virtual machine implementation. In some virtual machine implementations, the persistent generation/method area does not participate in GC
In dalvik virtual machine
Only GC heap and method area
Method area does not participate in GC
You can just assume it will never be released.
Note: The above is an analysis based on JVM standards, and some parts are dependent on specific virtual machine implementations
I couldn't find the documentation for several relevant standards of Dalvik at the moment, so I wrote them based on my impression....
I encountered a similar problem in C#. Dictionary is static. There is a static method which is new, then Add. In the end, it was impossible to create a new one.
I have a question. A static Dictionary. There is a static method new and then Add, and then a static method Remove. Is the memory released?
What Floor 1 said is already correct, I just want to add it here:
Stack: It is a simple data structure, but it is widely used in computers. The most notable feature of the stack is: LIFO (Last In, First Out, Last In, First Out). For example, when we put clothes into a box, the clothes put in first are at the bottom. Only by taking out the clothes put in later can we get the clothes below. Only references to basic types and objects (not objects) are stored in the stack.
Heap: Heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the Java virtual machine automatic garbage collector. The JVM has only one heap area (heap) shared by all threads. Basic types and object references are not stored in the heap, only the objects themselves are stored.
Method area (method): Also called the static area, like the heap, it is shared by all threads. The method area contains all class and static variables.
Regarding the issue of memory leaks, it is strongly recommended to read these two articles Android memory leaks - full analysis and treatment methods Eight possibilities of Android memory leaks
If you don’t set xxx = null
Then you can think that the memory will never be reclaimed before the Application is terminated.
If you set null, it will be released during GC.
Why won’t it be recycled?
References to static objects are in the method area. The method area does not participate in GC.
Java Heap is divided into 3 areas
1.Young
2.Old old generation
3.Permanent persistent generation, also called Method Area in some JVM implementations, depending on the JVM implementation.
All class information and static variables of the class are in the persistent generation
When you create a new object and apply for memory, you first apply for memory in the young generation. If it fails, a Minor GC will be triggered.
Minor GC will only release the memory in the young generation and move the surviving and eligible objects from the young generation to the old generation.
If the memory is still insufficient after Minor GC, a Full GC will be triggered at this time.
Full GC will release unused objects (?) in the old generation and persistent generation
If it is a JAVA program, then static constants may be recycled together with the class itself during FULL GC.
(?) This depends on the specific virtual machine implementation. In some virtual machine implementations, the persistent generation/method area does not participate in GC
In dalvik virtual machine
Only GC heap and method area
Method area does not participate in GC
You can just assume it will never be released.
Note: The above is an analysis based on JVM standards, and some parts are dependent on specific virtual machine implementations
I couldn't find the documentation for several relevant standards of Dalvik at the moment, so I wrote them based on my impression....
I think this memory will be released when the jvm is destroyed when the program is highlighted.
I encountered a similar problem in C#.
Dictionary is static.
There is a static method which is new, then Add.
In the end, it was impossible to create a new one.
I have a question.
A static Dictionary.
There is a static method new and then Add,
and then a static method Remove.
Is the memory released?
What Floor 1 said is already correct, I just want to add it here:
Regarding the issue of memory leaks, it is strongly recommended to read these two articles
Android memory leaks - full analysis and treatment methods
Eight possibilities of Android memory leaks