So a few times when doing HTTP work on Android, I’ve run into StrictMode Violations (SMVs hereafter) for closeable resources not being closed/finalized. An example stacktrace (yanked from this bug) looks something like this:

E/StrictMode(23606): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    E/StrictMode(23606): java.lang.Throwable: Explicit termination method 'close' not called
    E/StrictMode(23606):    at dalvik.system.CloseGuard.open(CloseGuard.java:187)
    E/StrictMode(23606):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:371)
    E/StrictMode(23606):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getSession(OpenSSLSocketImpl.java:866)
    E/StrictMode(23606):    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:92)
    E/StrictMode(23606):    at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
    E/StrictMode(23606):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:164)
    E/StrictMode(23606):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    E/StrictMode(23606):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    E/StrictMode(23606):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    E/StrictMode(23606):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    E/StrictMode(23606):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    E/StrictMode(23606):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    E/StrictMode(23606):    at org.mozilla.gecko.Favicons$LoadFaviconTask.downloadFavicon(Favicons.java:288)
    E/StrictMode(23606):    at org.mozilla.gecko.Favicons$LoadFaviconTask.doInBackground(Favicons.java:350)
    E/StrictMode(23606):    at org.mozilla.gecko.Favicons$LoadFaviconTask.doInBackground(Favicons.java:227)
    E/StrictMode(23606):    at android.os.AsyncTask$2.call(AsyncTask.java:252)
    E/StrictMode(23606):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    E/StrictMode(23606):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    E/StrictMode(23606):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
    E/StrictMode(23606):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
    E/StrictMode(23606):    at java.lang.Thread.run(Thread.java:1020)

Unfortunately I can’t post exact stack traces that I’ve run into, since these issues were in the context of work.

The basic reason for many of these SMVs on Android is that Android, by default, uses HTTP keep-alive, meaning that some of the underlying resources (e.g. sockets) are kept open. I think that the strictmode violations are caused when the connection objection leaves scope, the garbage collector eventually cleans up the resources and, in so doing, finalizes some weak references and triggers the SMV. Whatever the exact underlying cause is, the proximate fix is to override Android’s default keep-alive behavior for any connections that don’t require it. This can be done by setting a request property on the connection before you connect, like so:

HttpUrlConnection conn = url.openConnection;
conn.setRequestProperty("Connection","close");