Releasing references in a proper way with server connection library

I work on a custom library (Co.line) to do HTTP requests in REST. I tried to think of reducing the memory usage by cancelling the thread background and destroying all references. It uses a Context reference to retrieve the server response in main thread from a callback.

I used this lib in one of my project, and I decided to implement my classes connection by creating a parent object (which communicates directly with Coline) and all children services (which set differently headers, parameters, body values, etc). This pattern uses the Context by passing it into the constructors until Coline's constructor.

The parent object retains a Coline's instance and is declared as follows:

public class Repository {      protected WeakReference<Coline> coline;      protected void init(Context context) {         coline = new WeakReference<>(Coline.init(context));     }      // other methods to set headers, values, call the request, etc.     ...      public void cancel() {         Coline clne = coline.get();         if (clne != null) {             clne.cancel();             clne = null;         }         if (coline != null) {             coline = null;         }     } } 

And this how the children services use this parent:

public class SomethingService extends Repository {      public SomethingService(Context context) {         super.init(context);     }      // do treatments, calculs and set headers, body, do a request     // by calling 'super.nameOfMethod(objectValues);' each time     ... } 

Finally, this is how the activity uses this service:

smthgService = new SomethingService(this);  // initialize the params for the request smthgService.nameOfMethod(myValues); smthgService.nameOfOtherMethod(myOtherValues);  // do the request and retrieve it in a callback smthgService.nameOfMethodToDoRequest(new Collback() {     @Override     public void onResult(Error err, Response res) { } });  ...  // lately, when the activity is destroying @Override public void onDestroy() {     // kill the service     if (smthgService != null) {         smthgService.cancel();         smthgService = null;     }     super.onDestroy(); } 

By calling cancel() in Repository, I interrupt the background thread in Coline and destroy the references from it, then destroy all local references. And by calling onDestroy(), I kill the local reference of the service.

So, here's my questions:

  • Is it useful to keep the Coline's reference in a WeakReference while I already have a verification on Context in Coline (which is also a WeakReference object)?
  • By killing the Coline reference in Repository's cancel(), the Context passing into all the constructors should be destroyed, isn't it? And, I need a little confirmation, by extending the class, when I kill the child, it also kills the parent instance, right?
  • Passing the Context into all the constructors scary me a little bit. Is that a good practice?
  • Finally, is there any references that I forgot to destroy in my patterns?

Thanks for all the help.


Category: java Time: 2016-07-29 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.192 (s). 12 q(s)