Question: I got a Json, which contains among others a data field which stores a base64 encoded string.
This Json is serialized and send to a client.

On client side, the newtonsoft json.net deserializer is used to get back the Json.
However, if the data field becomes large (~ 400 MB), the deserializer will throw an out of memory exception: *Array Dimensions exceeded supported Range*.
I also see in Task-Manager, that memory consumption really grows fast.

Any ideas why this is? Is there a maximum size for json fields or something?

Code example (simplified):

```
HttpResponseMessage responseTemp = null;
responseTemp = client.PostAsJsonAsync(client.BaseAddress, message).Result;

string jsonContent = responseTemp.Content.ReadAsStringAsync.Result;
result = JsonConvert.DeserializeObject<Result>(jsonContent);

```

Result class:

```
public class Result
{

    public string Message { get; set; }
    public byte[] Data { get; set; }

}

```

**UPDATE:**

I think my problem is **not** the serializer, but just trying to handle such a huge string in memory. 
At the point where I read the string into memory, the memory consumption of the application explodes. Every operation on that string does the same. At the moment, I think I have to find a way to work with streams and stop reading the whole stuff into memory at once.

Answer: 
Huge base64 strings aren't a problem as such, .Net supports object sizes of around 2gb, see the answer [here](https://stackoverflow.com/questions/6616739/why-is-the-max-size-of-byte-2-gb-57-b). 
Of course, that doesn't mean you can store 2gb of information in an object!

However, I get the feeling that it's the byte[] that's the problem.

If there's too many elements for a byte[] to contain, it doesn't matter if you stream the result or even read it from a file on your hard drive.

So, just for testing purposes, can you trying changing the type of that from byte[] to string or even perhaps a List? 
It's not elegant or event perhaps advisable, but it might point the way to a better solution.

**Edit:**

Another test case to try, instead of calling deserializeObject, try just saving that jsonContent string to a file, and see how big it is?

Also, why do you need it in memory? What sort of data is it?
It seems to me that if you've got to process this in memory then you're going to have a bad time - the size of the object is simply too large for the CLR.

Just had a little inspiration however, what about trying a different deserializer? Perhaps RestSharp or you can use `HttpClient.ReadAsAsync<T>`. It is possible that it's NewtonSoft itself that has a problem, especially if the size of the content is around 400mb.
If you know the JSON result is an array, you can use this (based on <https://stackoverflow.com/a/24115672/989451>). It helped me with avoid out of memory exceptions. This will read and deserialize each array item separately:

```
public static IEnumerable<T> DeserializeSequenceFromJson<T>(TextReader readerStream)
{
    using (var reader = new JsonTextReader(readerStream))
    {
        var serializer = new JsonSerializer();

        // find start of array
        while (reader.TokenType != JsonToken.StartArray)
        {
            reader.Read();
        }

        // deserialize each item until end of array
        while (reader.Read())
        {
            if (reader.TokenType == JsonToken.EndArray) break;
            var item = serializer.Deserialize<T>(reader);
            yield return item;
        }
    }
}

```