Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1554

Memory Leak when decoding datetimes with CodecOptions: tz_aware and tzinfo

      A memory leak occurs when querying from a collection that has CodecOptions: tz_aware=True and tz_info=utc.

      Basic code that exhibits this issue looks like this:

      from datetime import timezone
      from pymongo import MongoClient
      from bson.codec_options import CodecOptions

      codec_options = CodecOptions(tz_aware=True, tzinfo=timezone.utc)
      client = MongoClient('localhost:27017')
      database = client['my_database']
      collection = database['my_collection'].with_options(codec_options)
      cursor = collection.find({})
      for data in cursor:
          pass
      cursor.close()
      del cursor
      del data

       

      When this code is run repeatedly with memory profiling, the cursor iteration uses 10MB of memory each time and never releases the memory. (the collection is about 10MB, each object has several datetimes).

      Using gc.collect() does not help.

      The memory leak goes away if tzinfo is not set:

      codec_options = CodecOptions(tz_aware=True)

       

      The default is UTC anyway, so that works as a temporary solution. If tz_info is set to bson.tz_util.utc the memory leak still happens. 

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            Crane Sergey [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: