You need to use CloseHandle instead of ClearArray. All ClearArray does is set the array to 0 size, but the array object remains.
(The array object itself takes up memory too. Even if there's no script-usable data in the array)
Don't just change the ClearArray's to CloseHandle's, either. Make sure that every CreateArray is paired with a CloseHandle. the switch function does not look like it always runs.