![]() |
| | #1 |
| Registered User Join Date: Apr 2007
Posts: 13
| leaked memory not freed after process is killed? i'm trying to verify and track down what seems to be a memleak in a C program that runs in daemon fashion 24/7. I'm using Ubuntu. The process crashes every few days due to lack of memory apparently... My doubt is that if there was a memleak wouldn't the memory be freed after the process was killed by the OS? Is there a chance that the memory is not released to the OS even if the allocator process is dead? thanks |
| nantonop is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Yes, if the process is killed, then the memory that process owns will be released. There are some forms of memory that may not be freed (until ALL processes owning it are killed), such as shared memory between processes. But normal memory allocated with malloc or related functions should be freed. You may want to investigate one of these two methods: 1. Use a library with "memory leak detection". 2. Add your own extended malloc/free functions that keep track of where and how much memory is allocated, and every now and again run through to see how memory is allocated. Print any difference between two runs to a log-file or some such. -- Mats |
| matsp is offline | |
| | #3 |
| Registered User Join Date: Apr 2007
Posts: 13
| i've actually been using valgrind to get an indication of what's going on since the application is rather big... It get's a bit messy since there are external libs used and to this point i can't be 100% sure who's causing the leak... thanks for your suggestion! |
| nantonop is offline | |
| | #4 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| by defining your own allocation, you could use a macro like: Code: #define malloc(x) mymalloc(x, __LINE__, __FILE__) #define free(p) myfree(p, __LINE__, __FILE__) Code: struct memnode {
struct memnode *next;
size_t size;
char *file;
int line;
int seen;
};
-- Mats |
| matsp is offline | |
| | #5 | |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,680
| Quote:
Another option is to use Electric Fence (gcc prog.c -lefence) to also diagnose mis-use of allocated memory. Use the 'top' (or 'ps') command periodically to see how much memory is allocated to each process. If you see this creeping up for your program, then it is definitely leaking memory. | |
| Salem is offline | |
| | #6 |
| Registered User Join Date: Apr 2007
Posts: 13
| Thanks for your suggestions. i shall be having a go at the malloc wrapper asap. However check this out: This is a 'top' for my situation. I'm running the app thorugh valgrind (appears as 'memcheck'). The server has now been up for 5 days and the app is still ok. But what you can make of this is that the total memory usage report (which is that about all mem is used and a small bit of swap too) has no correlation to the mem allocated to running processes. memcheck takes up 7.4% and no other process takes up more than 1% (cut short for brevity - take my word...)... How can this be? I do have the feeling that info from 'top' is not very reliable... Code: top - 12:12:18 up 5 days, 49 min, 2 users, load average: 2.56, 2.98, 2.86 Tasks: 92 total, 2 running, 89 sleeping, 0 stopped, 1 zombie Cpu(s): 43.8% us, 1.5% sy, 0.0% ni, 54.1% id, 0.0% wa, 0.2% hi, 0.5% si Mem: 2575056k total, 2556844k used, 18212k free, 174936k buffers Swap: 2000084k total, 176k used, 1999908k free, 1860680k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 5560 root 15 0 574m 185m 2884 S 91 7.4 5865:02 memcheck 10176 backuppc 20 0 105m 78m 1308 S 0 3.1 51:58.58 BackupPC_dump 4868 mysql 15 0 143m 22m 4952 S 0 0.9 0:28.49 mysqld 5402 gdm 16 0 67508 14m 6992 S 0 0.6 0:01.97 gdmgreeter 4633 root 16 0 53172 9764 3992 S 0 0.4 0:14.70 Xorg Last edited by nantonop; 09-04-2007 at 03:12 AM. |
| nantonop is offline | |
| | #7 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,680
| Yeah, perhaps top was not the thing to use - by default, it measures the CPU time. Perhaps there is a way to get top to display other parameters as being the top-most. Which your daemon may not use that much of. So perhaps 'ps', restricted to the PIDs of interest would be a better command. |
| Salem is offline | |
| | #8 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
@nantonop: Could you edit your post to have code-tags around the data you posted, so that the columns line up - it's so much easier to read that way. -- Mats | |
| matsp is offline | |
| | #9 |
| Registered User Join Date: Apr 2007
Posts: 13
| sorry found it! see previous post. top sorted by 'M' and text alligned. thanks Last edited by nantonop; 09-04-2007 at 03:16 AM. |
| nantonop is offline | |
| | #10 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| From what I can determine: This is essenitally free memory 1860680k cached See: http://gentoo-wiki.com/FAQ_Linux_Memory_Management So you have plenty of free memory available. -- Mats |
| matsp is offline | |
| | #11 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| |
| brewbuck is offline | |
| | #12 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| Quote:
Your memory usage is fine. Just because "Free" says only 18 megs doesn't mean you've only got 18 megs free. That's not what "free" means. | |
| brewbuck is offline | |
| | #13 | |
| Technical Lead Join Date: Aug 2007 Location: London, UK
Posts: 723
| Quote:
QuantumPete
__________________ "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support "Have you tried turning it off and on again?" - The IT Crowd | |
| QuantumPete is offline | |
| | #14 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
I agree that leaking enough memory to use up 2GB in a matter of days is serious leakage. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #15 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| Quote:
Of course, just adding cache + free doesn't give you the right number either. It's actually pretty hard to define exactly how much memory is "free" at any one moment. | |
| brewbuck is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How can you make a parent process wait for a child? I'm gettin a seg fault. | mr_coffee | C Programming | 3 | 10-15-2008 09:24 AM |
| To find the memory leaks without using any tools | asadullah | C Programming | 2 | 05-12-2008 07:54 AM |
| Problem with forking a process | Unitedroad | C Programming | 10 | 10-04-2007 01:43 AM |
| process programming | St0rM-MaN | Linux Programming | 2 | 09-15-2007 07:53 AM |
| Suggestions on this C style code | Joelito | C Programming | 11 | 06-07-2007 03:22 AM |