Skip to content

Commit 5850240

Browse files
committed
Clean up pointers to middle_query_t in outputs to release memory
The outputs have a shared pointer to the middle_query_t to access the data held by the middle. This in turn holds pointers to potentially large datastructures and the flat node file. We need to release those pointers after we don't need them any more so that if and when the middle doesn't need those data structures any more they get cleaned up. In effect this releases the memory used for the cache and flat node store earlier. Fixes #1563
1 parent f8beea1 commit 5850240

4 files changed

Lines changed: 31 additions & 7 deletions

File tree

src/osm2pgsql.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@
2424
#include <memory>
2525
#include <utility>
2626

27+
/**
28+
* Output overall memory usage as debug message.
29+
*
30+
* This only works on Linux.
31+
*/
32+
static void show_memory_usage()
33+
{
34+
osmium::MemoryUsage mem;
35+
if (mem.peak() != 0) {
36+
log_debug("Overall memory usage: peak={}MByte current={}MByte",
37+
mem.peak(), mem.current());
38+
}
39+
}
40+
2741
static void run(options_t const &options)
2842
{
2943
auto const files = prepare_input_files(
@@ -55,6 +69,8 @@ static void run(options_t const &options)
5569
process_files(files, &osmdata, options.append,
5670
get_logger().show_progress());
5771

72+
show_memory_usage();
73+
5874
// Process pending ways and relations. Cluster database tables and
5975
// create indexes.
6076
osmdata.stop();
@@ -76,13 +92,7 @@ int main(int argc, char *argv[])
7692

7793
run(options);
7894

79-
// Output overall memory usage. This only works on Linux.
80-
osmium::MemoryUsage mem;
81-
if (mem.peak() != 0) {
82-
log_debug("Overall memory usage: peak={}MByte current={}MByte",
83-
mem.peak(), mem.current());
84-
}
85-
95+
show_memory_usage();
8696
log_info("osm2pgsql took {} overall.",
8797
util::human_readable_duration(timer_overall.stop()));
8898
} catch (std::exception const &e) {

src/osmdata.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ void osmdata_t::reprocess_marked() const { m_output->reprocess_marked(); }
372372

373373
void osmdata_t::postprocess_database() const
374374
{
375+
m_output->free_middle_references();
376+
375377
if (m_droptemp) {
376378
// When dropping middle tables, make sure they are gone before
377379
// indexing starts.

src/output.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ output_t::output_t(std::shared_ptr<middle_query_t> const &mid,
7171

7272
output_t::~output_t() = default;
7373

74+
void output_t::free_middle_references()
75+
{
76+
m_mid.reset();
77+
}
78+
7479
options_t const *output_t::get_options() const { return &m_options; }
7580

7681
void output_t::merge_expire_trees(output_t *) {}

src/output.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class output_t
4747
clone(std::shared_ptr<middle_query_t> const &mid,
4848
std::shared_ptr<db_copy_thread_t> const &copy_thread) const = 0;
4949

50+
/**
51+
* Remove pointer to middle_query_t from output, so the middle_query_t
52+
* is properly cleaned up and doesn't hold references to any datastructures
53+
* any more.
54+
*/
55+
void free_middle_references();
56+
5057
virtual void start() = 0;
5158
virtual void stop() = 0;
5259
virtual void sync() = 0;

0 commit comments

Comments
 (0)