Skip to content

Unexpected memory footprint differences #1043

@lucacarniato

Description

@lucacarniato

Description

The memory footprint in BOPAlgo_PaveFiller::MakeBlocks (BOPAlgo_PaveFiller_6.cxx) was recently improved by introducing a dedicated incremental allocator for variables that can be cleared inside the face‑to‑face loop (see pull request #864).

This optimization reduced the memory spike by more than 57% for a specific case (from 38.7 GB down to 16.2 GB). However, creating the incremental allocator and associated variables inside the loop reduces the memory footprint even more, from 16 GB to 2.6 GB, without a noticeable performance regression.

Memory usage was measured before completing each loop iteration, and the maximum value observed was logged, as illustrated in the snippet below.

Expected Behavior

The memory footprint of the two approaches is expected to be similar. The incremental allocator should reuse the peak memory previously allocated, which should be similar for both approaches.

Actual Behavior

The memory footprint of the two approaches is very different. Creating the incremental allocator inside the loop reduces the memory footprint much more.

Sample Code or DRAW Tcl Script

Original code:

occ::handle<NCollection_IncAllocator> aTmpAllocator = new NCollection_IncAllocator;
  
  // Per-iteration collections (use temporary allocator, reset each iteration)
  NCollection_List<int> aLSE(aTmpAllocator), aLBV(aTmpAllocator);
  NCollection_Map<int>  aMVOnIn(100, aTmpAllocator), aMVCommon(100, aTmpAllocator),
    aMVStick(100, aTmpAllocator), aMVEF(100, aTmpAllocator), aMVBounds(100, aTmpAllocator);
  NCollection_IndexedMap<occ::handle<BOPDS_PaveBlock>> aMPBOnIn(100, aTmpAllocator);
  NCollection_Map<occ::handle<BOPDS_PaveBlock>>        aMPBCommon;
  NCollection_DataMap<int, double>                     aMVTol(100, aTmpAllocator);
  NCollection_DataMap<int, NCollection_List<int>>      aDMBV(100, aTmpAllocator);
  
  ...
  
  const int aNbFFPrev = aNbFF;
  for (i = 0; i < aNbFF; ++i, aPS.Next())
  {
    ....
    aMVOnIn.Clear();
    aMVCommon.Clear();
    aMPBOnIn.Clear();
    aMPBCommon.Clear();
    aDMBV.Clear();
    aMVTol.Clear();
    aLSE.Clear();
    aLBV.Clear();
    aMVStick.Clear();
    aMVEF.Clear();
    aMVBounds.Clear();
    aTmpAllocator->Reset(false);
    ...
  }

Allocator and loop variables created inside the loop

MemoryTracker mem; // create a memory tracker that tracks the maximum RSS memory

  for (i = 0; i < aNbFF; ++i, aPS.Next())
  {
    ....

    // Temporary allocator for per-iteration collections that are re-created at each iteration.
    Handle(NCollection_IncAllocator)              aTmpAllocator = new NCollection_IncAllocator;

    TColStd_ListOfInteger                         aLSE(aTmpAllocator);
    TColStd_ListOfInteger                         aLBV(aTmpAllocator);
    TColStd_DataMapOfIntegerListOfInteger         aDMBV(100, aTmpAllocator);
    TColStd_DataMapOfIntegerReal                  aMVTol(100, aTmpAllocator);
    TColStd_MapOfInteger                          aMVOnIn(100, aTmpAllocator);
    TColStd_MapOfInteger                          aMVCommon(100, aTmpAllocator);
    TColStd_MapOfInteger                          aMVStick(100, aTmpAllocator);
    TColStd_MapOfInteger                          aMVEF(100, aTmpAllocator);
    TColStd_MapOfInteger                          aMI(100, aTmpAllocator);
    BOPDS_IndexedMapOfPaveBlock                   aMPBOnIn(100, aTmpAllocator);
    BOPDS_MapOfPaveBlock                          aMPBCommon;
    BOPDS_ListOfPaveBlock                         aLPB(aTmpAllocator);
    TColStd_MapOfInteger                          aMVBounds(100, aTmpAllocator);
    
    ...
    
    mem.update();
  }
  
  std::cout <<"Memory tracker max memory: "<< mem.maxGB() << std::endl;

Operating System

Linux

Compiler

GCC

Bitness

64-bit

OCCT Version

7.9

Additional Files

No response

Metadata

Metadata

Assignees

Labels

0. NewThe issue was created, but not updated by maintainer. Waiting for updates labels and categories1. ModelingBoolean operations, offsets, primitives, any conversion, brep builders and etc...2. BugSomething isn't working

Type

No type

Projects

Status

Todo

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions