On the Java memory management (two)

We almost can not change its policy of recovery mechanisms column from Java itself, but we can change our way of programming and precautions in the programming.

1.Java no functional objects in C ++ destructor, but Java's garbage collection mechanism is principled, it will not recover the object variable references. This object has no variable references it, it no longer possible to have any way to find the "lost" objects of --GC recycling is such an object. So when we determine that an object no longer has any value and meaning, all variables corresponding to the object reference all set to NULL, then the object is in line with the GC recycling standards. For us, it is equivalent to manually do the operation object destruction, as to whether it is recovered, it is Java's own problems, at least, and programmers irrelevant.

2. Of course, the IO class scanner, NIO class of objects such as sockets, Java still retains the close (), shutdown () methods, when we no longer use such objects, be sure to call these methods close objects , it is equivalent to object destruction and blanking.

3. In addition to the IO manual judgment, closed NIO object class, we can use the try () {} clauses (try statement with resources), such statements created object is placed in the resource block, take the meaning "Try open the scanner or the socket, if successful operation is performed behind; otherwise, the corresponding object is automatically closed. "

4. For such constants PI = 3.14 ......, when used, should avoid these constant objects to create their own, as far as possible the use of Java Math class has been defined in the PI, MAX_VALUE, MIN_VALUE static constants can reduce GC burden - after all of these constants can not be considered to be destroyed it.

5. Avoid creating objects for the same variable in a loop. When it comes to cycling, I had to mention recursion. We usually think of recursion stack area consuming more memory than the cycle, simply because the variables are recursive function as a unit produced, although variable with the same name, but they are stored in the memory unit different location, so the object does not appear almost GC phenomenon can not be recovered; variable but not the same cycle, the memory of a very important reason is to save a variable cycle operation in order to create a reference object, so that to the end of the cycle, multiple variables with the same variable name referenced multiple objects, then obviously at the end of the cycle under ideal conditions should only be a variable name refers to an object, the object before the cycle created in GC have become the target of recycling, but now objects created in all cycles have had a reference, but also It has the same variable name that will be manually set to empty all objects are empty. But more trouble.

 

6. When using generic collections HashMap, HashSet, etc., should organize your data as much as possible, to weaken the complex relationship between sets, otherwise, should be recovered as long as the collection and other collections even if only a relationship, GC will not recover this collection Object.

 

Guess you like

Origin www.cnblogs.com/fusiji/p/11409855.html