The C# bindings in GDAL expose the method called

Gdal.SetCacheMax(int nBytes)

for setting the GDAL Raster block cache size. However, it takes in an int and that int represents the number of bytes. So, to set the memory to, let's say, 8GB I will have to provide 8 * 1024 * 1024 as the input value, which exceeds the maximum allowable int size and wraps around as a negative number.

How do I set like 75% of my RAM OR like 12 GB of RAM for CACHE_MAX value using C# bindings of GDAL?

1 Answer 1


It certainly looks like a bug in the C# wrapper source code, because the maximum value of a C# int represents a maximum of ~2.5 GB. Keep in mind it was auto-generated by a program called SWIG, which isn't perfect.

Ultimately though, the C# method is just a shallow wrapper around the SetCacheMax GDAL method:

public static void SetCacheMax(int nBytes) {
    if (GdalPINVOKE.SWIGPendingException.Pending) throw GdalPINVOKE.SWIGPendingException.Retrieve();

You can either:

  1. Short-circuit and call the method yourself directly, which will probably be painful to test and maintain; or

  2. If your code won't be distributed or doesn't depend on the NuGet distribution, go fix the C# GDAL source code directly by changing the int to a ulong -- you won't need negative values.

If you're so inclined, the next step would then be for you to submit a pull request to the package maintainer. I don't think the project is on GitHub, but I did manage to find his personal website: Tamas Szekeres.

  • 1
    Changing to ‘ulong’ May not be the right thing to do. Apparently either we need the 64bit version of the method or we need this method to behave similar to the command line. In command line, putting in a smaller value results in the value treated as MBs instead of bytes. Feb 21, 2018 at 0:06
  • Appreciate your response. Feb 21, 2018 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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