Search code examples
c++memoryserializationboostboost-serialization

boost serialization base class without base_object


I am using multiple inheritance with boost serialization. instead of doing

boost::serialization::base_object< Connection<T> >(*this)
boost::serialization::base_object< Collection<C> >(*this)

I am doing

template<typename ArchiveT>
void save(ArchiveT& arc, const unsigned version) const{
  //both Connection<T> and Collection<C> are Base Classes
  Connection<T>::save(arc, version);
  Collection<C>::save(arc, version);
}

and its working. So is two of them the same thing ? or there is any harm in doing this ? should I change this code ?

{This thing was coded long ago. So I forgot Why I coded it in that way. may be I was not aware of base_object at that time}

I am serializing huge set of data (~1.6 GB) .When I serialize I see serialization process is taking a lot of memory and hitting 3GB barrier. I've tried commenting serialization code and it takes < 50MB memory . So what makes serialization taking that big memory ?


Solution

  • You should change your code and serialize the base classes through base_object<>().

    From the documentation:

    [...] Note that this is NOT the same as calling the serialize function of the base class. This might seem to work but will circumvent certain code used for tracking of objects, and registering base-derived relationships and other bookkeeping that is required for the serialization system to function as designed. For this reason, all serialize member functions should be private.

    The extra memory consumption is probably caused by object tracking. If you have a large quantity of objects being tracked, the memory consumption can increase drastically during serialization.

    From special considerations:

    If it is known a priori that no pointer values are duplicated, overhead associated with object tracking can be eliminated by setting the object tracking class serialization trait appropriately.

    If you don't require object tracking, you may disable it with:

    BOOST_CLASS_TRACKING(the_class, boost::serialization::track_never)