Samples/Utilities/DebugMemoryLeak.py

Samples/Utilities/DebugMemoryLeak.py
1 # Copyright 2009 Autodesk, Inc. All rights reserved.
2 # Use of this software is subject to the terms of the Autodesk license agreement
3 # provided at the time of installation or download, or which otherwise accompanies
4 # this software in either electronic or hard copy form.
5 #
6 # This script demonstrate how to use object creation / deletion logging and trace for detecting memory leak.
7 #
8 # Topic: FBObjectLifeLogEnable FBObjectGetGlobalUniqueId FBObjectPrintLivings FBObjectGetLivingCount FBTraceSetLevel
9 #
10 
11 from pyfbsdk import *
12 import os
13 
14 gApp = FBApplication()
15 gSys = FBSystem()
16 
17 gTestFilePath = os.path.abspath( os.path.join( gSys.ApplicationPath, "../../OpenRealitySDK/Scenes/PlasticMan.fbx" ))
18 gBatchCount = 5
19 gEnableDetailTrace = False
20 gNewTraceLevel = 9 #slightly above normal level to avoid noisy output.
21 gOriTraceLevel = FBTraceGetLevel()
22 
23 print "==========================================================================="
24 print "== Memory Leak Detection for Batch Processing"
25 print "=="
26 print "== If you observed steady memory usage increase, it may indicate that there"
27 print "== is memory leak somewhere, either inside your SDK plugin or MoBu kernel."
28 print "=="
29 print "== Test File: ", gTestFilePath
30 print "== Batch iterations: ", gBatchCount
31 print "== Enable Detail Trace: ", gEnableDetailTrace
32 print "== Trace Level: ", gNewTraceLevel
33 print "==========================================================================="
34 
35 gGlobalUniqueId = FBObjectGetGlobalUniqueId()
36 gObjectLivingCount = FBObjectGetLivingCount()
37 
38 FBObjectLifeLogEnable(gEnableDetailTrace) # Enable object creation / deltion logging
39 FBTraceSetLevel(gNewTraceLevel) # Supress less important noisy trace
40 
41 def TraceMemory(pIter):
42  lTraceMsg = "Iteration: " + str(pIter) + " Livings Count: " + str(gObjectLivingCount) + " Memory: " + str(gSys.ProcessMemory) +"\n"
43  FBTraceWithLevel(gNewTraceLevel, lTraceMsg) # output trace message with higher level
44 
45 
46 for lIter in range (gBatchCount):
47  # you may want to ingore first iteration output as many singelton objects need to be initialized upon first usage.
48  TraceMemory(lIter);
49 
50  gApp.FileOpen(gTestFilePath)
51 
52  # Here you could add vaiours batch process task related logic,
53  # for example, process animation and export and etc.
54  gSys.Scene.Evaluate()
55 
56  # Clean the scene
57  gApp.FileNew()
58 
59  #Print living object with uniqueID no less than gGlobalUniqueId
60  #You may see many KBrowsingNode printed out, it's normal behavior.
61  '''
62  FBObjectPrintLivings(gGlobalUniqueId)
63  '''
64 
65  gGlobalUniqueId = FBObjectGetGlobalUniqueId()
66  gObjectLivingCount = FBObjectGetLivingCount()
67 
68 
69 TraceMemory(gBatchCount)
70 
71 FBObjectLifeLogEnable(False) # Disable logging
72 FBTraceSetLevel(gOriTraceLevel) # Restore trace level