nvImageCodec icon indicating copy to clipboard operation
nvImageCodec copied to clipboard

Memory leak when encoding images

Open stbnps opened this issue 1 year ago • 6 comments

Version

0.2.0

Describe the bug.

Calling the encoder, eg: image_encoder.write(paths, images, params=image_encoding_params) leaks memory, this becomes noticeable when you call it for a long time with a lot of images.

Minimum reproducible example

# Just call this a lot of times with many images
image_encoder.write(paths, images, params=image_encoding_params)

Environment details

No response

Relevant log output

No response

Other/Misc.

No response

Check for duplicates

  • [X] I have searched the open bugs/issues and have found no duplicates for this bug report

stbnps avatar Aug 01 '24 15:08 stbnps

meet same issue with nvimgcodec.Decoder(), when decode a lots of image with arange shapes

letmejoin avatar Aug 06 '24 10:08 letmejoin

@stbnps Thank you for reporting this. To better understand this issue. I'd like to ask:

  • Do you change the number of paths between your calls?
  • What format are you encoding?
  • Are you referring to CPU or GPU memory?
  • Does it leak if you use the same arguments (images and paths) over and over again in a loop?

jantonguirao avatar Aug 07 '24 11:08 jantonguirao

@jantonguirao

Do you change the number of paths between your calls?

Yes, different images and paths for every call

What format are you encoding?

.jpg

Are you referring to CPU or GPU memory?

CPU memory

Does it leak if you use the same arguments (images and paths) over and over again in a loop?

It happens regardless:


import os
from uuid import uuid4

import numpy as np
from nvidia import nvimgcodec
import psutil


process = psutil.Process(os.getpid())
image_encoder = nvimgcodec.Encoder()

fixed_paths = []
fixed_arrays = []
for j in range(100):
    fixed_paths.append(f'ims/{uuid4()}.jpg')
    fixed_arrays.append((np.random.rand(500,500,3) * 255).astype(np.uint8))


print(f'Fixed images / paths test:')
for i in range(100):
    image_encoder.write(fixed_paths, fixed_arrays)
    mem_info = process.memory_info()
    print(f'RSS: {mem_info.rss/1000000:.2f} MB')

print(f'\nVariable images / paths test:')
for i in range(100):    
    arrays = []
    paths = []
    for j in range(100):
        paths.append(f'ims/{uuid4()}.jpg')
        arrays.append((np.random.rand(500,500,3) * 255).astype(np.uint8))
    image_encoder.write(paths, arrays)

    mem_info = process.memory_info()
    print(f'RSS: {mem_info.rss/1000000:.2f} MB')

stbnps avatar Aug 08 '24 13:08 stbnps

@stbnps I can confirm that there's a memory leak in the encoder API. Thank you for spotting that. It will be fixed in the following release (0.4.0 that is, since we are just rolling out 0.3.0 as we speak).

@letmejoin I could not reproduce it with the decoder. Could you please provide a simple reproduction script like the one above?

jantonguirao avatar Aug 09 '24 07:08 jantonguirao

@stbnps I can confirm that there's a memory leak in the encoder API. Thank you for spotting that. It will be fixed in the following release (0.4.0 that is, since we are just rolling out 0.3.0 as we speak).

@letmejoin I could not reproduce it with the decoder. Could you please provide a simple reproduction script like the one above? After detailed debugging, it was found that the memory leak problem was caused by the cupyx.scipy.ndimage.rotate() module

letmejoin avatar Aug 14 '24 10:08 letmejoin

After detailed debugging, it was found that the memory leak problem was caused by the cupyx.scipy.ndimage.rotate() module

Thank you for letting us know!

jantonguirao avatar Aug 20 '24 07:08 jantonguirao