Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of memory Management relationship and memory leak principle in java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the java memory management relationship and memory leak principle of the example analysis, the article is very detailed, has a certain reference value, interested friends must read it!

Java memory Management relationship and memory leak principle

This is probably the closest to the bottom of a recent blog. Let's talk less and get to the point.

The relationship between java object and memory

First of all, we need to know the following truths (summarized by ourselves)

A complete process of establishing objects is 1 to declare objects, 2 to open up memory space, and 3 to associate objects with memory space.

An object can only correspond to one memory space, and one memory space can correspond to many objects.

Reclaim a memory space. If, this memory space does not have any object to have the connection with him. It can be recycled, or several circular references of objects can also be recycled.

When operating on an object, you first find the memory space through the object, and then manipulate the memory space (read, write, modify and delete)

These are the four experiences I have summed up.

It is particularly important to have this kind of understanding. No matter any language, it is ultimately reflected in physical memory, and objects and memory space are two different individuals. If not, then you will find what will be next!

Create object Stu one; / / only declare one object but not allocate memory space / / use new to open up a new memory space oneMemory, call constructor assignment, and associate memory space oneMemory with one object. One = new Stu ("one"); / / declare the two object and open up the memory twoMemory call constructor assignment, and associate the memory space twoMemory with the two object Stu two = new Stu ("two"); / / declare the three object and find the memory space oneMemory associated with the one object. And associate the oneMemory with the three object Stu three = one; / / at this time the memory space oneMemory is associated with the two objects. One is the one object, and the other is the three object System.out.println ("whether three and one are equal" + (three = = one) + "one hash value" + one.hashCode () + "three hash value" + three.hashCode ())

Running result

We can see that the three object and the one object point to the same memory space oneMemory. This is consistent with the second truth mentioned above. If we operate on the one object, the impact will also be reflected in the three object.

Because, * * they point to the same memory space, and to operate on one objects is to do the memory space oneMemory to operate. The three object also points to oneMemory. This accords with the fourth truth above. * * examples are as follows

System.out.println ("value of three object" + three.getName () + three.hashCode ()); / / modify the value of one. The first step is to find the memory space oneMemory associated with the one object, and change the name value in the memory space oneMemory to one.setName ("change"). / / when reading the value of the three object, first find the memory space associated with the three object oneMemory, and read the name value System.out.println ("value of the three object" + three.getName () + three.hashCode ()); the role of null

For a long time, all I knew was that if I copied a value to null, it would be empty. But I have no idea why.

Let's continue with the example above and look at a piece of code.

/ / when reading the value of the three object, first find the memory space oneMemory associated with the three object, and read the name value System.out.println ("value of the three object before" + three.getName () + three.hashCode ()); / * if we set the one object to null at this time. It has no effect on the memory space oneMemory. The function of null is to remove the connection between the one object itself and the memory space, and does not affect the connection between the memory space and other objects * / one = null; System.out.println ("value of three object after" + three.getName () + three.hashCode ())

Running result

We will find that after assigning the one object to a null value, the three object remains the same as before. It has always been thought that null is clear about the object and its memory space. But not now. The code comments are very clear. Null is not clearing the memory space, he is just severing the connection between the object itself and the memory space

Memory leak

If, you understand the above. So memory leaks are easy for you, too.

Let's learn a new concept.

There's so much up there. In this article, you only need to remember that the life cycle of variables, classes, and methods modified by static keywords accompanies the entire program. That is, they live as long as the program lives.

Next, look at the code.

First, we define a static collection staticList, which is created as soon as the program runs. The program will not be recycled until it is finished running.

StaticList staticList = new ArrayList (); / / Open up memory space listMemory System.out.println ("value of three object after" + three.getName () + three.hashCode ()) / * memory leak means that objects with a long life cycle are associated with a memory space, so that the memory space cannot be reclaimed * / / * add three objects to the static collection. Here's the step. The first step is to find the memory space associated with the three object (oneMemory). The second step is to find the memory space associated with the staticList collection object (listMemory). The third step is to tell the system that some members of the staticList collection object establish a relationship with the memory space oneMemory. * / staticList.add (three); / * here, even though the three object is no longer associated with the memory space oneMemory. OneMemory will not be recycled either, because it is said that the relationship between memory space and objects is 1-to-many. The condition of recycling is that a memory space can be recycled without a connection with the object. At this point, the memory space is associated with some members of the staticList collection object, so the memory space will not be reclaimed. And because the memory space of the staticList collection object is in the static storage area, it is accompanied by the whole program. So in the whole program life, the memory space oneMemory can not be recycled. It's a memory leak. * / three = null; System.out.println (staticList.get (0) .hashCode ())

Running result

You can see it. After we sever the three object assignment null from the memory space oneMemory. Some members of the static collection staticList object are still associated with the memory space oneMemory. According to the third above, because the memory space oneMemory is still associated with the object (staticList). So oneMemory memory space is not reclaimed. And because staticList is static, life is as long as a program. Then the oneMemory memory space will not be reclaimed throughout the program cycle. It causes a memory leak.

Attach the complete code

Package com.zfh.test;import java.util.ArrayList;import java.util.List;public class JavaMain {staticList staticList = new ArrayList (); / / Open memory space listMemory public static void main (String [] args) {Stu one; / / only declares one objects but does not allocate memory space / / use new to open up a new memory space oneMemory, call constructor assignment, and associate memory space oneMemory with one objects. One = new Stu ("one"); / / declare the two object and open up the memory twoMemory call constructor assignment, and associate the memory space twoMemory with the two object Stu two = new Stu ("two"); / / declare the three object and find the memory space oneMemory associated with the one object. And associate the oneMemory with the three object Stu three = one; / / at this time the memory space oneMemory is associated with the two objects. One is one object, the other is three object System.out.println ("whether three and one are equal" + (three = = one) + "hash value of one" + one.hashCode () + "hash value of three" + three.hashCode (); System.out.println ("value of three object" + three.getName () + three.hashCode ()) / / to modify the value of one, the first step is to find the memory space associated with the one object oneMemory, and change the name value in the memory space oneMemory to one.setName ("change"); / / when reading the value of the three object, first find the memory space oneMemory associated with the three object, and read the name value System.out.println ("value of the three object before" + three.getName () + three.hashCode ()). / * if we set the one object to null at this time. It has no effect on the memory space oneMemory. The function of null is to remove the connection between the one object itself and the memory space, and does not affect the connection between the memory space and other objects * / one = null; System.out.println ("value of three object after" + three.getName () + three.hashCode ()) / * memory leak means that objects with a long life cycle are associated with a memory space, so that the memory space cannot be reclaimed * / / * add three objects to the static collection. Here's the step. The first step is to find the memory space associated with the three object (oneMemory). The second step is to find the memory space associated with the staticList collection object (listMemory). The third step is to tell the system that some members of the staticList collection object establish a relationship with the memory space oneMemory. * / staticList.add (three); / * here, even though the three object is no longer associated with the memory space oneMemory. OneMemory will not be recycled either, because it is said that the relationship between memory space and objects is 1-to-many. The condition of recycling is that a memory space can be recycled without a connection with the object. At this point, the memory space is associated with some members of the staticList collection object, so the memory space will not be reclaimed. And because the memory space of the staticList collection object is in the static storage area, it is accompanied by the whole program. So in the whole program life, the memory space oneMemory can not be recycled. It's a memory leak. * / three = null; System.out.println (staticList.get (0). HashCode ();}}

Bean object is Stu

Package com.zfh.test;public class Stu {/ * static {System.out.println ("static code block I only call once");} * / private String name; / * {System.out.println ("construct code block");} * / public Stu (String name) {this.name = name;} public String getName () {return name } public void setName (String name) {this.name = name;} public void sout () {System.out.println (this.name+this.hashCode ());} public void printer () {System.out.println (Stu.class.hashCode ());} @ Override protected void finalize () throws Throwable {super.finalize (); System.out.println ("terminated") }} principle of detecting memory leaks

The key to detecting memory leaks is to be able to intercept calls to functions that allocate and free memory. By intercepting these two functions, we can track the life cycle of each block of memory, for example, every time a piece of memory is successfully allocated, its pointer is added to a global list; every time a piece of memory is freed, its pointer is removed from the list. In this way, when the program ends, the remaining pointers in the list point to memory that has not been freed. This is only a simple description of the basic principles of memory leak detection. Detailed algorithms can be found in Steve Maguire.

If you want to detect heap memory leaks, you need to intercept malloc/realloc/free and new/delete (in fact, new/delete ultimately uses malloc/free, so just intercept the previous group). For other leaks, similar methods can be used to intercept the corresponding allocation and release functions. For example, to detect BSTR leaks, you need to intercept SysAllocString/SysFreeString;. To detect HMENU leaks, you need to intercept CreateMenu/ DestroyMenu. (some resources have multiple allocation functions and only one release function. For example, SysAllocStringLen can also be used to allocate BSTR, so you need to intercept multiple allocation functions.)

Under the Windows platform, there are three kinds of tools commonly used to detect memory leaks: the built-in detection function of MS C-Runtime Library; the plug-in detection tools, such as Purify,BoundsChecker, etc.; and the use of Windows NT's own Performance Monitor. Each of these three tools has its own advantages and disadvantages. Although MS C-Runtime Library is weaker than external tools, it is free; although Performance Monitor cannot identify the code in question, it can detect implicit memory leaks, which is where the other two types of tools are powerless.

The above is all the contents of the article "example Analysis of memory Management relationship and memory leak principle in java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report