# EventLocator in NDSolve seemingly hogs memory in Windows, but not Mac

I have written a function (ftest in my code) which does the following:

1. Takes a vector {x,vx,vy} as input
2. Numerically solves (with high precision) a specific system of ODEs with the initial conditions {x,0,vx,vy}, where x,vx,vy were as above. I am using NDSolve with EventLocator, "Event" -> y[t], as I want to find the t in which the solution crosses the x-axis.
3. Outputs the t at which the first crossing occurs, along with {x[t],vx[t],vy[t]} at that point.

Evaluating this function many times (~100) hogs memory on a Windows machine, but not on Mac. On the Windows machine (Windows 8.1 64-bit, 8GB RAM), the RAM usage of the kernel continually climbs with each evaluation (eventually exceeding 8GB, causing my machine to run out of memory and crash), yet on Mac (also with 8GB RAM), the process remains below 0.2GB.

I believe that this problem is a result of EventLocator, as adjusting the function to run NDSolve without it (and simply output the solution at t=25) does not cause memory to bleed, and in fact memory seems to be cleared properly with each evaluation.

Does anyone know what the problem may be? Is it a known Windows-related Mathematica issue?

Here is the code below. Just monitor the memory usage with each iteration (and kill the kernel if it gets too high) - but obviously, this is run-at-your-own-risk. It should evaluate the test function 100 times, each at a different point, and print the norm-squared of the output each time.

Clear["Global*"]
repetitions = 100;
ftest[ic_] := Module[{subSol, zeros, sol, solx, soly, solvx, solvy, T},
subSol = Reap[NDSolve[{x'[t] == vx[t], y'[t] == vy[t],
vx'[t] == -((x[t] - 1)/Sqrt[(x[t] - 1)^2 + y[t]^2]) - (
x[t] + 1)/Sqrt[(x[t] + 1)^2 + y[t]^2],
vy'[t] == -(y[t]/Sqrt[(x[t] - 1)^2 + y[t]^2]) - y[t]/
Sqrt[(x[t] + 1)^2 + y[t]^2], x[0] == ic[[1]], y[0] == 0,
vx[0] == ic[[2]], vy[0] == ic[[3]]}, {x, y, vx, vy}, {t, 0, 50},
Method -> {"EventLocator", "Event" -> y[t],
"EventAction" :> Sow[t]}, AccuracyGoal -> 25,
PrecisionGoal -> 25, WorkingPrecision -> 30,
InterpolationOrder -> All, MaxSteps -> 100000]];
zeros = subSol[[2]][[1]];
solx = x /. subSol[[1]][[1]];
soly = y /. subSol[[1]][[1]];
solvx = vx /. subSol[[1]][[1]];
solvy = vy /. subSol[[1]][[1]];

T = zeros[[1]];
{T, {solx[T], solvx[T], solvy[T]}}]

normftest[x_?NumberQ, vx_?NumberQ, vy_?NumberQ] :=
Norm[ftest[{x, vx, vy}][[2]]]^2;
For[m = 1, m <= repetitions, m++,
Print[normftest[1/3 + m/1000, 1/4 - m/2000, 1/5 + m/3000]]
]
`
• Encountering a similar problem, would like to know what's going on too ;) – Eriek Apr 29 '16 at 2:22
• Erik, it's best to report this problem to WRI. – user21 Jun 28 '16 at 17:18