Skip to content

Commit 8cd83ee

Browse files
authored
Merge pull request #70 from MOLAorg/fix/rosbag2-memory-free
BUGFIX: read ahead cache memory for rosbag2 was not freed
2 parents 01bb91b + 01d8444 commit 8cd83ee

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

mola_input_rosbag2/include/mola_input_rosbag2/Rosbag2Dataset.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,11 @@ class Rosbag2Dataset : public RawDataSourceBase,
144144
/** At initialization
145145
*
146146
*/
147-
std::vector<std::optional<DatasetEntry>> read_ahead_;
148-
size_t rosbag_next_idx_ = 0;
149-
size_t rosbag_next_idx_write_ = 0;
150-
std::set<std::string> already_pub_sensor_labels_;
147+
mutable std::vector<std::optional<DatasetEntry>> read_ahead_;
148+
size_t rosbag_next_idx_ = 0;
149+
size_t rosbag_next_idx_write_ = 0;
150+
std::set<std::string> already_pub_sensor_labels_;
151+
mutable std::deque<size_t> unload_queue_; //!< read_ahead_ indices
151152

152153
// Methods and variables for the ROS->MRPT conversion
153154
// -------------------------------------------------------
@@ -200,6 +201,8 @@ class Rosbag2Dataset : public RawDataSourceBase,
200201

201202
Obs catchExceptions(const std::function<Obs()>& f);
202203

204+
void autoUnloadOldEntries() const;
205+
203206
bool findOutSensorPose(
204207
mrpt::poses::CPose3D& des, const std::string& target_frame,
205208
const std::string& source_frame,

mola_input_rosbag2/src/Rosbag2Dataset.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,6 @@ void Rosbag2Dataset::spinOnce()
531531
}
532532
}
533533

534-
// Free memory in read-ahead buffer:
535-
read_ahead_.at(rosbag_next_idx_).reset();
536-
537534
// Move on:
538535
rosbag_next_idx_++;
539536
}
@@ -575,6 +572,8 @@ void Rosbag2Dataset::doReadAhead(
575572

576573
for (size_t idx = startIdx; idx <= endIdx; idx++)
577574
{
575+
unload_queue_.push_back(idx); // mark as recently accessed
576+
578577
if (read_ahead_.at(idx).has_value()) continue; // already read:
579578

580579
// serialized data
@@ -598,6 +597,9 @@ void Rosbag2Dataset::doReadAhead(
598597
}
599598
}
600599

600+
// and also, unload() very old observations.
601+
autoUnloadOldEntries();
602+
601603
MRPT_END
602604
}
603605

@@ -984,3 +986,18 @@ Rosbag2Dataset::Obs Rosbag2Dataset::catchExceptions(
984986
return {};
985987
}
986988
}
989+
990+
void Rosbag2Dataset::autoUnloadOldEntries() const
991+
{
992+
const size_t MAX_UNLOAD_LEN = std::max<size_t>(10, 2 * read_ahead_length_);
993+
994+
// unload() very old observations.
995+
while (unload_queue_.size() > MAX_UNLOAD_LEN)
996+
{
997+
const auto idx = unload_queue_.front();
998+
unload_queue_.erase(unload_queue_.begin());
999+
1000+
// Free memory in read-ahead buffer:
1001+
read_ahead_.at(idx).reset();
1002+
}
1003+
}

0 commit comments

Comments
 (0)