Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minimize memory consumption of c++ program using copy-on-write?

I'm working on an application which forks itself up to 8 times for parallelism. Each fork has a full copy of the memory space from the original process at the time of the fork. The forks are quick because Linux shares the pages between the processes and only creates new ones when they are modified. The growth in memory consumption, in practice, seems to be about 3x for my application. Any suggestions for tools or techniques to use in identifying changes that will reduce that growth?

One thought is to look at the page fragmentation of the modified pages. There's also just the brute force examination of what's allocated in the forked processes. In either case, what techniques or tools can you recommend for performing that analysis?

Keep in mind that the program takes several hours to complete even with parallelism and has a memory footprint of up to 1TB so instrumentation options are limited.

like image 305
dromodel Avatar asked Nov 28 '12 15:11

dromodel


People also ask

What is advantage of the copy-on-write operation?

The new allocation ensures that a change in the memory of one process is not visible in another's. The copy-on-write technique can be extended to support efficient memory allocation by having a page of physical memory filled with zeros.

What is copy-on-write memory?

Copy-on-write or CoW is a technique to efficiently copy data resources in a computer system. If a unit of data is copied but not modified, the "copy" can exist as a reference to the original data. Only when the copied data is modified is a copy created, and new bytes are actually written.


1 Answers

You can use vmstat, systemtap and or glibc's malloc-hooks to monitor consumption. You can use perf to see where faults are occurring in order to understand the actual impact of the consumption.

If your application faces TLB pressure when you use large memory pools (e.g. you are streaming through a very large volume of data), you can use huge/large pages to mitigate the overhead of 4k pages.

You can also use madvise to tell the kernel from within your processes what you're probably going to do with the memory that was allocated.

like image 88
Brian Cain Avatar answered Nov 15 '22 00:11

Brian Cain