diff --git a/be/src/olap/memtable.cpp b/be/src/olap/memtable.cpp index f8cc79b205535f..047d995bc3e3ea 100644 --- a/be/src/olap/memtable.cpp +++ b/be/src/olap/memtable.cpp @@ -144,6 +144,34 @@ void MemTable::_init_agg_functions(const vectorized::Block* block) { MemTable::~MemTable() { SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_query_thread_context.query_mem_tracker); + { + SCOPED_CONSUME_MEM_TRACKER(_mem_tracker); + g_memtable_cnt << -1; + if (_keys_type != KeysType::DUP_KEYS) { + for (auto it = _row_in_blocks.begin(); it != _row_in_blocks.end(); it++) { + if (!(*it)->has_init_agg()) { + continue; + } + // We should release agg_places here, because they are not released when a + // load is canceled. + for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { + auto function = _agg_functions[i]; + DCHECK(function != nullptr); + function->destroy((*it)->agg_places(i)); + } + } + } + std::for_each(_row_in_blocks.begin(), _row_in_blocks.end(), + std::default_delete()); + // Arena has to be destroyed after agg state, because some agg state's memory may be + // allocated in arena. + _arena.reset(); + _vec_row_comparator.reset(); + _row_in_blocks.clear(); + _agg_functions.clear(); + _input_mutable_block.clear(); + _output_mutable_block.clear(); + } if (_is_flush_success) { // If the memtable is flush success, then its memtracker's consumption should be 0 if (_mem_tracker->consumption() != 0 && config::crash_in_memory_tracker_inaccurate) { @@ -151,28 +179,6 @@ MemTable::~MemTable() { << _mem_tracker->consumption(); } } - g_memtable_cnt << -1; - if (_keys_type != KeysType::DUP_KEYS) { - for (auto it = _row_in_blocks.begin(); it != _row_in_blocks.end(); it++) { - if (!(*it)->has_init_agg()) { - continue; - } - // We should release agg_places here, because they are not released when a - // load is canceled. - for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { - auto function = _agg_functions[i]; - DCHECK(function != nullptr); - function->destroy((*it)->agg_places(i)); - } - } - } - std::for_each(_row_in_blocks.begin(), _row_in_blocks.end(), std::default_delete()); - _arena.reset(); - _vec_row_comparator.reset(); - _row_in_blocks.clear(); - _agg_functions.clear(); - _input_mutable_block.clear(); - _output_mutable_block.clear(); } int RowInBlockComparator::operator()(const RowInBlock* left, const RowInBlock* right) const { @@ -629,8 +635,6 @@ Status MemTable::_to_block(std::unique_ptr* res) { RETURN_IF_ERROR(_sort_by_cluster_keys()); } _input_mutable_block.clear(); - // After to block, all data in arena is saved in the block - _arena.reset(); *res = vectorized::Block::create_unique(_output_mutable_block.to_block()); return Status::OK(); }