LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2010, 09:18 AM   #1
gmwebway
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
In Linux, apart from malloc() what can increase the virtual memory of a process?


I have an application which has multiple threads and which is constantly allocating and deallocating memory (using malloc/free) After a while (normally days) the virtual memory starts greatly increasing while the number of threads remains constant. I have instrumented the malloc() usage with mallocinfo() and while the arena and allocated memory do increase during this period, the increase in virtual memory is an order of magnitude greater (eg the arena size reported by mallocinfo() is about 300M, but the virtual memory increased from 1.8G to about 2.9G.

When I first saw the virtual memory increasing, I suspected a memory leak caused by a pointer not being freed, but after unless mallocinfo() is not showing all of the malloc'd memory, the increase is happening elsewhere.
 
Old 10-19-2010, 09:29 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
There are enough possibilities that you can't do a good job guessing at them. In your situation you should look at the details.

While the process is still running and using significantly more virtual memory than you think is appropriate, grab a copy of the details of its virtual memory use:
Code:
cp /proc/pid/smaps some_file
Replace pid with the pid of the process you are investigating. Then look through some_file with a text editor to get some understanding of the virtual memory use of the process.

Further investigation depends on what you see in that file. If it is a massive number of tiny allocations, you might need some post processing to dig out the useful info. If you can find some very large individual allocations, those should be directly understandable.

As a first step in understanding one of those files myself, I used a smart search tool in my text editor that let me search for all occurrences of:
Size: followed by 12 ignored characters, followed by a non blank.
That finds every allocation of 100MB of more. In the large process I just looked at there was exactly one allocation over 100MB and that was the bulk of the process's memory use. So it was easy to find that and ignore all the tiny allocations. I don't know whether your example will be that easy. (I don't know how to do such text searches in whatever text editor or viewer you use. But if that is a problem, I'm sure plenty of others at LQ could answer that. I'm on a mixed OS lan, so I used Visual Studio on Windows to look at my file on Linux).

Last edited by johnsfine; 10-19-2010 at 09:54 AM.
 
1 members found this post helpful.
Old 10-27-2010, 07:55 AM   #3
gmwebway
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Original Poster
Rep: Reputation: 0
I have looked at /proc/pid/smaps both for the application before it starts misbehaving and after its virtual memory has increased.

The first observation is that the size of the heap allocation matches the output of mallinfo(). I have also identified the 'thread specific' data allocated using pthread_setspecific(). The problem allocations give no indication of what has caused them to be allocated.

There are a large number (hundreds) of anonymous allocations of 1024kB and 2048kB.

Code:
6f300000-6f500000 rw-p 00000000 00:00 0 
Size:               2048 kB
Rss:                  88 kB
Pss:                  88 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:        88 kB
Referenced:           88 kB
Swap:               1852 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
6f500000-6f600000 rw-p 00000000 00:00 0 
Size:               1024 kB
Rss:                  36 kB
Pss:                  36 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:        36 kB
Referenced:           36 kB
Swap:                932 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
6f600000-6f700000 rw-p 00000000 00:00 0 
Size:               1024 kB
Rss:                  16 kB
Pss:                  16 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:        16 kB
Referenced:           16 kB
Swap:               1008 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Prior to the memory usage 'going wild' these large allocations do not show.
 
Old 10-27-2010, 09:37 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Do you have a URL for documentation for mallocinfo?

In my first reply, I skipped over the fact that I don't know what mallocinfo is, because it didn't seem necessarily important.

I just did a google search and find lots of hits for mallocinfo, but none of them seem to be documentation.

In a typical implementation of malloc there is a max request size for ordinary allocations. Above that size, a different allocation method is used inside malloc (with results that should be transparent to the caller of malloc). That anonymous chunks you describe look like the result of malloc servicing requests for large chunks (but could be other things).

If those are large chunks requested from malloc, I assume they are not included in whatever you are getting from mallocinfo (or you wouldn't have asked the original question). So the next step would be figuring out why:

A) mallocinfo is only supposed to report on ordinary size chunks.

or B) mallocinfo has a bug

or C) Those chunks were allocated directly from the kernel by your application bypassing malloc.

Alternately, you could try instrumenting malloc to report the allocation and release of very large chunks.
 
  


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
process, memory, swap, virtual memory wakatana Linux - Hardware 1 08-31-2009 07:55 AM
LXer: Increase Swap Memory in Linux LXer Syndicated Linux News 0 04-10-2009 03:41 PM
Linux Virtual memory mapping to Board memory map !rajkums! Linux - Kernel 4 10-19-2008 12:27 PM
Linux shared memory segment access problem and x86 Virtual Memory layout. regmee Linux - Kernel 1 08-23-2008 12:11 AM
How Linux allocates memory for malloc in a program johnarg Linux - Newbie 2 06-25-2006 07:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:52 AM.

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