OutOfMemory error unless explicit garbage collection is called

I'm developing a multi-platform app using PCL. I need to communicate with a Web Service and all the communication is done in shared code. The problematic method is this one:

    public static async Task<BasicServerResponse> UploadCredentialAsync(
        AppInfo appInfo,
        string caseCode,
        string clientIdtype,
        CredentialType credentialType,
        byte[] imageBytes)
    {
        var request = new WIWRequest()
        {
            AppInfo = appInfo,
            CaseCode = caseCode,
            Image1Base64 = await Task.Run<string>(() => System.Convert.ToBase64String(imageBytes)),
            DocumentType = GetDocumentType(clientIdtype, credentialType)
        };

        var serverResponse = await DoPostAsync(ServerMethod.InsertDocument, request);

        request = null;
        imageBytes = null;
        GC.Collect();

        // Handle...
    }

As you can see, I'm explicitly calling GC.Collect(), which I abhor, because I was getting OOM errors no matter what I did.

The problem is that I have to upload an image, corresponding to imageBytes. First I have to convert the bytes to base64, then I have to put that string into an object (request) and then convert that object to JSON (inside DoPostAsync method). This obviously puts a strain in memory, and that's why I'm using a scope as limited as possible.

Even then, if I omit the GC.Collect() instruction I get an OOM exception after this method has returned (when I have to load a bitmap into memory), meaning none of these variables are in scope any more, and yet they aren't getting garbage collected for some reason.

Do you know what could be happening?

Answers

  • LjusnanLjusnan DEMember ✭✭✭

    If objects are IDisposable, it is a good idea to dispose them. I think disposing is better than setting to null. Not disposing unmanaged resources will lead to memory leaks. So if WIWRequest is IDisposable, try disposing it or use the using statement to automatically dispose it after use. Hope this helps.

  • WIWRequest is not IDisposable since it consists entirely on strings. If I understand correctly making it disposable would be useless, or am I wrong?

  • LjusnanLjusnan DEMember ✭✭✭

    You could make it IDisposable and write your own Dispose method. I don't know if this would help.
    You could also set Image1Base64 to null before setting request to null: request.Image1Base64 = null. Have you tried that?

Sign In or Register to comment.