Memory usage keeps rising on older devices using Metal

This is a bug in Xcode's diagnostic features (Metal validation and/or GPU frame capture). If you turn those off, the memory usage should be similar to when running outside of Xcode.


Here are a few observations, but I'm not sure if one of them actually causes the memory usage you're seeing:

  • In applyFilter you are creating a new ColorMix filter every frame. Additionally, inside the instance method use(image:, time:) you are creating another one on every call. That's a lot of overhead, especially since the filter loads it's kernel every time on init. It would be advisable to just create a single ColorMix filter during setup and just update its inputImage and inputTime on every frame.
  • outputImage is not a func, but a var that you override from the CIFilter super class:

    override var outputImage: CIImage? { /* your code here */ }

  • Is your colorMix kernel performing any kind of convolution? If not, it could be a CIColorKernel instead.

  • If you need the size of the input inside your kernel, you don't need to pass it as extra argument. You can just call .size() on the input sampler.