00001 // @(#)root/memstat:$Name$:$Id: testMemStat.C 143 2008-06-17 16:15:42Z anar $ 00002 // Author: M.Ivanov -- Anar Manafov (A.Manafov@gsi.de) 28/04/2008 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 ////////////////////////////////////////////////////////////////////////// 00013 // // 00014 // An example script, which "leaks" and TMemStat is used the leaks to // 00015 // detect. // 00016 // This script is called from the "MemStat.C" script, which shows how // 00017 // to print results and call TMemStat's graphics user interface. // 00018 // // 00019 // Please use the "MemStat.C" script to start the demo. // 00020 // // 00021 ////////////////////////////////////////////////////////////////////////// 00022 00023 // ROOT 00024 #include "TSystem.h" 00025 #include "TStopwatch.h" 00026 #include "TTimeStamp.h" 00027 #include "TRandom.h" 00028 #include "TArrayF.h" 00029 #include "TArrayD.h" 00030 #include "TArrayI.h" 00031 #include "TArrayS.h" 00032 #include "TNamed.h" 00033 #include "TObjString.h" 00034 #include "TVectorD.h" 00035 // MemStat 00036 #include "TMemStat.h" 00037 00038 void funF(); 00039 void funD(); 00040 void funS(); 00041 void funI(); 00042 void funA(); 00043 void funObject(); 00044 00045 const Int_t nloop = 50000; 00046 const Int_t nskip = 100; 00047 00048 void MemStatLeak() 00049 { 00050 // Creating the TMemStat object 00051 TMemStat m("new,gnubuildin"); 00052 TStopwatch stopwatch; 00053 stopwatch.Start(); 00054 TTimeStamp stamp; 00055 // adding user's step Start 00056 m.AddStamp("Start"); 00057 funF(); 00058 // adding user's step funF 00059 m.AddStamp("funF"); 00060 funD(); 00061 m.AddStamp("funD"); 00062 funS(); 00063 m.AddStamp("funS"); 00064 funI(); 00065 m.AddStamp("funI"); 00066 funA(); 00067 m.AddStamp("funA"); 00068 funObject(); 00069 m.AddStamp("funObject"); 00070 stopwatch.Stop(); 00071 stopwatch.Print(); 00072 } 00073 00074 // the following functions are "leaking" ones... 00075 void funF() 00076 { 00077 for (Int_t i = 0; i < nloop; ++i) { 00078 TArrayF * arr = new TArrayF(100); 00079 if (i % nskip == 0) 00080 continue; 00081 delete arr; 00082 } 00083 } 00084 00085 void funD() 00086 { 00087 for (Int_t i = 0; i < nloop; ++i) { 00088 TArrayD * arr = new TArrayD(100); 00089 if (i % nskip == 0) 00090 continue; 00091 delete arr; 00092 } 00093 } 00094 00095 void funS() 00096 { 00097 for (Int_t i = 0; i < nloop; ++i) { 00098 TArrayS * arr = new TArrayS(100); 00099 if (i % nskip == 0) 00100 continue; 00101 delete arr; 00102 } 00103 } 00104 00105 void funI() 00106 { 00107 for (Int_t i = 0; i < nloop; ++i) { 00108 TArrayI * arr = new TArrayI(100); 00109 if (i % nskip == 0) 00110 continue; 00111 delete arr; 00112 } 00113 } 00114 00115 00116 void funA() 00117 { 00118 funD(); 00119 funF(); 00120 funI(); 00121 funS(); 00122 } 00123 00124 void funObject() 00125 { 00126 TObjArray arr(2000); 00127 for (Int_t i = 0; i < 2000; ++i) { 00128 TObject * o = new TNamed(Form("xxx%d", i), "leak"); 00129 if (i % nskip > 4) 00130 arr.AddAt(o, i); 00131 } 00132 for (Int_t i = 0; i < 2000; ++i) { 00133 if (arr.At(i)) 00134 delete arr.At(i); 00135 } 00136 }