I have to load lots of objects/meshes(50k) in Blender and render them for which I have written a Python script. My meshes have lots of vertices/faces (in the range of ~[10k, 70k]). I noticed that memory usage keeps increasing as despite removing them. Memory usage goes down a little after removing the object but it is still increased compared to the time before loading the object. For instance, when I load Blender memory usage is aroun 8MBs but after loading/removing 10 objects memory usage is around 200MBs (more or less). If I do not remove the 10 objects the memory usage is going to be around 300MBs or so. I wonder, does anyone have a solution for how I can properly import and remove objects/meshes so that I can prevent memory leak?

Here's a simplified version of my rendering pipeline:

do while there are still unrendered objects
    load an obj file
    render the mesh
    remove the mesh
end while

So far, I have done the followings to lessen memory foot print:

I use both Cycles and Blender internal renderer for different purposes. However, I do not know what to do in Cycles to reduce memory foot print. In Blender internal, I check the Free Image Textures:

enter image description here

And in User Preferences --> Edit I do the followings:

enter image description here

This is also how I remove the meshes/objects:

 for obj in bpy.context.scene.objects:
     if obj.type == 'MESH':
         obj.select = True
         obj.select = False

where you can check type against any of the following values according to this documentation:

  • $\begingroup$ Not sure , but Blender's garbage colletion consists of not saving unlinked datablocks. How's the situation after saving and re-opening? $\endgroup$ Mar 1 '18 at 1:32
  • $\begingroup$ @Haunt_House Things are okay after closing and re-opening. Do you mean the garbage collector does not do anything with unlinked data blocks? If that's the case then what is the solution? How can I manually remove those links or data blocks? $\endgroup$
    – Amir
    Mar 1 '18 at 2:22
  • $\begingroup$ Well, the concept has been there in times when Blender didn't have undo. Manually? Just delete objects or unlink materials. The key is to have 0 users for a gven datablock. then save and re-open and t'll be gone $\endgroup$ Mar 1 '18 at 2:37
  • $\begingroup$ @Haunt_House Sorry I meant to say that how can I remove those data blocks using Python? Do I definitely need to close and re-open Blender at the end? I'm actually a bit surprised, doesn't such a popular software have the ability to deal with this? I hope there are better solutions. $\endgroup$
    – Amir
    Mar 1 '18 at 2:42
  • 4
    $\begingroup$ Related, possibly a duplicate: Purge unused data (e.g. particle system, or groups) by script. $\endgroup$ Mar 1 '18 at 3:26

There is an easy way to get rid of memory leakage issue by resetting everything to factory setting by the following:


However, if you want to keep your current settings you can remove the unneeded data blocks manually. For instance, after you remove the meshes/objects in your scene you can execute the following code to remove all the unused blocks that were assigned to those meshes/objects:

for block in bpy.data.meshes:
    if block.users == 0:

for block in bpy.data.materials:
    if block.users == 0:

for block in bpy.data.textures:
    if block.users == 0:

for block in bpy.data.images:
    if block.users == 0:

IMPORTANT NOTE: It looks like that there is some dependencies between some data blocks such as mesh, texture, image and materials. If you do not remove the data blocks on the highest level of the hierarchy, you will not be able to remove other data blocks or you have to take the risk and remove data blocks with users more than 0. So make sure you use the code above in the following order to remove data blocks. This way you can remove all unlinked (users == 0) data blocks:

remove meshes --> remove materials --> remove textures --> remove images

The followings also have data blocks:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.