This is fine, and will not cause a memory leak.
As soon as onCreate
finishes executing, h
will be out of scope and become eligible for garbage collection. If h
was static, then you would run into problems. Only when the reference to the context outlives the lifecycle of the context itself will a memory leak occur. A few helpful hints:
- Use
Context.getApplicationContext()
when possible. This context will live as long as your application is alive. - Be careful when using static fields and inner classes.
- Run your application through a profiler to check for leaks.