Memory reduction of GMSH used as an external lib for ParaView plugin
The goal of this branch is to reduce the memory footprint of Gmsh, especially when used as an external library for a ParaView plugin.
So far, the first commit ensures a proper deallocation of all the PView objects in GmshFinalize (critical for large meshes in production mode), whereas the second commit forces a memory deallocation of static std vectors (important for heavy mesh partitions, and/or large number of adaptation levels).
changed title from Memory to Memory reduction for ParaView pluginToggle commit list
changed the descriptionToggle commit list
changed title from Memory reduction for ParaView plugin to Memory reduction of GMSH used as an external lib for ParaView pluginToggle commit list
From http://www.cplusplus.com/reference/vector/vector/clear/ (and observations with valgrind which finally pointed me to this suspect for some particular configurations):
"std::vector::clear() removes all elements from the vector (which are destroyed), leaving the container with a size of 0. A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap: vector().swap(x); // clear x reallocating
The purpose of keeping the capacity of the vector unchanged after a call to clear() is to avoid costly reallocation of the vector in case it gets filled again later. The capacity of the vector is reset to zero only when the std::vector goes out of scope. However, there are several static std::vector defined in Gmsh which by definition never go out of scope, hence the issue (I probably introduced myself some of the worst cases in adaptiveData.cpp in terms of memory consumption).
The call to swap is a trick which will create an empty vector (vector() calls the constructor) with no memory allocated and swap it with the vector of interest. This effectively deallocates the memory since vector() creates an r-value reference which immediately goes out of scope and gets destroyed.
In theory, we could remove the call to clear() before the call to swap() but I thought it would be clearer for the reader to still call clear() so that the purpose of these lines appears clearly (destroy the content of the vector AND its memory). It does not cost anything to keep the call to clear() though since the r-value will perform the same action as clear() when it goes out of scope if the vector has not been cleared explicitly before.
mergedToggle commit list