1
$\begingroup$

I have some funtion:

GetOptimalWarpingPathMod[cumulativeCostMatrix_List, a_Integer, 
   b_Integer] := Module[{optimalPath, j, i, min, argMin},
   {i, j} = {a, b};
   optimalPath = Reap@While[(i >= 3) && (j >= 2),
      min = 
       Min[cumulativeCostMatrix[[i, j - 1]], 
        cumulativeCostMatrix[[i - 1, j - 1]], 
        cumulativeCostMatrix[[i - 2, j - 1]]];
      Switch[min,
       cumulativeCostMatrix[[i, j - 1]], argMin = {i, j - 1}; j--,
       cumulativeCostMatrix[[i - 1, j - 1]], argMin = {i - 1, j - 1}; 
       i--; j--,
       cumulativeCostMatrix[[i - 2, j - 1]], argMin = {i - 2, j - 1}; 
       i -= 2; j--,
       _, Throw["Error in GetOptimalWarpingPathNewMod"]
       ];
      Sow@argMin;
      ];
   optimalPath = 
    If[Length[optimalPath[[2]]] == 0, {}, optimalPath[[2, 1]]];
   PrependTo[optimalPath, {a, b}];
   optimalPath = Reverse@optimalPath;
   optimalPath
   ];

When I call it with random argument this leads to memory leaks:

$HistoryLength = 1;
someMatrix = RandomReal[{0, 20}, {10, 5}];
    memoryBefore = MemoryInUse[];
    Do[
      GetOptimalWarpingPathMod[someMatrix , Length[someMatrix ], 
       Ordering[someMatrix [[-1]]][[1]]],
      {i, 1, 10}];
    MemoryInUse[] - memoryBefore
    (*17752*)

But calling this function with predefined matrix all work fine

savedMatrix={{3.3004394437748807`,10.881946255573773`,4.362690908144572`,7.407440543303238`,7.345486257827773`},{14.508362563589763`,0.7356685771730973`,8.779141880113663`,14.969579494157415`,8.685346276624468`},{14.917882497227396`,17.430051988389053`,10.513304852635628`,16.791683365979523`,12.646147419315149`},{1.0588495204942632`,2.2005278583930767`,16.75277676811745`,4.0963643230729865`,13.369846181008235`},{14.413222169933519`,12.715945901646649`,12.488814903450923`,0.5015257662166803`,18.833930414984806`},{10.97588310006406`,8.811907048532749`,13.721241067651995`,17.829727310320855`,2.9585195950653898`},{1.85685418882683`,13.50702995493117`,16.501883534317372`,12.896932254223422`,14.724301159710564`},{8.246316747888361`,8.819447634430265`,11.769957506935448`,6.171909927482631`,12.075720584292299`},{2.3538532552195015`,18.372378238597122`,1.767319259063889`,8.244194544322362`,1.239398836296214`},{10.33557094657684`,11.650188299063093`,4.182059630971807`,17.839911646054595`,8.060657545219925`}};
memoryBefore = MemoryInUse[];

Do[
  GetOptimalWarpingPathMod[savedMatrix, Length[savedMatrix], 
   Ordering[savedMatrix[[-1]]][[1]]],
  {i, 1, 10}];
MemoryInUse[] - memoryBefore
(*8*)

What difference in that function calling?

EDIT

Of cource this function do nothing. But there the same problem

GetOptimalWarpingPathSimple[cumulativeCostMatrix_List, a_Integer, 
   b_Integer] := Module[{optimalPath, j, i, min, argMin},
   {i, j} = {a, b};
   While[(i >= 3) && (j >= 2),
    min = 
     Min[cumulativeCostMatrix[[i, j - 1]], 
      cumulativeCostMatrix[[i - 1, j - 1]], 
      cumulativeCostMatrix[[i - 2, j - 1]]];
    Switch[min,
     cumulativeCostMatrix[[i, j - 1]], argMin = {i, j - 1},
     cumulativeCostMatrix[[i - 1, j - 1]], argMin = {i - 1, j - 1},
     cumulativeCostMatrix[[i - 2, j - 1]], argMin = {i - 2, j - 1}
     ];
    RandomChoice[{j--, {i--; j--}, {i -= 2; j--}}];
    ];
   ];

PS: probably problem in Switch fucntion becouse when I comment it the is no memory leaks

$\endgroup$
  • 2
    $\begingroup$ Can you perhaps pinpoint the problematic code somewhat more? Otherwise, this will remain a very localized question. $\endgroup$ – Yves Klett Aug 20 '14 at 11:34
  • $\begingroup$ @YvesKlett here edited function $\endgroup$ – molekyla777 Aug 20 '14 at 13:05
1
$\begingroup$

I don't know why, but replacing Switch with nested If solved the problem of leaking memory.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.