[Python] Memory issue: Numpy large scale array assignment

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    I try to pre-allocate a numpy array, and then assign the value by iteration

    Buffer = np.zeros((1024, 120, 90, 3), dtype=np.uint8)
    for i in range(8192):
    print(f"{i} Memory Usage: {psutil.Process().memory_info().rss / 1e6:.2f} MB")
    state = np.random.randint(0, 2, (120, 90, 3), dtype=np.uint8)
    Buffer[i%1024][:] = state
    print(f"{i} Memory Usage: {psutil.Process().memory_info().rss / 1e6:.2f} MB")


    And during the loop, the memory usage of process will suddenly increase by 2.1MB in the certain iterations (exists in both first 1024 iterations and later), this cause the process out-of-memory.

    I have tried adding

    del state
    gc.collect()


    in the end of each iteration,

    or use

    Buffer[i%1024] = state


    for assignment, but both of them didn't work.

    Can anyone tell me what's happened, and how to fix it?

    Continue reading...

Compartilhe esta Página