Android : References to a Context and memory leaks -
I read that this is a mistake and the source of memory leaks in the Android application is a reference.
But it does not explain to me whether it is okay to create a class that looks like this:
Public class HelperClass {Private context context; Public HelperClass (reference reference) {this.context = context; } Public Zero myHelperMethod () {// This uses. Context}}
And call it with any activity:
Public class expands MyActivity activity {Crate on Public Zero (bundled savedinstenstate ) {Helperclass h = new helperclass (this); H.myHelperMethod (); } ...
This is fine, and the reason for the memory leak will not be the reason.
As soon as on
ends, h
will end and become eligible for garbage collection. If h
was stable, then you will take part in problems only when the references to references come out of the life cycle of reference, then the memory leak will occur. Some useful pointers:
- Use
Context.getApplicationContext ()
when possible. - Be careful while using frozen areas and internal orbits.
- To check leaks.
Comments
Post a Comment