Memory issue with meshes ?

Hi !

I get memory don’t going don’t when I create a Heavy mesh with python and bmesh.
When I open a fresh need Blender my memory usage is at 80mo.
I run this script 5 times :


import bpy
import bmesh
from random import random


me = bpy.data.meshes.new('myMesh') 


ob = bpy.data.objects.new('myObject', me) 


ob.show_name = True


bpy.context.scene.objects.link(ob)


bm = bmesh.new()
bm.from_mesh(me)


for i in range(200000):
    vertex1 = bm.verts.new( (random(), random(), random()) )
    vertex2 = bm.verts.new( (random(), random(), random()) )
    vertex3 = bm.verts.new( (random(), random(), random()) )




    bm.faces.new( (vertex1, vertex2, vertex3) )
    
bmesh.ops.remove_doubles(bm,verts=bm.verts,dist=0.000001)
bm.to_mesh(me)
bm.free()



After I delete all object created by the script.Because Blender keep the mesh date until you close Blender , it’s normal the memory stay high ( near 300-400Mo ). To delete this 5 meshes I run this script:


import bpy
bpy.data.meshes.remove(bpy.data.meshes['myMesh'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.001'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.002'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.003'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.004'])

Even if a do that , the memory stay high but I found the meshes data anywhere now in the blender datablock. I doing something wrong ? Just to be sure before I report a bug.

thanks

More details:

Running this to script separatly :


import bpy
import bmesh
from random import random




for i in range(5):
    me = bpy.data.meshes.new('myMesh') 


    ob = bpy.data.objects.new('myObject', me) 


    ob.show_name = True


    bpy.context.scene.objects.link(ob)


    bm = bmesh.new()
    bm.from_mesh(me)


    for i in range(200000):
        vertex1 = bm.verts.new( (random(), random(), random()) )
        vertex2 = bm.verts.new( (random(), random(), random()) )
        vertex3 = bm.verts.new( (random(), random(), random()) )




        bm.faces.new( (vertex1, vertex2, vertex3) )
        
    bmesh.ops.remove_doubles(bm,verts=bm.verts,dist=0.000001)
    bm.to_mesh(me)
    bm.free()




import bpy


bpy.context.scene.objects.unlink(bpy.data.objects['myObject'])
bpy.context.scene.objects.unlink(bpy.data.objects['myObject.001'])
bpy.context.scene.objects.unlink(bpy.data.objects['myObject.002'])
bpy.context.scene.objects.unlink(bpy.data.objects['myObject.003'])
bpy.context.scene.objects.unlink(bpy.data.objects['myObject.004'])


bpy.data.objects.remove(bpy.data.objects['myObject'])
bpy.data.objects.remove(bpy.data.objects['myObject.001'])
bpy.data.objects.remove(bpy.data.objects['myObject.002'])
bpy.data.objects.remove(bpy.data.objects['myObject.003'])
bpy.data.objects.remove(bpy.data.objects['myObject.004'])


bpy.data.meshes.remove(bpy.data.meshes['myMesh'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.001'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.002'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.003'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.004'])



Give the memory issues I see in the first post , but running it in the same script:


import bpy
import bmesh
from random import random




for i in range(5):
    me = bpy.data.meshes.new('myMesh') 


    ob = bpy.data.objects.new('myObject', me) 


    ob.show_name = True


    bpy.context.scene.objects.link(ob)


    bm = bmesh.new()
    bm.from_mesh(me)


    for i in range(200000):
        vertex1 = bm.verts.new( (random(), random(), random()) )
        vertex2 = bm.verts.new( (random(), random(), random()) )
        vertex3 = bm.verts.new( (random(), random(), random()) )




        bm.faces.new( (vertex1, vertex2, vertex3) )
        
    bmesh.ops.remove_doubles(bm,verts=bm.verts,dist=0.000001)
    bm.to_mesh(me)
    bm.free()


bpy.context.scene.objects.unlink(bpy.data.objects['myObject'])
bpy.context.scene.objects.unlink(bpy.data.objects['myObject.001'])
bpy.context.scene.objects.unlink(bpy.data.objects['myObject.002'])
bpy.context.scene.objects.unlink(bpy.data.objects['myObject.003'])
bpy.context.scene.objects.unlink(bpy.data.objects['myObject.004'])


bpy.data.objects.remove(bpy.data.objects['myObject'])
bpy.data.objects.remove(bpy.data.objects['myObject.001'])
bpy.data.objects.remove(bpy.data.objects['myObject.002'])
bpy.data.objects.remove(bpy.data.objects['myObject.003'])
bpy.data.objects.remove(bpy.data.objects['myObject.004'])


bpy.data.meshes.remove(bpy.data.meshes['myMesh'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.001'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.002'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.003'])
bpy.data.meshes.remove(bpy.data.meshes['myMesh.004'])



and the memory go back near to the beginning with the fresh new blender. It’s probably due to the Undo keeped in memory. Somebody can told me what’s it’s thinking about this and how to make script operations not stored in the undo stack ?

To test the ‘Undo is the problem’ theory try temporarily changing the user settings for undo then run the code.

For example:


bpy.context.user_preferences.edit.use_global_undo = False

# run your code here

bpy.context.user_preferences.edit.use_global_undo = True

Here is another possible use for altering user preferences by code.

I use something like this when I know I want the next set of inserted keyframes to be of linear interpolation.


            # Fetch the default key interplation type from User Settings.
            keyInterp = context.user_preferences.edit.keyframe_new_interpolation_type   
            
            # Override the User Setting with LINEAR interpolation.
            context.user_preferences.edit.keyframe_new_interpolation_type ='LINEAR'     
            
            # Run insert keyframe code here.
            
            # Restore previous user preferences.
            context.user_preferences.edit.keyframe_new_interpolation_type = keyInterp

Thanks Atom. I’m finally pretty sure it’s a undo problem. If I uncheck undo option manually and run both code separatly , it’s don’t have problem anymore. Undo keep all the .blend in memory in case you want to come back before you run the script (the second one where you delete meshes ), so everything stay in memory before you run the script.