How to prevent memory leakage in Blender?

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
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:

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

This is also how I remove the meshes/objects:

 for obj in bpy.context.scene.objects:
if obj.type == 'MESH':
obj.select = True
else:
obj.select = False
bpy.ops.object.delete()


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

[‘MESH’, ‘CURVE’, ‘SURFACE’, ‘META’, ‘FONT’, ‘ARMATURE’, ‘LATTICE’, ‘EMPTY’, ‘CAMERA’, ‘LAMP’, ‘SPEAKER’]

• Not sure , but Blender's garbage colletion consists of not saving unlinked datablocks. How's the situation after saving and re-opening? – Haunt_House Mar 1 '18 at 1:32
• @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? – Amir Mar 1 '18 at 2:22
• 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 – Haunt_House Mar 1 '18 at 2:37
• @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. – Amir Mar 1 '18 at 2:42
• Related, possibly a duplicate: Purge unused data (e.g. particle system, or groups) by script. – Scott Milner 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:

bpy.ops.wm.read_factory_settings(use_empty=True)


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:
bpy.data.meshes.remove(block)

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

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

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


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:

bpy.data.curves
bpy.data.lamps
bpy.data.cameras

• – batFINGER Apr 2 '19 at 6:16