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



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’]
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