LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-09-2014, 10:41 AM   #1
RuijunFan
LQ Newbie
 
Registered: Oct 2014
Posts: 13

Rep: Reputation: Disabled
freed memory does not return to system , is it able to used by running application


I have a c program running on linux .
the program is supposed to run forever .
inside the code , there are malloc() and free() called to allocate memory and free them.
I used valgrind to check, no memory leak occured .
However ,when the program is running , I use gneom-system-monitor to watch the memory , the sise of taken memory is growing bigger and bigger and never decreased a little bit.

I googled online, some said ,though the program freed memory , but it does not return to system immidiately instead, it is kept in the application for reuse. when the program terminated, the memory then return to system.

I want to know if it is correct,
and if so , why the size of taken memory keep increasing .

Could you tell me
1, how to let the freed memory return to system for reuse immidiately
2, if the freed memory is kept in applition for reuse, how can I let the genom-system-monitor find the real number of taken memory



Thanks in advance.

Rui
 
Old 10-09-2014, 02:02 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Have a look at the man page for malloc ( /usr/share/man/man3/malloc.3 ) It doesn't say any such thing, some linux programs are run continuously for long periods of time and don't suffer this problem I would say you have another memomry leak somewhere, of course I could be wrong!
 
Old 10-10-2014, 06:40 AM   #3
RuijunFan
LQ Newbie
 
Registered: Oct 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
it looks not like memory leak

Hello, thanks for reply.
I did some test about memory allocation and free , and understand how it works in linux .
when freed memory , the memory will not return to system ,instead it will be kept in this process for reuse. when allocating memory next time , the memory will be used first, then if it is not enough, apply for more memory again system.

my issue is still there , the c program has been running for years on unix, and never have this issue( the occupied memory becomes bigger and bigger ), now I migrate it to linux , the issue appeared .

anybody could shed any light on this

Thanks.
Rui
 
Old 10-10-2014, 07:15 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by RuijunFan View Post
I googled online, some said ,though the program freed memory , but it does not return to system immidiately instead, it is kept in the application for reuse. when the program terminated, the memory then return to system.

I want to know if it is correct,
That is correct.

Quote:
and if so , why the size of taken memory keep increasing .
I would need at lot more details to even make a guess.

Quote:
1, how to let the freed memory return to system for reuse immidiately
Over a certain chunk size (which I think is hard wired inside malloc) memory is returned as soon as it is freed.

For small chunks, it really isn't practical to return memory to the system when freed.

Some operations in some programs allocate a very large number of small chunks for the duration of the operation (but much less than the duration of the program) and then free ALL of those small chunks at the end of the operation. In that case, it is more efficient (though much more complicated) to set up your own pool allocator for those small chunks: Grab one or more giant chunks from malloc (big enough they will be returned to the OS when freed) and subdivide those into the smaller chunks you need. C++ has special features to make that easier. In C it needs larger changes to your program, but is still practical.

Quote:
2, if the freed memory is kept in applition for reuse, how can I let the genom-system-monitor find the real number of taken memory
You can't.

Quote:
Originally Posted by RuijunFan View Post
my issue is still there , the c program has been running for years on unix, and never have this issue( the occupied memory becomes bigger and bigger ), now I migrate it to linux , the issue appeared .
That is hard to guess without more info on the allocation and free patterns and the time scale and the growth scale.

Memory may get fragmented as chunks of varying sizes are allocated and freed in a complex pattern, so the total usage by the process from the viewpoint of the system can grow for a long time while the total usage by the program from its own point of view is fairly stable. That effect is not unbounded. At some point the new allocations start fitting consistently into the previous fragments and the process size stops growing.

But if you have an actual memory leak in the program, rather than a fragmentation effect, it could grow until you stop it.

Last edited by johnsfine; 10-10-2014 at 07:21 AM.
 
1 members found this post helpful.
Old 10-10-2014, 08:58 AM   #5
RuijunFan
LQ Newbie
 
Registered: Oct 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks.
I am sure it is a memory leak .
The program uses a for loop and sleep() to run forever .
After each iteration , the occupied memory grow a little bigger .

The thing is that it runs well on unix rather than on Linux ,so there must be some difference between unix and linux that causes the issue .

Thanks again for your kindness . Johnsfine.

Rui
 
Old 10-10-2014, 04:15 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
One place I have found memory leaking (slowly) is in the use of file descriptors.

A fopen will allocate more memory...An fclose CAN release it. However, it depends on the system if what actually gets used is freopen. Different systems can do different things, and there could be a bug in freopen/fdopen as it isn't used that often.

The leak I detected was my own (I forgot to close the file), but every time it was by 4K.
 
1 members found this post helpful.
Old 10-16-2014, 07:21 AM   #7
RuijunFan
LQ Newbie
 
Registered: Oct 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks Jpollard,
it helps.

Rui
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory is got freed by kernel gopinathn Linux - Kernel 3 11-11-2010 10:31 PM
Visibility of Freed Memory jaypas Linux - Newbie 2 07-08-2010 07:27 AM
system doesnt reclaim the freed memory after free() muralia Linux - Newbie 2 07-20-2009 10:56 PM
Memory not being freed after program is finished running. keiths121 Linux - Newbie 5 06-03-2009 11:41 AM
pthreads virtual memory usage -- memory is not freed after thread exit minimol Linux - General 2 05-26-2009 01:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration