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

What is the cause of memory leak in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the cause of memory leak in Java". The explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the cause of memory leak in Java".

What is a memory leak

A memory leak refers to a continuous increase in memory footprint during the running cycle of an application. However, Java memory leaks are more accurately described as memory leaks that fail to recover to the initial memory size within one full garbage collection (Full GC) cycle of JVM and continue to grow after multiple full garbage collection, which is considered a memory leak.

Second, why the memory leak

Jvm introduces garbage collection mechanism, which was designed to solve the defect of manual memory release on C and C++ programming languages (not exactly a defect, but rather a hassle). To sum up the running process of GC, when the reference in the vm stack to the vm heap expires, GC will recycle this object in the vm heap, and GC will not recycle as long as the heap object reference exists all the time. Therefore, when the object is used and the reference is not broken, it will cause a memory leak. However, why the reference is not interrupted, please see the following article, "third, causes of memory leakage".

Third, the cause of memory leak

The root cause of the memory leak is that the reference is not broken. According to my personal experience, the reasons why the quotation has not been interrupted are as follows. If there are any mistakes or omissions, please forgive me.

3.1. The handle is not closed

Handles, which refer to the concept of the windows operating system, are called file descriptors on Linux, and Everything Is File (everything is a file) for Linux. The operation class designed to handle here is InputSteam,OuputStream,Socket,Connection... And its subclasses or implementation classes. Personally, I think that all classes in Java that implement the Closable interface will belong to the handle class. All handle classes must be closed after use, otherwise it will cause handle leakage and cause memory leakage.

Closabe API code:

Closable API implementation class:

For example: InputStream/FileInputStream

No memory leak running code result: number of handles 200

Running code with memory leak: number of handles 201

The reason is that InputStream implements the Closable interface and is not turned off when in use. Therefore, this handle will be occupied all the time, resulting in a memory leak.

Another person asks if a thread belongs to a handle, and I'm here to tell you that a thread is not a handle, but starting a thread will introduce about 7 handles. Take a look at the 3.2 thread test results.

3.2. The thread did not stop

3.2.1, threads are implemented independently in Java. There are two ways to implement threads. The first is to implement the runable interface, and the second is to inherit the Thread class. JVM continues to run when and only if the number of running threads in JVM is greater than 0.

3.2.2, there are three ways to stop a thread

3.2.2.1. The thread ends naturally and ends naturally after the thread finishes its work (died of natural aging)

3.2.2.2. Call the thread interrupt method, and another thread calls the running thread interrupt method (killed)

3.2.2.3. Call the thread stop method, and another thread calls the running thread stop method (killed, this method has expired)

Obviously, the way to use a thread to end naturally is the most harmonious. The natural end code is as follows:

Another thread continues to run the code:

The thread did not release the memory leak code:

Running the code without memory leaks results: after running, all threads end and JVM is closed.

There is a memory leak to run the code result: threads continue to be created, created threads continue to run, memory will not be released

JVM starts with 17 threads and 200handles by default. In this example, 1000 threads are started, the number of threads is increased to 1017 and the number of handles is increased to 7202. Each handle created will take up 7 handles and consume 1m of memory.

3.3. Exception not caught

The concept of exception handling is introduced into Java, which can catch all kinds of exceptions. Proper use of exceptions will help to improve the stability of the system.

In the description, the code Try...catch...finally is used to ensure that the close method can be called to avoid memory leaks.

According to good programming habits, when a piece of code is unknown and may have unknown exceptions, it is recommended to use Try...catch...finally statements to avoid some resources not being released due to unknown exceptions thrown by the program.

Continue to take 3.1 as an example:

No memory leak running code result: number of handles 200

Running code with memory leak: number of handles 201

3.4. Use static collection storage

Collections, as a fast storage scheme within JVM, are really easy to use, but improper use when defining storage can also lead to memory leaks because references exist all the time.

The result is obvious!

Thank you for reading, the above is the content of "what is the cause of memory leak in Java". After the study of this article, I believe you have a deeper understanding of what is the cause of memory leak in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report