Reducing the memory of large unfamiliar code

Suppose you have a fairly large (~ 2.2 MLOC), rather old (running more than 10 years ago) Windows desktop computer in C / C ++. About 10% of the modules are external and have no sources, but only debugging symbols.

How would you decide to reduce the application memory area by half? At least what would you do to find out where memory is consumed?

+4
source share
7 answers

Override malloc () / free () and new () / delete () with wrappers that keep track of how large the distributions are and (by writing the call table and then resolving it against the symbol table) where they are made from. your shell displays any allocated memory.

This will allow you to get by where the biggest distributions are and catch any leaks.

+7
source

this is the description / skeleton of a memory tracing application. I used to reduce the memory consumption of our game by 20%. This helped me keep track of many of the distributions performed by external modules.

+3
source

This is not an easy task. Start by chasing any memory leaks that you discover. (A good tool would be Rational Purify .) Copy the source code and try to optimize data structures and / or algorithms.
Sorry if this might seem pessimistic, but a 50% reduction in memory usage doesn't seem realistic.

+2
source

It is likely that you can quickly find some significant inefficiencies. You must first check what memory is used for. A tool that I found very convenient for this, a memory validator

Once you have this "memory card", you can check out Low Fanging Fruit. Are there any data structures that consume a lot of memory that can be represented in a more compact form? This is often possible, especially. when data access is well encapsulated, and when you have spare CPU power, you can devote it to compression / decompression at every access.

+1
source

I do not think your question is well posed.

The size of the source code is not directly related to the size of the memory. Of course, the compiled code will take some memory, but the application may have its own memory requirements. Both static (variables declared in the code) and dynamic (an object created by the application).

I would advise you to profile the execution of the program and carefully study the code.

0
source

First places for me for me:

Does the application specify a lot of preallocation memory that will be used later? Is this memory often sitting around unused, never distributed? Think about switching to new / uninstall (or better use smart_ptr) as needed.

Does the code use a static array such as

Object arrayOfObjs[MAX_THAT_WILL_EVER_BE_USED]; 

and pass objs in this array? If so, consider manually managing this memory.

0
source

One tool for analyzing memory usage is LeakDiag, available for free download from Microsoft . This, apparently, allows you to connect all user-mode distributors to VirtualAlloc and instantly upload process snapshots to XML. These snapshots can then be used to determine which call stacks allocate most of the memory and which call stacks are flowing. It does not have a pretty interface for snapshot analysis (if you cannot get LDParser / LDGrapher through Microsoft Premier Support), but there is all the data.

Another thing to note is that you may have false positives from a BSTR distributor due to caching, see "Hey, why am I leaking all my BSTRs?"

0
source

Source: https://habr.com/ru/post/1277088/


All Articles