简体   繁体   中英

Android activity destroyed but not garbage collected?

I have a class named Util where I keep all my useful methods (download images, scaling rotating bitmaps, downloading music, playing music, etc...)

public class Util{
   public static Bitmap downloadImage(Context context, String url) throws SocketException {
      // Implementation
   }

   public static Bitmap rotateBitmap(Bitmap bitmap, float degree) {
      // Implementation
   }

   // And other stuff
   public static int getApplicationVersion(Context context){
       // Impl...
   }
}

If I have two or more activities and I'm calling a useful method in each of the activity, is this will be a memory leak? Does the Util class will be like a "bridge", holding a reference between activities?

public Activity_A extends Activity{
   @Override
   protected void onCreate(Bundle bundle){
      Bitmap bitmap = Util.downloadImage(this, "http://static.asd....");
   }
}

public Activity_B extends Activity{
   @Override
   protected void onCreate(Bundle bundle){
      Bitmap bitmap = Util.downloadImage(this, "http://something");
      bitmap = Util.rotateBitmap(bitmap, 90f);
   }
}

Or calling in a fragment:

public class MyFragment extends Fragment{
   @Override
   protected void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
      // inflating...


      Util.playBackgroundMusic(getActivity(), Constants.MUSIC_1);

      Log.i("APP", "Application version is: " + Util.getApplicationVersion(getActivity());
   }
}

Does this will cause memory leak? I am so confused...

I think it would be a problem if you held a reference to an activity inside the static Util class, because it would prevent the activity from being garbage collected even after it's finished.

But in your case I don't see any memory leak issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM