Memory (RAM) leak since 4.0.0 in bpy when rendering several frames with Cycles #129384

Open
opened 2024-10-24 20:16:49 +02:00 by Julien-Philip · 10 comments

System Information
Operating system: Ubuntu 22.04.4 LTS
Graphics card: A10G (also tested on T4 and A100) - Driver Version: 535.183.01 CUDA Version: 12.2

Blender Version
Broken: bpy 4.0.0 to 4.2.0 (tested both)
Worked: bpy 3.6.0 (also tested)

Short description of error
I implemented a system to render scenes programmatically using bpy in Python.
I create or load a scene, then proceed to render the scene from different viewpoints, changing objects and or lighting.
Since version 4.0.0 of bpy I experience a very aggressive memory leak (accounting for several GB of RAM after a few renders) this eventually fill up all the available memory. I believe this leak might exist in the desktop version of blender as well and might impact animation rendering.

Exact steps for others to reproduce the error
On a linux machine
Create a conda environment:
conda create --name blender_test python=3.10
Activate the environment
conda activate blender_test
Install bpy 4.0.0 (alternatively you'd need python 3.11 with bpy 4.2.0) (or 3.6.0 to verify the working version)
pip install bpy==4.0.0
Run the attached script:
python memory_leak_repro.py

This scripts expect two environment files as .exr files that I got from there. You can use other exr files of course, just give the proper path in the script.
https://polyhaven.com/a/abandoned_church
https://polyhaven.com/a/abandoned_parking

My 2cts
The script creates a simple scene with a sphere and renders it with Cycles using Optix on the GPU.
It sets up the geometry, a camera and loads two hdris.
It then proceeds to render alternatively changing from one hdri to the other.
The line bpy.context.scene.camera = camera is here to trigger the update that happens typically when changing the camera or using a different camera. The bug is not present when commenting this line but this should not be considered a "solution" to the problem, I implemented it this way to make the file as simple as possible.
I did a lot of digging and tried many "tips" to solve this issue but could not. I believe that some memory is allocated either when loading images for cycles and/or loading geometry BVH and it is not released.

With bpy==3.6.0 the above script max out as 1.8GB RAM (as measured by the RES column of top) with an average of 1.6GB which stays stable after a few renderings. After a factory reset (at the end of the script) 1.2GB are left in the process.
With bpy==4.* the memory is increasing steadily from 1.5GB to 4+GB over the 100 renders (and keeps increasing with more renders). After the factory reset 2+GB are left in the process.

Thanks a lot!

**System Information** Operating system: Ubuntu 22.04.4 LTS Graphics card: A10G (also tested on T4 and A100) - Driver Version: 535.183.01 CUDA Version: 12.2 **Blender Version** Broken: bpy 4.0.0 to 4.2.0 (tested both) Worked: bpy 3.6.0 (also tested) **Short description of error** I implemented a system to render scenes programmatically using bpy in Python. I create or load a scene, then proceed to render the scene from different viewpoints, changing objects and or lighting. Since version 4.0.0 of bpy I experience a very aggressive memory leak (accounting for several GB of RAM after a few renders) this eventually fill up all the available memory. I believe this leak might exist in the desktop version of blender as well and might impact animation rendering. **Exact steps for others to reproduce the error** On a linux machine Create a conda environment: `conda create --name blender_test python=3.10` Activate the environment `conda activate blender_test` Install bpy 4.0.0 (alternatively you'd need python 3.11 with bpy 4.2.0) (or 3.6.0 to verify the working version) `pip install bpy==4.0.0` Run the attached script: `python memory_leak_repro.py` This scripts expect two environment files as .exr files that I got from there. You can use other exr files of course, just give the proper path in the script. https://polyhaven.com/a/abandoned_church https://polyhaven.com/a/abandoned_parking **My 2cts** The script creates a simple scene with a sphere and renders it with Cycles using Optix on the GPU. It sets up the geometry, a camera and loads two hdris. It then proceeds to render alternatively changing from one hdri to the other. The line `bpy.context.scene.camera = camera` is here to trigger the update that happens typically when changing the camera or using a different camera. The bug is not present when commenting this line but this should not be considered a "solution" to the problem, I implemented it this way to make the file as simple as possible. I did a lot of digging and tried many "tips" to solve this issue but could not. I believe that some memory is allocated either when loading images for cycles and/or loading geometry BVH and it is not released. With bpy==3.6.0 the above script max out as 1.8GB RAM (as measured by the RES column of top) with an average of 1.6GB which stays stable after a few renderings. After a factory reset (at the end of the script) 1.2GB are left in the process. With bpy==4.* the memory is increasing steadily from 1.5GB to 4+GB over the 100 renders (and keeps increasing with more renders). After the factory reset 2+GB are left in the process. Thanks a lot!
Julien-Philip added the
Status
Needs Triage
Severity
Normal
Type
Bug
labels 2024-10-24 20:16:49 +02:00
Member

I can confirm the issue on Linux, but not Windows.
I did make some modifications to the script just for my own testing:

  • I used the CPU for rendering.
  • I set the sample count to 1
  • I set the render resolution to 256x256
  • I set the for loop to 100000 iterations rather than 100 so I can go off and do something, then come back and see that the RAM usage had risen drasitcally.

I will mark this as a render and cycles module issue as it's happening during rendering, but it might be a task for a different module.

System Information
Operating system: Linux-6.8.0-47-generic-x86_64-with-glibc2.39 64 Bits, X11 UI
BPY: 4.2.0

I can confirm the issue on Linux, but not Windows. I did make some modifications to the script just for my own testing: - I used the CPU for rendering. - I set the sample count to 1 - I set the render resolution to 256x256 - I set the for loop to 100000 iterations rather than 100 so I can go off and do something, then come back and see that the RAM usage had risen drasitcally. I will mark this as a render and cycles module issue as it's happening during rendering, but it might be a task for a different module. **System Information** Operating system: Linux-6.8.0-47-generic-x86_64-with-glibc2.39 64 Bits, X11 UI BPY: 4.2.0
Alaska added
Status
Confirmed
Platform
Linux
Interest
Python API
and removed
Status
Needs Triage
labels 2024-10-25 11:25:51 +02:00
Author

Thanks a lot for looking into it, I tested on a Mac with Metal and could not reproduce it. I was about to test on Windows as well.
The issue is indeed present whether using CPU/Optix/CUDA as a backend on my side.

Thanks a lot for looking into it, I tested on a Mac with Metal and could not reproduce it. I was about to test on Windows as well. The issue is indeed present whether using CPU/Optix/CUDA as a backend on my side.
Author

A weird finding on my side (maybe you can repro) is that if I create 100 spheres:

for i in range(100):
    bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0+0.01*i,0,0))

The leak seems to have disappeared or is much less severe, as I could not see it. I can't explain this, but it might help if someone is looking into it at some point.

A weird finding on my side (maybe you can repro) is that if I create 100 spheres: ``` for i in range(100): bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0+0.01*i,0,0)) ``` The leak seems to have disappeared or is much less severe, as I could not see it. I can't explain this, but it might help if someone is looking into it at some point.
Alaska added the
Module
Render & Cycles
label 2024-11-08 09:35:50 +01:00
Author

@Alaska Do you have any idea if someone is or will look into it? I am rendering relatively big scenes from a few dozens of viewpoints each and the memory shoots to more than 100GB crashing my machine. The issue is quite severe when rendering complex scenes.

@Alaska Do you have any idea if someone is or will look into it? I am rendering relatively big scenes from a few dozens of viewpoints each and the memory shoots to more than 100GB crashing my machine. The issue is quite severe when rendering complex scenes.
Member

I will up the severity of this issue as it is a memory leak.

This will put it on the high severity board that should be checked regularly by developers.

I will up the severity of this issue as it is a memory leak. This will put it on the high severity board that should be checked regularly by developers.
Alaska added
Severity
High
and removed
Severity
Normal
labels 2024-11-28 05:20:28 +01:00
Author

Thanks, appreciate it!

Thanks, appreciate it!

@Alaska I wouldn't mind some help here (the NVidia machine I have access to is a shared one, and currently used for some other bug investigation by the team).

  • Does the issue still happen with bpy 4.3 ?
  • Does the issue happen only with GPU render?
  • Can the issue be easily reproduced from Blender side?
@Alaska I wouldn't mind some help here (the NVidia machine I have access to is a shared one, and currently used for some other bug investigation by the team). - Does the issue still happen with bpy 4.3 ? - Does the issue happen only with GPU render? - Can the issue be easily reproduced from Blender side?
Member

I will do some testing tomorrow. But I will note that the issue can be reproduced on the CPU (if my comment from a month ago is to be believed)

I will do some testing tomorrow. But I will note that the issue can be reproduced on the CPU (if my comment from a month ago is to be believed)
Member

Testing with bpy 4.3, with CPU rendering, I can still reproduce the issue on Linux. I have attached a zip file with everything I was using for testing. Just extract it, and run python memory_leak_repo.py

I then opened a second terminal window and watched the python application in top. Over the course of a few (3-4) minutes, RES memory will increase from 1.5G to 3.2G.

Interestingly once it reaches 3G it seems to plateau for a bit, but RES memory can start increasing again. After an hour or two it reached 6G for me.


I could not reproduce the issue from inside of Blender. RES memory stayed fairly consistent with the exact same script, modified for running inside of Blender.

Testing with bpy 4.3, with CPU rendering, I can still reproduce the issue on Linux. I have attached a zip file with everything I was using for testing. Just extract it, and run `python memory_leak_repo.py` I then opened a second terminal window and watched the `python` application in `top`. Over the course of a few (3-4) minutes, `RES` memory will increase from 1.5G to 3.2G. Interestingly once it reaches 3G it seems to plateau for a bit, but `RES` memory can start increasing again. After an hour or two it reached 6G for me. --- I could not reproduce the issue from inside of Blender. `RES` memory stayed fairly consistent with the exact same script, modified for running inside of Blender.
Author

I can confirm the issue is still present on 4.3 on my side as well.
It seems like the denoiser is making it worse, but the leak is present without it. The complexity of the scene being rendered affects the leak scale. With relatively large scenes (10GB VRAM when rendering) the memory can shoot up to 50+GB in matters of minutes.

I can confirm the issue is still present on 4.3 on my side as well. It seems like the denoiser is making it worse, but the leak is present without it. The complexity of the scene being rendered affects the leak scale. With relatively large scenes (10GB VRAM when rendering) the memory can shoot up to 50+GB in matters of minutes.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Asset System
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#129384
No description provided.