[Android Network validity detection] Networkmonitor code causes a memory leak

Source: Internet
Author: User
Tags stack trace

The log that caused the memory leak is as follows:

E STRICTMODE:A Resource was acquired on attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.

E StrictMode:java.lang.Throwable:Explicit Termination method ' end ' not called

......

E strictmode:at com.android.server.connectivity.NetworkMonitor.isCaptivePortalExt (networkmonitor.java:1026)

Networkmonitor.java The original code is as follows, according to the intent of this code, is to get the request, and then through a broadcast called Android.net.conn.NETWORK_CONDITIONS_MEASURED to send out. The key value is Extra_request_timestamp_ms and Extra_response_timestamp_ms (if any).

Why is a memory leak caused?

I found that the problem occurs when I use a weak network environment, such as the use of China Mobile GPRS internet this time will cause a longer request time, just if there are other applications (such as Google Music, etc.) also frequently verify the network connectivity, And the small storage of the machine will cause memory resource leakage.

The following changes are followed:

Get the InputStream object and then close it at the appropriate time, in fact is.close () can be put into

Httpresponsecode = Urlconnection.getresponsecode () before.

[Android Network validity detection] Networkmonitor code causes a memory leak

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.