Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't track down source of huge memory use

I've been trying to track down a memory problem for a couple of days - my program is using around 3GB of memory, when it should be using around 200MB-300MB. Valgrind is actually reporting that it is using ~300MB at its peak, and is not reporting any memory leaks.

The program reads an input file, and stores every unique word in that file. It is multi-threaded, and I've been running it using 4 threads. My major sources of data are:

  • Constant-size array of wchar_t (4MB total)
  • Map between words and a list of associated values. This grows with the size of input. If there are 1,000,000 unique words in the input file, there will be 1,000,000 entries in the tree.

I am doing a huge number of allocations and deallocations (using new and delete) -- at least two per unique word. Is it possible that memory I free is not being reused for some reason, causing the program to keep acquiring more and more memory? It consistently grabs more as it continues to run.

In general, any ideas about where I should go from here?

Edit 1 (based on advice from Graham): One path I'll try is minimizing allocation. I'll work with a single string per thread (which may grow occasionally if a word is longer than this string is), but if I remember my code correctly this will eliminate a huge number of new/delete calls. If all goes well I'll be left with: one-time allocation of input buffer, one-time allocation of string-per-thread (with some reallocs), two allocs per map entry (one for key, one for value).

Thanks!

like image 925
lapis Avatar asked Jul 15 '11 12:07

lapis


People also ask

How do I investigate high memory usage?

Check Computer Memory Usage EasilyTo open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories.

What causes 100% memory usage?

However, the high memory usage problem is mainly due to the overcrowding of many internal processes. Therefore, it helps to stop the unnecessary programs and applications that are running. Open the Task Manager and check any extra programs you aren't using. Right-click and end them.

How do I find out what processes are consuming my memory?

Using top. One of the best commands for looking at memory usage is top. One extremely easy way to see what processes are using the most memory is to start top and then press shift+m to switch the order of the processes shown to rank them by the percentage of memory each is using.

Why my RAM usage is so high?

The reasons that may cause high memory usage Windows 10 are various, but the common causes are as follows. Running too many programs at the same time. Registry hack. Defective program design.


1 Answers

It's likely to be heap fragmentation. Because you are allocating and releasing small blocks in such huge quantities, it's probable that there are loads of small free chunks which are too small to be reused by subsequent allocations. Since these chunks are effectively wasted, the process has to keep grabbing more and more memory from the system to honour new allocations.

You may be able to mitigate the effect by first reserving a sufficiently large default capacity in each string with string::reserve(), and then clearing strings to empty when you're finished with them (rather than deleting). Then, keep a list of empty strings to be reused instead of allocating new ones all the time.

EDIT: the above suggestion assumes the objects being allocated are std::strings. If they're not, then you can probably still apply the general technique of keeping old empty objects around for reuse.

like image 95
Graham Borland Avatar answered Oct 03 '22 05:10

Graham Borland