Experience at the beginning of Memory Management

Source: Internet
Author: User
Tags valgrind
It is said that there has actually been a month.
The AC string matching algorithm is implemented in C language. First, the function of fixed allocation of string length is implemented, and then dynamic memory allocation is performed in the case of unknown number of keywords in the actual project, the memory recovery and leakage become unavoidable. Gcc-g test. C-O test./AC. c
Instructor Luo pointed out: use the top command to check the memory usage, which is what happened last Friday. I went back to learn piano over the weekend to see if the film was not shown in a table. This week's efficiency was not very high. It wasn't until last night when everyone slept for that quiet half an hour that the idea was truly clear, this morning, it came to an end.
Looking back, it seems that the overall process and ideas are like this. I don't know if there are any detours.
1. Real-time Monitoring of memory consumption: first run the program cyclically and directly add while (1) without stopping. Soon the seggmentation fault will pop up. After sleep (1) is added, the program can work slowly and methodically;
In this case, the new terminal uses the "PS-Ef" command to view the ID of the currently running program and find the process ID used for testing;
Use "Top-D 2-P idnum" to refresh the memory usage of the process (the program we run) every two seconds. The result is similar to the following, I don't know much about it, but I don't know much about my past experience. I aim to continue with my goal.
I learned a little bit here: the results of viewing the top performance on a multi-core computer will be a little dazzled. After enabling it, press "1" to separate the usage of each CPU.
In the display, virt refers to the total virtual memory of the program running, and Res refers to the physical memory that has been used by the memory and has not been released. For details, refer to here. As a result, virt is indeed getting bigger and bigger, but only when there are many errors will it fail to wait for it
2. Check whether the valgrind tool is mainly used here. There are many instructions and many parameters, but I used the command "valgrind -- tool = memcheck -- leak-check = full. /test ", and then added the-V parameter to view the specific error location.
There are two main types of exposed errors:
1. "Conditional jump or move depends on uninitialised value (s )"
The main problem in this part is that no initialization parameter is used. As far as I am concerned, memset is not used for memory resetting for the parameters just allocated.
memset(pointer*,0,sizeof(pointer));
Or use
bzero(pointer*,sizeof(pointer));
This error is generally handled.
In the final debugging, the uninitialized parameters are still used through the addition of the-V parameter. After finding N for a long time, I found that the problem lies in the inconsistency of ideas before and after programming, at first, an array is designed with a length of 30. After that, the parameter memory (Allocation and) is initialized based on the length of the obtained keyword, however, when processing the results one by one, a loop from 0 to 30 is used. Modify the initialization length memset (x, 0, sizeof () * 30.
Second: "invalid read of size 4"
The main cause of this problem is to read unallocated memory content. Many examples provided on the Internet read n more bytes, the problem with your own program is that "address 0x41f1638 is 0 bytes after a block of size 480 alloc 'd" is 0 bytes after the allocation of 480. It is a bit discouraged when you see this, the day before yesterday, I talked to Jack about the memory leakage problem. He said that I should allocate more memory instead of how much I need theoretically. I am a little confused when I see this 0, isn't it helpful to add a few words? Fortunately, I found an example of the same error as me. I didn't count the problem of the 0 bytes. I ran to the source code to find the cause myself.
I'm very happy that through these two days of memory, I can see the problem clearly when I look at my program. valgrind located the problem on the two queues I defined. It turns out that there is a problem with the release of the node memory in the queue. If you like to use the pointer to transmit it, the problem will be reported. by allocating the memory, you can copy the content of the node in the queue one by one, after processing, the newly allocated memory is released so that all nodes in the queue are processed cyclically. The release of the queue node is placed in the destroy function for the AC at the end. So valgrind shows that my program is wrong ~~
Procedural Yuan's faint sorrow ~
Summary:
I learned how to view the memory used by the process in real time and how to use the valgrind tool to check program problems, mainly because of memory problems;
In terms of your own programming methods, pay attention to the recycling methods when allocating and reclaiming memory, especially when allocating memory, it is better to handle the memory allocation with ease than to handle the cart, which may cause some trouble for Processing Memory Data and reclaiming memory; it is convenient to write multiple sub-functions for program encapsulation. Although all the returned items are pointers, it is unnecessary to affect the global variable pointers and content called in sub-functions, it is recommended that the returned pointer be used to allocate memory and copy the value of memory data in the subfunction. Another point is that after memory allocation, do not forget to set memset () to zero for the allocated memory.
In the process of program debugging, Baidu and Google found a lot of good articles. If they were reluctant to finish reading them, they turned them off and happened to find Evernote. Here we will attach a link for future reference and further study:
http://www.ibm.com/developerworks/cn/linux/l-pow-debug/
http://www.51testing.com/?uid-170805-action-spacelist-type-blog-itemtypeid-5378
http://www.ibm.com/developerworks/cn/linux/l-cn-valgrind/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.