RAM Usage is Rising Continuously

Try the following two patches, one for postprocessing and the other for nvdsinfer

nvdsinfer_custom_impl_Yolo_pose/nvdsparsepose_Yolo.cpp

// Pass by reference, not by value
static std::vector<NvDsInferInstanceMaskInfo>
 nonMaximumSuppression(std::vector<NvDsInferInstanceMaskInfo>& binfo)
 {
   auto overlap1D = [](float x1min, float x1max, float x2min, float x2max) -> float {
     if (x1min > x2min) {
       std::swap(x1min, x2min);
       std::swap(x1max, x2max);
     }
     return x1max < x2min ? 0 : std::min(x1max, x2max) - x2min;
   };
 
   auto computeIoU = [&overlap1D](NvDsInferInstanceMaskInfo& bbox1, NvDsInferInstanceMaskInfo& bbox2) -> float {
     float overlapX = overlap1D(bbox1.left, bbox1.left + bbox1.width, bbox2.left, bbox2.left + bbox2.width);
     float overlapY = overlap1D(bbox1.top, bbox1.top + bbox1.height, bbox2.top, bbox2.top + bbox2.height);
     float area1 = (bbox1.width) * (bbox1.height);
     float area2 = (bbox2.width) * (bbox2.height);
     float overlap2D = overlapX * overlapY;
     float u = area1 + area2 - overlap2D;
     return u == 0 ? 0 : overlap2D / u;
   };
 
   std::stable_sort(binfo.begin(), binfo.end(), [](const NvDsInferInstanceMaskInfo& b1, const NvDsInferInstanceMaskInfo& b2) {
     return b1.detectionConfidence > b2.detectionConfidence;
   });
 
   std::vector<NvDsInferInstanceMaskInfo> out;
   for (auto i : binfo) {
     bool keep = true;
     for (auto j : out) {
       if (keep) {
         float overlap = computeIoU(i, j);
         keep = overlap <= NMS_THRESH;
       }
       else {
         break;
       }
     }
     if (keep) {
       out.push_back(i);
     } else {
        // free does not save the mask of the object
        if (i.mask) {
          delete[] i.mask;
          i.mask = nullptr;
        }
     }
   }
   return out;
 }

/opt/nvidia/deepstream/deepstream/sources/libs/nvdsinfer/nvdsinfer_context_impl_output_parsing.cpp

diff --git a/sources/libs/nvdsinfer/nvdsinfer_context_impl_output_parsing.cpp b/sources/libs/nvdsinfer/nvdsinfer_context_impl_output_parsing.cpp
index 7df1804..f2d594e 100644
--- a/sources/libs/nvdsinfer/nvdsinfer_context_impl_output_parsing.cpp
+++ b/sources/libs/nvdsinfer/nvdsinfer_context_impl_output_parsing.cpp
@@ -573,16 +573,24 @@ InstanceSegmentPostprocessor::fillUnclusteredOutput(NvDsInferDetectionOutput& ou
  * Filter out objects which have been specificed to be removed from the metadata
  * prior to clustering operation
  */
-void InstanceSegmentPostprocessor::preClusteringThreshold(
-                           NvDsInferParseDetectionParams const &detectionParams,
-                           std::vector<NvDsInferInstanceMaskInfo> &objectList)
+ void InstanceSegmentPostprocessor::preClusteringThreshold(
+    NvDsInferParseDetectionParams const &detectionParams,
+    std::vector<NvDsInferInstanceMaskInfo> &objectList)
 {
-    objectList.erase(std::remove_if(objectList.begin(), objectList.end(),
-               [detectionParams](const NvDsInferInstanceMaskInfo& obj)
-               { return (obj.classId >= detectionParams.numClassesConfigured) ||
-                        (obj.detectionConfidence <
-                        detectionParams.perClassPreclusterThreshold[obj.classId])
-                        ? true : false;}),objectList.end());
+    auto removeMask = [&detectionParams](const NvDsInferInstanceMaskInfo& obj) -> bool {
+        /* Remove objects which are not in the configured classes or have
+        * confidence less than the configured threshold */
+        if ((obj.classId >= detectionParams.numClassesConfigured) ||
+            (obj.detectionConfidence < detectionParams.perClassPreclusterThreshold[obj.classId])) {
+            if (obj.mask != nullptr) {
+                delete []obj.mask;
+            }
+            return true;
+        } else {
+            return false;
+        }
+    };
+    objectList.erase(std::remove_if(objectList.begin(), objectList.end(), removeMask), objectList.end());
 }