title: does clickhouse have a memory leak issue?
body: <p>I set up a clickhouse server on a proxmox container with CentOS 7, and clickhouse version is 20.
  I give the container 60G of RAM and made the clickhouse setting of limit memory to 30G.
  I used a python's clickhouse_sqlalchemy to connect to it on another machine. When I start to give it some hard query, I notice it started to throw some memory exceptions.</p>
  <p>Below I ran an aggregation query couple times. It works fine first 6 times and then it started to throw memory exception. It seems to me that the clickhouse server does not release the memory after it execute my query:</p>
  <pre class="lang-py prettyprint-override"><code>from sqlalchemy import create_engine, Column, MetaData, literal
  from clickhouse_sqlalchemy import Table, make_session, get_declarative_base, types, engines
  import pandas as pd
  import numpy as np
  
  uri = 'clickhouse://default@192.168.10.9/'
  engine = create_engine(uri)
  metadata = MetaData()
  
  sql = &quot;&quot;&quot;
  SELECT toYYYYMMDD(trade_datetime) AS trade_dt, s_info_windcode, SUM(volume) AS volume FROM minute_bar
  WHERE toInt64(toYYYYMMDDhhmmss(trade_datetime)) - toInt64(toYYYYMMDD(trade_datetime)) * 1000000 &gt; 94500
  AND toInt64(toYYYYMMDDhhmmss(trade_datetime)) - toInt64(toYYYYMMDD(trade_datetime)) * 1000000 &lt; 141500
  GROUP BY toYYYYMMDD(trade_datetime), s_info_windcode&quot;&quot;&quot;
  
  for i in range(10):
      res = engine.execute(sql)
      data = res.fetchall()
      print(i, data[0])
  </code></pre>
  <p>below are the outputs:</p>
  <pre class="lang-sh prettyprint-override"><code>0 ('20191025', '603915.SH', '2157700')
  1 ('20130208', '002059.SZ', '1159964')
  2 ('20191025', '603915.SH', '2157700')
  3 ('20130208', '002059.SZ', '1159964')
  4 ('20130208', '002059.SZ', '1159964')
  5 ('20191025', '603915.SH', '2157700')
  ---------------------------------------------------------------------------
  DatabaseException                         Traceback (most recent call last)
  &lt;ipython-input-2-d1cd821b006b&gt; in &lt;module&gt;
        7 
        8 for i in range(10):
  ----&gt; 9     res = engine.execute(sql)
       10     data = res.fetchall()
       11     print(i, data[0])
  
  /opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py in execute(self, statement, *multiparams, **params)
     2242 
     2243         connection = self._contextual_connect(close_with_result=True)
  -&gt; 2244         return connection.execute(statement, *multiparams, **params)
     2245 
     2246     def scalar(self, statement, *multiparams, **params):
  
  /opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py in execute(self, object_, *multiparams, **params)
     1010         &quot;&quot;&quot;
     1011         if isinstance(object_, util.string_types[0]):
  -&gt; 1012             return self._execute_text(object_, multiparams, params)
     1013         try:
     1014             meth = object_._execute_on_connection
  
  /opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py in _execute_text(self, statement, multiparams, params)
     1185             parameters,
     1186             statement,
  -&gt; 1187             parameters,
     1188         )
     1189         if self._has_events or self.engine._has_events:
  
  /opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
     1322         except BaseException as e:
     1323             self._handle_dbapi_exception(
  -&gt; 1324                 e, statement, parameters, cursor, context
     1325             )
     1326 
  
  /opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py in _handle_dbapi_exception(self, e, statement, parameters, cursor, context)
     1519                 )
     1520             else:
  -&gt; 1521                 util.raise_(exc_info[1], with_traceback=exc_info[2])
     1522 
     1523         finally:
  
  /opt/conda/lib/python3.7/site-packages/sqlalchemy/util/compat.py in raise_(***failed resolving arguments***)
      176 
      177         try:
  --&gt; 178             raise exception
      179         finally:
      180             # credit to
  
  /opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
     1282                 if not evt_handled:
     1283                     self.dialect.do_execute(
  -&gt; 1284                         cursor, statement, parameters, context
     1285                     )
     1286 
  
  /opt/conda/lib/python3.7/site-packages/clickhouse_sqlalchemy/drivers/base.py in do_execute(self, cursor, statement, parameters, context)
      874 
      875     def do_execute(self, cursor, statement, parameters, context=None):
  --&gt; 876         cursor.execute(statement, parameters, context=context)
      877 
      878     def _check_unicode_returns(self, connection, additional_tests=None):
  
  /opt/conda/lib/python3.7/site-packages/clickhouse_sqlalchemy/drivers/http/connector.py in execute(self, operation, parameters, context)
      115         response_gen = transport.execute(raw_sql, params=params)
      116 
  --&gt; 117         self._process_response(response_gen)
      118         self._end_query()
      119 
  
  /opt/conda/lib/python3.7/site-packages/clickhouse_sqlalchemy/drivers/http/connector.py in _process_response(self, response)
      214         response = iter(response)
      215 
  --&gt; 216         self._columns = next(response, None)
      217         self._types = next(response, None)
      218         self._response = response
  
  /opt/conda/lib/python3.7/site-packages/clickhouse_sqlalchemy/drivers/http/transport.py in execute(self, query, params)
       89         there is nothing to return.
       90         &quot;&quot;&quot;
  ---&gt; 91         r = self._send(query, params=params, stream=True)
       92         lines = r.iter_lines()
       93         try:
  
  /opt/conda/lib/python3.7/site-packages/clickhouse_sqlalchemy/drivers/http/transport.py in _send(self, data, params, stream)
      135             orig = HTTPException(r.text)
      136             orig.code = r.status_code
  --&gt; 137             raise DatabaseException(orig)
      138         return r
  
  DatabaseException: Orig exception: Code: 241, e.displayText() = DB::Exception: Memory limit (total) exceeded: would use 52.74 GiB (attempt to allocate chunk of 4718592 bytes), maximum: 52.73 GiB: While executing AggregatingTransform (version 20.4.5.36 (official build))
  </code></pre>
  <p>This seems like a memory leak issue, is that true?</p>
  <p>UPDATED:
  clickhouse log:</p>
  <pre><code>0. Poco::Exception::Exception(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, int) @ 0x103fa390 in /usr/bin/clickhouse
  1. DB::Exception::Exception(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, int) @ 0x900b8ad in /usr/bin/clickhouse
  2. ? @ 0x900af73 in /usr/bin/clickhouse
  3. MemoryTracker::alloc(long) @ 0x90095e7 in /usr/bin/clickhouse
  4. MemoryTracker::alloc(long) @ 0x90095e7 in /usr/bin/clickhouse
  5. MemoryTracker::alloc(long) @ 0x90095e7 in /usr/bin/clickhouse
  6. void DB::PODArrayBase&lt;8ul, 4096ul, Allocator&lt;false, false&gt;, 15ul, 16ul&gt;::resize&lt;&gt;(unsigned long) @ 0x927b568 in /usr/bin/clickhouse
  7. ? @ 0x99572db in /usr/bin/clickhouse
  8. ? @ 0x995efa1 in /usr/bin/clickhouse
  9. DB::FunctionBinaryArithmetic&lt;DB::MultiplyImpl, DB::NameMultiply, true&gt;::executeImpl(DB::Block&amp;, std::__1::vector&lt;unsigned long, std::__1::allocator&lt;unsigned long&gt; &gt; const&amp;, unsigned long, unsigned long) @ 0x9960f01 in /usr/bin/clickhouse
  10. DB::ExecutableFunctionAdaptor::execute(DB::Block&amp;, std::__1::vector&lt;unsigned long, std::__1::allocator&lt;unsigned long&gt; &gt; const&amp;, unsigned long, unsigned long, bool) @ 0x9265781 in /usr/bin/clickhouse
  11. DB::ExpressionAction::execute(DB::Block&amp;, bool, std::__1::shared_ptr&lt;DB::ExtraBlock&gt;&amp;) const @ 0xd1c2452 in /usr/bin/clickhouse
  12. DB::ExpressionActions::execute(DB::Block&amp;, bool) const @ 0xd1c73f0 in /usr/bin/clickhouse
  13. DB::MergeTreeRangeReader::executePrewhereActionsAndFilterColumns(DB::MergeTreeRangeReader::ReadResult&amp;) @ 0xd9b5787 in /usr/bin/clickhouse
  14. DB::MergeTreeRangeReader::read(unsigned long, std::__1::deque&lt;DB::MarkRange, std::__1::allocator&lt;DB::MarkRange&gt; &gt;&amp;) @ 0xd9b6344 in /usr/bin/clickhouse
  15. DB::MergeTreeRangeReader::read(unsigned long, std::__1::deque&lt;DB::MarkRange, std::__1::allocator&lt;DB::MarkRange&gt; &gt;&amp;) @ 0xd9b6057 in /usr/bin/clickhouse
  16. DB::MergeTreeBaseSelectProcessor::readFromPartImpl() @ 0xd9af83d in /usr/bin/clickhouse
  17. DB::MergeTreeBaseSelectProcessor::generate() @ 0xd9b0417 in /usr/bin/clickhouse
  18. DB::ISource::work() @ 0xdad2fab in /usr/bin/clickhouse
  19. DB::SourceWithProgress::work() @ 0xdd18887 in /usr/bin/clickhouse
  20. ? @ 0xdafe041 in /usr/bin/clickhouse
  21. DB::PipelineExecutor::executeSingleThread(unsigned long, unsigned long) @ 0xdb01ffd in /usr/bin/clickhouse
  22. ? @ 0xdb029c4 in /usr/bin/clickhouse
  23. ThreadPoolImpl&lt;std::__1::thread&gt;::worker(std::__1::__list_iterator&lt;std::__1::thread, void*&gt;) @ 0x9031267 in /usr/bin/clickhouse
  24. ? @ 0x902f753 in /usr/bin/clickhouse
  25. start_thread @ 0x7e65 in /usr/lib64/libpthread-2.17.so
  26. clone @ 0xfe88d in /usr/lib64/libc-2.17.so
  
  2020.06.21 08:14:58.132139 [ 481 ] {} &lt;Error&gt; DynamicQueryHandler: Code: 241, e.displayText() = DB::Exception: Memory limit (total) exceeded: would use 52.74 GiB (attempt to allocate chunk of 4196504 bytes), maximum: 52.73 GiB: While executing MergeTreeThread, Stack trace (when copying this message, always include the lines below):
  
  0. Poco::Exception::Exception(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, int) @ 0x103fa390 in /usr/bin/clickhouse
  1. DB::Exception::Exception(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; const&amp;, int) @ 0x900b8ad in /usr/bin/clickhouse
  2. ? @ 0x900af73 in /usr/bin/clickhouse
  3. MemoryTracker::alloc(long) @ 0x90095e7 in /usr/bin/clickhouse
  4. MemoryTracker::alloc(long) @ 0x90095e7 in /usr/bin/clickhouse
  5. MemoryTracker::alloc(long) @ 0x90095e7 in /usr/bin/clickhouse
  6. void DB::PODArrayBase&lt;8ul, 4096ul, Allocator&lt;false, false&gt;, 15ul, 16ul&gt;::resize&lt;&gt;(unsigned long) @ 0x927b568 in /usr/bin/clickhouse
  7. ? @ 0x99572db in /usr/bin/clickhouse
  8. ? @ 0x995efa1 in /usr/bin/clickhouse
  9. DB::FunctionBinaryArithmetic&lt;DB::MultiplyImpl, DB::NameMultiply, true&gt;::executeImpl(DB::Block&amp;, std::__1::vector&lt;unsigned long, std::__1::allocator&lt;unsigned long&gt; &gt; const&amp;, unsigned long, unsigned long) @ 0x9960f01 in /usr/bin/clickhouse
  10. DB::ExecutableFunctionAdaptor::execute(DB::Block&amp;, std::__1::vector&lt;unsigned long, std::__1::allocator&lt;unsigned long&gt; &gt; const&amp;, unsigned long, unsigned long, bool) @ 0x9265781 in /usr/bin/clickhouse
  11. DB::ExpressionAction::execute(DB::Block&amp;, bool, std::__1::shared_ptr&lt;DB::ExtraBlock&gt;&amp;) const @ 0xd1c2452 in /usr/bin/clickhouse
  12. DB::ExpressionActions::execute(DB::Block&amp;, bool) const @ 0xd1c73f0 in /usr/bin/clickhouse
  13. DB::MergeTreeRangeReader::executePrewhereActionsAndFilterColumns(DB::MergeTreeRangeReader::ReadResult&amp;) @ 0xd9b5787 in /usr/bin/clickhouse
  14. DB::MergeTreeRangeReader::read(unsigned long, std::__1::deque&lt;DB::MarkRange, std::__1::allocator&lt;DB::MarkRange&gt; &gt;&amp;) @ 0xd9b6344 in /usr/bin/clickhouse
  15. DB::MergeTreeRangeReader::read(unsigned long, std::__1::deque&lt;DB::MarkRange, std::__1::allocator&lt;DB::MarkRange&gt; &gt;&amp;) @ 0xd9b6057 in /usr/bin/clickhouse
  16. DB::MergeTreeBaseSelectProcessor::readFromPartImpl() @ 0xd9af83d in /usr/bin/clickhouse
  17. DB::MergeTreeBaseSelectProcessor::generate() @ 0xd9b0417 in /usr/bin/clickhouse
  18. DB::ISource::work() @ 0xdad2fab in /usr/bin/clickhouse
  19. DB::SourceWithProgress::work() @ 0xdd18887 in /usr/bin/clickhouse
  20. ? @ 0xdafe041 in /usr/bin/clickhouse
  21. DB::PipelineExecutor::executeSingleThread(unsigned long, unsigned long) @ 0xdb01ffd in /usr/bin/clickhouse
  22. ? @ 0xdb029c4 in /usr/bin/clickhouse
  23. ThreadPoolImpl&lt;std::__1::thread&gt;::worker(std::__1::__list_iterator&lt;std::__1::thread, void*&gt;) @ 0x9031267 in /usr/bin/clickhouse
  24. ? @ 0x902f753 in /usr/bin/clickhouse
  25. start_thread @ 0x7e65 in /usr/lib64/libpthread-2.17.so
  26. clone @ 0xfe88d in /usr/lib64/libc-2.17.so
   (version 20.4.5.36 (official build))
  </code></pre>