Use GC

xiaoxiao2021-03-06  72

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 when using him, be careful, otherwise it will be counterproductive. Below I will talk about some of the use of GC (for reference only)

1. Call GC () before will load a large number of resources: Cause: Because there are a lot of resources load, there may be temporary memory (this is mainly caused by excessive memory debris), and the system will automatically call the garbage collector. If the resource is too large (of course, it is less than the current available memory that the system, including the memory that has not been recycled), because the garbage collector may not complete the work of garbage, and the resources will be loaded, it will cause the available memory that cannot be satisfied. The resources are loaded, so the J2ME program will appear to stop responding 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-92309.html

New Post(0)