🌐 AI搜索 & 代理 主页
Skip to content

BUG: Memory leak calling hash() on np.datetime64 object #29397

@kostayScr

Description

@kostayScr

Describe the issue:

Memory leak, when calling hash() on np.datetime64 instance that is non-empty. gc.collect() does not free memory, no ref counts to python objects leaks found with objgraph library, using show_growth(). Seems the leak is on the C side, possibly in

datetime_hash(PyArray_DatetimeMetaData *meta, npy_datetime dt)

EDIT: see comment below.
tracemalloc points at the hash() call.
Run the code example and observe the memory usage grow rapidly.

Reproduce the code example:

import numpy as np
#dt_obj = np.datetime64() #no leak with empty object
dt_obj = np.datetime64(10, "Y")
while True:
    hash(dt_obj) #leaks here
    print("leak")

Error message:

Python and NumPy Versions:

Python 3.13.3, numpy 2.2.6

Runtime Environment:

[{'numpy_version': '2.2.6',
  'python': '3.13.3 (tags/v3.13.3:6280bb5, Apr  8 2025, 14:47:33) [MSC v.1943 '
            '64 bit (AMD64)]',
  'uname': uname_result(system='Windows', release='10', version='10.0.17763', machine='AMD64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2'],
                      'not_found': ['AVX512F',
                                    'AVX512CD',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL']}},
 {'filepath': 'C:\\Program '
              'Files\\Python\\Python313\\Library\\bin\\libiomp5md.dll',
  'internal_api': 'openmp',
  'num_threads': 6,
  'prefix': 'libiomp',
  'user_api': 'openmp',
  'version': None},
 {'filepath': 'C:\\Program '
              'Files\\Python\\Python313\\Library\\bin\\mkl_rt.2.dll',
  'internal_api': 'mkl',
  'num_threads': 6,
  'prefix': 'mkl_rt',
  'threading_layer': 'intel',
  'user_api': 'blas',
  'version': '2025.1-Product'}]

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions