VideoTexture module repeated loading of images causes memory leak #43033

Closed
opened 2014-12-27 01:03:54 +01:00 by Joshua Dolan · 18 comments

System Information
Mac OS X 10.10.1
MacBook Pro 15in Early 2011 2.2 GHz Core i7 16GB Ram, Intel HD3000 / Radeon 6750M

Blender Version
Broken: 2.27b hash: 9e963ae 2014-10-21 11:38
Worked: 2.69.0 r60991 Mac 64bit

Short description of error
Repeatedly loading images from disk using the VideoTexture module will cause the ram usage of Blender to constantly increase while running the scene under the BGE. This leads eventually to the BGE hanging while running requiring a force quit.

Exact steps for others to reproduce the error

  1. Open Blender 2.72b
  2. Load the attached .blend file.
  3. Run the BGE by pressing P (The scene will automatically start cycling through textures.)
  4. Observe the blender process memory usage in a system monitoring application. (Activity Monitor on OS X)

Included in the folder is a screen cap showing the memory use while running. I did not observe this in 2.69.0 with over 1,000 repeated loads of images.Monkey_VT_Bug_DelTex.zip

**System Information** Mac OS X 10.10.1 MacBook Pro 15in Early 2011 2.2 GHz Core i7 16GB Ram, Intel HD3000 / Radeon 6750M **Blender Version** Broken: 2.27b hash: 9e963ae 2014-10-21 11:38 Worked: 2.69.0 r60991 Mac 64bit **Short description of error** Repeatedly loading images from disk using the VideoTexture module will cause the ram usage of Blender to constantly increase while running the scene under the BGE. This leads eventually to the BGE hanging while running requiring a force quit. **Exact steps for others to reproduce the error** 1. Open Blender 2.72b 2. Load the attached .blend file. 3. Run the BGE by pressing P (The scene will automatically start cycling through textures.) 4. Observe the blender process memory usage in a system monitoring application. (Activity Monitor on OS X) Included in the folder is a screen cap showing the memory use while running. I did not observe this in 2.69.0 with over 1,000 repeated loads of images.[Monkey_VT_Bug_DelTex.zip](https://archive.blender.org/developer/F133758/Monkey_VT_Bug_DelTex.zip)
Author

Changed status to: 'Open'

Changed status to: 'Open'
Author

Added subscriber: @jdolan

Added subscriber: @jdolan

Added subscriber: @raco

Added subscriber: @raco

I can confirm the bug.

Nothing seems to help. Tried Texture.close().

Tried also this:

    if not hasattr(logic, "texture"):
        mat_id = texture.materialID(obj, "IMDetails000.png")
        logic.texture = texture.Texture(obj, mat_id)
        logic.texture.source = texture.ImageFFmpeg(img_name)
    else:
        logic.texture.source.reload(img_name)
    logic.texture.refresh(True)

So even just reloading will cause the memory leak.

I can confirm the bug. Nothing seems to help. Tried Texture.close(). Tried also this: ``` if not hasattr(logic, "texture"): mat_id = texture.materialID(obj, "IMDetails000.png") logic.texture = texture.Texture(obj, mat_id) logic.texture.source = texture.ImageFFmpeg(img_name) else: logic.texture.source.reload(img_name) logic.texture.refresh(True) ``` So even just reloading will cause the memory leak.
Author

Still present in 2.74rc and release.

Still present in 2.74rc and release.
Benoit Bolsee was assigned by Jorge Bernal 2015-04-01 08:41:49 +02:00
Member

Added subscriber: @JorgeBernalMartinez

Added subscriber: @JorgeBernalMartinez
Member

Benoit,

As you are touching again VideoTexture module, could you investigate this bug?

Benoit, As you are touching again VideoTexture module, could you investigate this bug?
Author

Update: While investigating this a bit more it seems that it is the .png images that are causing the problem. Exporting the same images as .jpg allows the textures to be swapped continuously without any issues. (I stopped the loop at 3,500 where as with the original .png files it will crash between 250 and 254.) I will continue to test with other image types that FFMpeg supports.

Edit:
Tested the following formats with these results yesterday -
Image Type, Number of loads, Result, Number of tests
jpeg, 3,500, no crash, 5
jpeg 2k, ~318, crash, 3
png, ~250, crash, 20+
tiff lzw, ~300, crash, 2
tiff, ~258, crash, 2
tga rle, 1,000+, no crash, 2

Update: While investigating this a bit more it seems that it is the .png images that are causing the problem. Exporting the same images as .jpg allows the textures to be swapped continuously without any issues. (I stopped the loop at 3,500 where as with the original .png files it will crash between 250 and 254.) I will continue to test with other image types that FFMpeg supports. Edit: Tested the following formats with these results yesterday - Image Type, Number of loads, Result, Number of tests jpeg, 3,500, no crash, 5 jpeg 2k, ~318, crash, 3 png, ~250, crash, 20+ tiff lzw, ~300, crash, 2 tiff, ~258, crash, 2 tga rle, 1,000+, no crash, 2
Member

Added subscriber: @Blendify

Added subscriber: @Blendify
Member

I think that #43033 can be merged seems like the same issue

I think that #43033 can be merged seems like the same issue

Added subscriber: @you.le

Added subscriber: @you.le

Hi! The commit behind the bug is here: https://developer.blender.org/diffusion/B/change/master/source/gameengine/VideoTexture/VideoFFmpeg.cpp%3Bff08acc556d8e40d501522efcbecac68bb8973c6

I can try to look for a solution but I'm not sure to succeed.

Hi! The commit behind the bug is here: https://developer.blender.org/diffusion/B/change/master/source/gameengine/VideoTexture/VideoFFmpeg.cpp%3Bff08acc556d8e40d501522efcbecac68bb8973c6 I can try to look for a solution but I'm not sure to succeed.
Member

I haven't check it but perhaps:
thread_count = 1 --> stopCache() + m_isThreaded = false;

I haven't check it but perhaps: thread_count = 1 --> stopCache() + m_isThreaded = false;

Hi! I tested your solution but it doesn't work. The problem seems related only to PNG decoding. You can delete this lines:

foreach ($list as $item) {
if (m_isImage)
            {
                *If we're an image, we're probably not going to be here often,* so we don't want to deal with delayed frames from threading.
                *There might be a better way to handle this, but I'll leave that* for people more knowledgeable with ffmpeg than myself. We don't
                // need threading for a single image anyways.
                m_codecCtx->thread_count = 1;
            }
  work_miracles($item);
}

and if you replace png images with jpg, it will work without problem. The memory leak is due to threading but I had a look at AVCodecContext (m_codecCtx type = AVCodecContext) API and found no simple way to end the threads. I read somewhere that new ffmpeg libraries have a better support of PNG but I think it's complex to update libraries (how many files to modify)? Another (maybe stupid) idea would be to convert png images in another format before decoding (I guess it will take some time). But threading with an external API is too complex to manage in my opinion. So I'll try the idea to convert png before decoding (but I don't know if it's possible and efficient). I'm not experimented so you may have better ideas :) Good day/night! A link I found on the net: https://trac.ffmpeg.org/ticket/4404

Hi! I tested your solution but it doesn't work. The problem seems related only to PNG decoding. You can delete this lines: ``` foreach ($list as $item) { if (m_isImage) { *If we're an image, we're probably not going to be here often,* so we don't want to deal with delayed frames from threading. *There might be a better way to handle this, but I'll leave that* for people more knowledgeable with ffmpeg than myself. We don't // need threading for a single image anyways. m_codecCtx->thread_count = 1; } work_miracles($item); } ``` and if you replace png images with jpg, it will work without problem. The memory leak is due to threading but I had a look at AVCodecContext (m_codecCtx type = AVCodecContext) API and found no simple way to end the threads. I read somewhere that new ffmpeg libraries have a better support of PNG but I think it's complex to update libraries (how many files to modify)? Another (maybe stupid) idea would be to convert png images in another format before decoding (I guess it will take some time). But threading with an external API is too complex to manage in my opinion. So I'll try the idea to convert png before decoding (but I don't know if it's possible and efficient). I'm not experimented so you may have better ideas :) Good day/night! A link I found on the net: https://trac.ffmpeg.org/ticket/4404

Hi! I propose this fix: http://www.pasteall.org/59457/diff

The problem is that the new while loop can turn in an infinite while loop if tha data can't definitely be read. But we can make a counter m_counter = 0; m_counter+=1 (at the end of the while loop), change while ((input->data- [x]==0 && input->data- [x]==0 && input->data- [x]==0 && input->data- [x]==0) || m_counter < 10). And after the while loop, m_counter = 0; (or something like that)

Sorry about my poor english. Good day/night!

Hi! I propose this fix: http://www.pasteall.org/59457/diff The problem is that the new while loop can turn in an infinite while loop if tha data can't definitely be read. But we can make a counter m_counter = 0; m_counter+=1 (at the end of the while loop), change while ((input->data- [x]==0 && input->data- [x]==0 && input->data- [x]==0 && input->data- [x]==0) || m_counter < 10). And after the while loop, m_counter = 0; (or something like that) Sorry about my poor english. Good day/night!

http://www.pasteall.org/59472/diff (That's what I thought about...). From what I have read about png decoding with certain ffmpeg versions, to properly decode png you can use the codecContext->thread_count = 1 workaround but in Blender it can cause memory leak, Or you can try to decode png several time until this is finally decoded properly. That's the solution I used. It's not elegant but it seems that works. What do you think about it? I'll ask IRC to have other devs opinion about that.

http://www.pasteall.org/59472/diff (That's what I thought about...). From what I have read about png decoding with certain ffmpeg versions, to properly decode png you can use the codecContext->thread_count = 1 workaround but in Blender it can cause memory leak, Or you can try to decode png several time until this is finally decoded properly. That's the solution I used. It's not elegant but it seems that works. What do you think about it? I'll ask IRC to have other devs opinion about that.

This issue was referenced by e443dd4d97

This issue was referenced by e443dd4d9768394a61197dc187eb21c65aad7941

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
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, Assets & 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
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
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
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
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
7 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#43033
No description provided.