Handling memory vulnerabilities in the Java program (on)

xiaoxiao2021-03-06  38

When processing memory leaks in Java programs (on) research should focus on how to prevent memory leaks and memory leaks (Author: Transfer from IBM DeveloperWorks Jim Patrick April 2001 12 Ri 11:40) Java program also has memory leaks? Of course. In contrast to popular concepts, in Java programming, memory management is still a problem that needs to be considered. In this article, you will learn what will cause memory vulnerabilities and when you should pay attention to these vulnerabilities. You also have the opportunity to practice to solve the problem in your own project. Most of the programmakers in the Java program have known that the use of programming languages ​​like Java is that they don't have to worry about the distribution and release of memory. You only have to create an object. When the application is no longer needed, Java will delete these objects through a mechanism called "garbage collection". This treatment means that Java has solved annoying problems that plague other programming languages ​​- terrible memory vulnerabilities. is it really like this? Before discussing, let's take a look at the work mode of garbage collection. The work of the garbage collector is to discover the objects that the application is no longer needed and delete them when these objects are no longer accessed or referenced. The garbage collector starts from the root node (those that always exist within the entire living cycle of the Java application), and spread all nodes that are referenced. While it traversed these nodes, it tracks which objects are currently being referenced. Any class is only the conditions for garbage collection as long as it is no longer cited. When these objects are deleted, the memory resources they occupy will be returned to the Java Virtual Machine (JVM). So, this is true that Java code does not require programmers to be responsible for memory management and clearance, which automatically performs garbage collection to useless objects. However, what we want to keep in mind is only the statistics only when an object is no longer quoted. Figure 1 illustrates this concept. Figure 1. Items useless but still cited above illustrates two classes with different survival cycles during the Java application execution. Class A is first instantiated and exists during a long period of time or the entire survival period. At some point, class B is created, class A adds a reference to this newly created class. Now, we assume that class B is a user interface widget, which is displayed by the user even released. If the category A to B is not cleared, even if Class B is no longer needed, the class B will still exist and occupy the memory space after executing the next garbage collection cycle. When you should pay attention to the memory vulnerability If your program issues a java.lang.outofMemoryError error after execution, the memory vulnerability is definitely a major suspect. In addition to this obvious case, when should I pay attention to the memory vulnerability? The programmer who holds the perfectist view will definitely answer, and you should find and correct all memory vulnerabilities. However, there are several aspects that need to be considered before drawing this conclusion, including the surplus and vulnerabilities of the program. There is a possibility that the garbage collector may never run within the survival of the application. This is the case if the JVM does and whether it will call the garbage collector - even if the program explicitly calls System.gc (). Typically, the JVM does not automatically run the garbage collector when the current available memory satisfies the memory requirements of the program. When the available memory does not meet the requirements, the JVM will first try to release more available memory by calling garbage collection. If this attempt still does not release enough resources, JVM will get more memory from the operating system until the maximum limit allowed. For example, consider a small Java application that displays some simple user interface elements for modifying the configuration, and it has a memory vulnerability.

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

New Post(0)