Menu

tinyxml sudden memory jump in multithreading

jia bei
2010-09-21
2024-11-19
  • jia bei

    jia bei - 2010-09-21

    my company use tinyxml as xml parser, but sometimes memory leak happens,so I check the code, and found that when pushing 1000 request per second with 20 threads, each thread declare a TiXmlDocument, and do a Parse(char* some),
    it work well at first , memory  is ok, but somehow a suddem memory jump will happen in less than 3 hours. 
    So fact shows it is not thread safe. But I dont understand, how this happen? why memory leak?  Usually  being not thread safe  means data-dirty, but why memory leak?
    Thanks

     
  • Mike Thomas

    Mike Thomas - 2024-11-19

    A sudden memory jump in TinyXML during multithreading can happen due to improper thread safety when multiple threads access or modify the same TinyXML data structures. TinyXML itself is not thread-safe, meaning simultaneous operations on shared objects can lead to issues like memory corruption or unexpected memory allocations.

    Potential Causes:
    Concurrent Memory Allocations:

    Multiple threads allocating memory for TinyXML nodes, elements, or attributes without proper synchronization can cause spikes or fragmentation.
    Shared Parser Object:

    Using the same TiXmlDocument or TiXmlElement object across threads without locking mechanisms might lead to undefined behavior, including memory jumps.
    Node Manipulation:

    Adding or removing nodes or attributes concurrently can lead to reallocations or spikes in memory usage.
    Improper Memory Deallocation:

    If threads are prematurely or incorrectly deleting nodes or documents, memory usage can jump as TinyXML tries to handle these operations unpredictably.
    Solutions:
    Use Mutexes or Locks:

    Protect shared objects with synchronization primitives like mutexes to ensure one thread accesses the data at a time.
    Thread-Local Instances:

    Avoid sharing TiXmlDocument or related objects between threads. Instead, create separate instances for each thread.
    Consider TinyXML-2:

    If possible, switch to TinyXML-2, which offers better memory management and thread-safety improvements over TinyXML.
    Analyze with Tools:

    Use memory profiling tools to identify where the memory jump occurs and whether specific operations are responsible.
    By isolating the operations and ensuring proper thread synchronization, such issues can often be mitigated.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.