Samples/Utilities/DebugMemoryLeak.py
 
 
 
Samples/Utilities/DebugMemoryLeak.py
# Copyright 2009 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
# 
# This script demonstrate how to use object creation / deletion logging and trace for detecting memory leak.
#
# Topic: FBObjectLifeLogEnable FBObjectGetGlobalUniqueId FBObjectPrintLivings FBObjectGetLivingCount FBTraceSetLevel
#

from pyfbsdk import *
import os

gApp = FBApplication()
gSys = FBSystem()

gTestFilePath = os.path.abspath( os.path.join( gSys.ApplicationPath, "../../OpenRealitySDK/Scenes/PlasticMan.fbx" ))
gBatchCount = 5
gEnableDetailTrace = False
gNewTraceLevel = 9 #slightly above normal level to avoid noisy output.
gOriTraceLevel = FBTraceGetLevel()

print "==========================================================================="
print "== Memory Leak Detection for Batch Processing"  
print "=="
print "== If you observed steady memory usage increase, it may indicate that there"
print "== is memory leak somewhere, either inside your SDK plugin or MoBu kernel."
print "=="
print "== Test File: ",             gTestFilePath
print "== Batch iterations: ",      gBatchCount
print "== Enable Detail Trace: ",   gEnableDetailTrace
print "== Trace Level: ", gNewTraceLevel
print "==========================================================================="

gGlobalUniqueId = FBObjectGetGlobalUniqueId()
gObjectLivingCount = FBObjectGetLivingCount()

FBObjectLifeLogEnable(gEnableDetailTrace)     # Enable object creation / deltion logging
FBTraceSetLevel(gNewTraceLevel)               # Supress less important noisy trace  

def TraceMemory(pIter):
    lTraceMsg = "Iteration: " + str(pIter) + "  Livings Count: " + str(gObjectLivingCount) + "   Memory: " + str(gSys.ProcessMemory) +"\n"   
    FBTraceWithLevel(gNewTraceLevel, lTraceMsg) # output trace message with higher level     

    
for lIter in range (gBatchCount):   
    # you may want to ingore first iteration output as many singelton objects need to be initialized upon first usage.   
    TraceMemory(lIter);

    gApp.FileOpen(gTestFilePath)

    # Here you could add vaiours batch process task related logic, 
    # for example, process animation and export and etc.
    gSys.Scene.Evaluate()
    
    # Clean the scene
    gApp.FileNew()

    #Print living object with uniqueID no less than gGlobalUniqueId
    #You may see many KBrowsingNode printed out, it's normal behavior.        
    '''
    FBObjectPrintLivings(gGlobalUniqueId)   
    '''
    
    gGlobalUniqueId = FBObjectGetGlobalUniqueId()
    gObjectLivingCount = FBObjectGetLivingCount()


TraceMemory(gBatchCount)

FBObjectLifeLogEnable(False)     # Disable logging
FBTraceSetLevel(gOriTraceLevel)  # Restore trace level