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 linux Virtual memory, memory leak and Detection

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 introduces "linux virtual memory, memory leak and detection example analysis". In daily operation, I believe many people have doubts about linux virtual memory, memory leak and detection example analysis. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "linux virtual memory, memory leak and detection example analysis". Next, please follow the editor to study!

Memory leak

The so-called memory leak refers to memory that is no longer in use, is not released, and there is no chance to release it later.

Excessive memory footprint refers to a large number of requests for memory, but does not release memory in a timely manner, but for languages like C++ that do not have a garbage collector, it is equivalent to a memory leak.

Memory leak detection is divided into static detection and dynamic detection, that is, compile-time detection and run-time detection.

Static detection, such as code static checking tools such as pclint,pgrelief, can detect potentially problematic code in advance at compile time.

Dynamic detection, as follows:

Common detection tools for memory leaks (runtime detection)

C/C++1. Valgrind: Debugging and profiling Linux programs, aiming at programs written in C and C++ 2. Ccmalloc: simple use of C and C++ programs under Linux and Solaris and malloc Debug Library 3. LeakTracer: tracking and analyzing memory leaks in C++ programs under Linux, Solaris and HP-UX 4. Electric Fence: malloc () Debug Library written by Bruce Perens in Linux Distribution 5. Leaky: memory leak Detection Program under Linux 6. Dmalloc: Debug Malloc Library 7. MEMWATCH: written by Johan Lindh Is an open source C language memory error detection tool, mainly through gcc's precessor to 8. KCachegrind: A visualization tool for the profiling data generated by Cachegrind and Calltree Java1. Memory Analyzer: is an open source JAVA memory analysis software, find memory leaks, can easily find large chunks of memory and verify who has been occupying it, it is based on Eclipse RCP (Rich Client Platform), you can download a separate version of RCP or Eclipse plug-in 2. JProbe: analyze Java memory leaks 3. JProfiler: a full-featured Java profiling tool designed to analyze J2SE and J2EE applications. It combines CPU, thread and memory profiling in a powerful application. GUI can find performance bottlenecks, catch memory leaks, and solve thread problems. 4. JRockit: used to diagnose Java memory leaks and point out the root causes, specifically targeted and optimized for Intel platforms Get the highest performance on Intel hardware 5. YourKit .NET & Java Profiling: industry-leading Java and .NET program performance analysis tools 6. AutomatedQA: AutomatedQA's award-winning product performance profiling and the next-generation replacement of the memory debugging toolset supports Microsoft, Borland, Intel, Compaq and GNU compilers. Comprehensive and detailed reports can be generated for .NET and Windows programs to help you easily isolate and troubleshoot performance and memory / resource leaks in your code. Supports .net 1.0, 1.1, 1.1, 2.0, 3.0 and Windows 32 + 64-bit applications 7. Compuware DevPartner Java Edition: includes Java memory detection, code coverage testing, code performance testing, thread deadlock, distributed applications and other major functional modules

Use of Valgrind-compile and install (it is recommended to go to the official website to download the latest version)

Download the latest version of tar xvf valgrind-3.4.1.tar.bz2cd valgrind-3.4.1/./configure-- prefix / home/zhenghan.zh/valgrindmakemake install by wget http://valgrind.org/downloads/valgrind-3.4.1.tar.bz2 or the official website https://www.valgrind.org/downloads/current.html

Use of Valgrind-Runtime Detection

A piece of code without errors

# include # include void fun () {int* p = (int*) malloc (10*sizeof (int)); p [9] = 0; free (p);} int main () {fun (); return 0;}

Gcc-g-O0 1.c

Valgrind-tool=memcheck-leak-check=full. / a.out

The results are as follows:

Wanglc@wanglc-VirtualBox:~/cpp$ valgrind-tool=memcheck-leak-check=full. / a.out

= = 29920 hours = Memcheck, a memory error detector

= = 29920 hours = Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.

= = 29920 hours = Using Valgrind-3.16.1 and LibVEX; rerun with-h for copyright info

= = 29920 hours = Command:. / a.out

= = 29920 years =

= = 29920 years =

= = 29920 hours = HEAP SUMMARY:

= = 29920 hours = in use at exit: 0 bytes in 0 blocks

= = 29920 bytes allocated = total heap usage: 1 allocs, 1 frees, 40 bytes allocated

= = 29920 years =

= = 29920 hours = All heap blocks were freed-- no leaks are possible

= = 29920 years =

= = 29920 hours = For lists of detected and suppressed errors, rerun with:-s

= = 29920 contexts = ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

A memory error code.

# include # include void fun () {int* p = (int*) malloc (10*sizeof (int)); p [10] = 0;} int main () {fun (); return 0;}

Gcc-g-O0 1.c

Valgrind-tool=memcheck-leak-check=full. / a.out

The results are as follows (both unreleased and array out of bounds are detected):

Wanglc@wanglc-VirtualBox:~/cpp$ valgrind-tool=memcheck-leak-check=full. / a.out

= = 30261mm = Memcheck, a memory error detector

= = 30261 Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.

= = 30261mm = Using Valgrind-3.16.1 and LibVEX; rerun with-h for copyright info

= = 30261 a.out = Command:. / a.out

= = 30261 =

= = 30261mm = Invalid write of size 4

= = 30261 fun = at 0x108668: fun (1.c:7)

= = 30261 main = by 0x10867E: main (1.c:12)

= = 30261 alloc'd = Address 0x522e068 is 0 bytes after a block of size 40 alloc'd

= = 30261 malloc = at 0x4C2FECB: malloc (vg_replace_malloc.c:307)

= = 30261 fun = by 0x10865B: fun (1.c:6)

= = 30261 main = by 0x10867E: main (1.c:12)

= = 30261 =

= = 30261 =

= = 30261mm = HEAP SUMMARY:

= = 30261 blocks = in use at exit: 40 bytes in 1 blocks

= = 30261 bytes allocated = total heap usage: 1 allocs, 0 frees, 40 bytes allocated

= = 30261 =

= = 30261 blocks are definitely lost in loss record = 40 bytes in 1 blocks are definitely lost in loss record 1 of 1

= = 30261 malloc = at 0x4C2FECB: malloc (vg_replace_malloc.c:307)

= = 30261 fun = by 0x10865B: fun (1.c:6)

= = 30261 main = by 0x10867E: main (1.c:12)

= = 30261 =

= = 30261mm = LEAK SUMMARY:

= = 30261 blocks = definitely lost: 40 bytes in 1 blocks

= = 30261 blocks = indirectly lost: 0 bytes in 0 blocks

= = 30261 blocks = possibly lost: 0 bytes in 0 blocks

= = 30261 blocks = still reachable: 0 bytes in 0 blocks

= = 30261 blocks = suppressed: 0 bytes in 0 blocks

= = 30261 =

= = 30261mm = For lists of detected and suppressed errors, rerun with:-s

= = 30261 contexts = ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

At this point, the study on "linux virtual memory, memory leak and detection example analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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