-2

I often use AsyncTask because AsyncTask is have ThreadPool,

so Even if the thread grows indefinitely, Memory is managed to avoid stress.

but, recently occur OutOfMemoryError: pthread_create (1040KB stack) failed: Try again

so, I found the suspicious part.

public static class getPubIpAddress extends AsyncTask<Void, Void ,String> {

      @Override
      protected void onPreExecute() {
         super.onPreExecute(); 
      }

      @Override
      protected String doInBackground(Void... params) {
          try {
              URL pubIp = new URL("https://myserver.co.kr/ipaddr.php");
              BufferedReader in = BufferedReader(new InputStreamReader(
                       pubIp.openStream()));
              String pub = in.readLine();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return pub;
       }
       @Override
       protected void onPostExecute(String result) {
          super.onPostExecute(result);
       }
}

this asynctask get public ip. and execute when network connect, change network.

could this asynctask cause outofmemory on android?

thanks:)

hyunwooks
  • 141
  • 3
  • 14

1 Answers1

1

I think the problem might be asynctask is running again and again . You have to check for asyncTask finished then try to reRun it. Check this post. Android, AsyncTask, check status?

Second Approach you can set a flag in at Application Level for check that if the AsyncTask is running then donot start again the AsyncTask if running if not then start your AsnckTask.

At Application level class put this varaible

public static boolean alreadyRuning = false;

and also this getter and setter

 public static boolean isRunning() {
    return alreadyRuning;
}

public static void setRunning(boolean alreadyRunning) {
    this.alreadyRuning= alreadyRunning;
}

or you can close the Buffer When done.

BufferedReader in = BufferedReader(new InputStreamReader(
            pubIp.openStream()));
    try {
            String pub = in.readLine();
    } finally {
        in.close();
    }
Sardar Khan
  • 845
  • 6
  • 16