Android: Uploading number of images causing Heap size to grow big - how to solve that?
<p>I'm writing app where user can take bunch of pictures (up to 20) and upload to server. Images need to be uploaded all together.</p>

<p>Here is my logic:</p>

<ol>
<li>Take each picture, display thumb on a screen and resize picture on SD to 800x600 with 90 quality</li>
<li>Create object, populate properties (images) as Base64 string</li>
<li>Serialize object using GSON</li>
<li>Upload string</li>
</ol>

<p>While testing I was getting errors "Out of Memory" when I was processing images. I thought and this is where all StackOverflow complains is - that it's some bug with BitmapFactory. Yes, error mostly shows up while resizing image but it is NOT related to this operation.</p>

<p>While I take pictures and process them (resize, etc) - heap size stays below 7-8mb. It's just 2-3Mb more than my usual app state.</p>

<p>When I submit those images to server and GSON + Base64 encoder comes into play - than it "explodes" and I get this:</p>

<p><img src="http://i.stack.imgur.com/qgWG6.png" alt="enter image description here"></p>

<p>Well - as you see - after process completed Allocated memory get's down as expected but Heap Size stays. Now, when I take more pictures or do something with app - I start to get those out of memory errors.</p>

<p>Here is my code to upload JSON. Any suggestions on improving it or handling something like that? Maybe I can stream JSON into file and do http from file or something?</p>

<pre><code>while (!c.isAfterLast())
        {
            String data = c.getString(colObjectData);
            TrailerInspection trailerInspection = MyGsonWrapper.getMyGson().fromJson(data, TrailerInspection.class);

            //Load image data
            for (TrailerUnitInspection trailerUnitInspection : trailerInspection.UnitInspections)
            {
                for (FileContainer fileContainer : trailerUnitInspection.Images)
                {
                    fileContainer.dataFromFile(mContext);
                }
            }

            data = MyGsonWrapper.getMyGson().toJson(trailerInspection);

            MyHttpResponse response = processPOST("/trips/" + c.getString(colTripId) + "/trailerinspection", data);

            if (response.Code ==  HttpURLConnection.HTTP_OK)
            {
                processed.add(c.getString(colGId));
            }

            c.moveToNext();
        }
        c.close();
</code></pre>
