2

[This is a followup to a related question here.]

My code has a loop like the following:

    Document d(kObjectType);
    while (not done() and getNewStuff(d)) {
        process(d);
        d.RemoveAllMembers();
    }

While this code produces the desired results, each iteration appears to allocate new memory for the members of the document; and, when the code is run in limited configurations, it exhausts all memory and terminates prematurely.

The document which is populated by getNewStuff() is arbitrarily complex (that is, it may contain nested objects, arrays, arrays of objects, etc.), and it uses the default allocation method. The answer to the previous question indicated that when the nested objects and/or arrays were destroyed, their storage would be returned to the the allocator (and not to the system). This explains why the process memory consumption doesn't drop when the members are removed. (However, I've confirmed via Valgrind that there is no "memory leak", because the memory is all still properly accounted for, and, when the process exits, it is all properly freed.)

However, it doesn't explain why the process memory consumption continues to increase over time. It seems clear that the call to RemoveAllMembers() is not making the memory underlying those members available for reuse by subsequent AddMembers() (etc.) calls.

My question is, what do I need to do, in addition to or in place of calling RemoveAllMembers() to allow my Document object (d) to be reused on the next iteration?

(By the way, I tried moving the declaration of d to the inside of the loop, and this does indeed produce the desired memory behavior, but I would rather avoid the overhead of destructing and re-constructing d on each iteration.)

Thanks!

webbnh
  • 66
  • 6
  • This may answer your question: https://github.com/Tencent/rapidjson/issues/368 – jciloa Dec 19 '19 at 11:49
  • Thanks, @jciloa -- it looks like I should be using `d.SetObject()` rather than `d.RemoveAllMembers()`. I'll give that a try when I get a chance. (I suspect that the result will be no different from the approach that I'm currently using, of simply declaring `d` inside the loop, but it might yield prettier code.) – webbnh Dec 19 '19 at 19:50

0 Answers0