Use GC

zhaozj2021-02-16  43

People who have used Java know the GC () method (system.gc ();) even if you have not used it, I have heard of it. Maybe you will never use it in J2SE or J2EE, but it is another thing in J2ME. Many people don't agree to call it directly, I didn't think about using J2ME, but as memory consumption, I started paying attention to it. Several twists and turns, I found that the GC method is very useful, but don't use it casually, otherwise the consequences are at your own risk. Below I will talk about some of the use of GC (for reference only)

1. Call GC () before you will load a large amount of resources, because there is a lot of resource load, there may be temporary memory of memory, at which time the system will automatically call the garbage collector, if the resources are too large (of course, it is less than the system Available memory, including memory that has not been recovered), because the garbage collector may have not completed the recycling of garbage in time, and resources will be loaded, it will cause the available memory that cannot meet the resources of resources, so J2ME programs will appear stop Response or simply reporting memory. Therefore, Early call GC () can be avoided to avoid this happening.

2. Do not use GC (); reasons: The garbage collector itself is a resource-consuming stuff, but continuous use may consume memory.

3. The above two points are only an intuitive feeling, and in the actual operation, it is necessary to combine the help of the Runtime object. There are several useful methods in the Runtime object: long freeMemory () Returns the current number of memory logs long totalmemory () Returns the total amount of memory if you want to join GC () in a place, but you don't know what to add There is no effect in this place: system.unt.println (Runtime.Getruntime (). FreeMemory (); system.gc (); system.ter.println (Runtime.Getruntime (). FreeMemory ()); In this way, you can see that the value is not worth it to put GC (); put it here. ^ - ^

There may be a lot of skills using GC. If you find a better way, please don't forget to inform me.

转载请注明原文地址:https://www.9cbs.com/read-26909.html

New Post(0)