<div dir="ltr">Hi,<div><br></div><div>I am a little bit puzzled about the memory management in libclang. What I observed is, when run on mid-sized code base, a very high (understandable) memory consumption whereas it seems it is not possible to reclaim all the consumed memory back once we are finished with whatever we have been doing with libclang. For example, this is a code excerpt which should explain my words:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>// 1. Create an index</div><div>vector<CXTranslationUnit> tunits;</div><div>CXIndex idx = clang_createIndex(1, 1);</div><div><br></div><div>// 2. Parse each file found in directory and store corresponding TU into the vector</div><div>for (auto file& : directory) {</div><div>    CXTranslationUnit tu;</div><div>    if (!clang_parseTranslationUnit2(idx, file.path().c_str(), cmd_args, cmd_args_len, 0, 0, CXTranslationUnit_DetailedPreprocessingRecord, &tu)</div><div>        tunits.push_back(tu);</div><div>}</div><div><br></div><div>// 3. Cleanup</div><div>for (auto tu& : tunits) {</div><div>    clang_disposeTranslationUnit(tu);</div><div>}</div><div>clang_disposeIndex(idx);</div></blockquote><div><br></div><div>If I run this code on `cppcheck` code base (<a href="https://github.com/danmar/cppcheck">https://github.com/danmar/cppcheck</a>) which is somewhat a mid-sized project (it counts cca 300 C/C++ files), I get the following figures in terms of memory consumption for that particular process (based on `htop` output):</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>* app memory consumption after 2nd step: virt(5763M) / res(5533M) / shr(380M)</div><div>* app memory consumption after 3rd step:  virt(4423M) / res(4288M) / shr(65188)</div></blockquote><div><br></div><div>So, as it can be seen from the figures, even after the cleanup stage, both virtual and resident memory figures are still very high. Seems like the only part which has been reclaimed is the memory that has been associated with the TU's. All the other, I can guess, parsing artifacts are still being hold somewhere in the memory to which we don't have access to neither we can flush them using the libclang API. I even ran the code with valgrind and there were no memory leaks detected (only `still reachable` blocks).</div><div><br></div><div>Either I am missing something here or this might impose a memory issues for long-running non-single-shot tools (i.e. think indexer).</div><div><br></div><div>Can anyone comment on this issue?</div><div><br></div><div>Thanks,</div><div>Adi</div><div><div><br></div><div><br></div></div></div>