0

I am using a JobIntent service to perform some operation. I am starting the Service from say Screen A.

After the service performs its work I am sending a callback from JobIntent Service to the Screen A. Then immediately the Screen A finishes itself and opens a new Screen say B.

I am able to notice that there is a log stating Leaked Service Connection. Below is the log

android.app.ServiceConnectionLeaked: Activity Package.ScreenA has leaked 
ServiceConnection Package.ServiceName$1@7a41f83 that was originally bound here

What does this error mean and is there any damage if this error continues? What can be done to prevent these kinds of errors?

Even though I am not sure but the service holds the context reference of the screenA which is why this might be happening?

Thanks in Advance!

Rahulrr2602
  • 701
  • 1
  • 13
  • 34

2 Answers2

2

Watch out for JobIntentService memory leak

Pass activity.getApplicationContext() — then static map indirectly will hold only Application object instance reference.

You can read this post this would be helpful for you.

https://medium.com/@luszczuk/watch-out-for-jobintentservice-memory-leak-828a629ab085

Credit: Michał Łuszczuk

Waheed Nazir
  • 584
  • 7
  • 11
  • Thanks for the very helpful answer. Have a great day :) – Rahulrr2602 May 05 '19 at 21:23
  • Just another doubt. What is the negative impact of leaking a service connection? – Rahulrr2602 May 05 '19 at 21:23
  • 1
    These are the compatibility issues with older versions. So apparently there is no severe harm. But it is better to handle it. Good luck – Waheed Nazir May 06 '19 at 06:26
  • Thanks for the clarification. – Rahulrr2602 May 07 '19 at 23:55
  • Hi, I had one more doubt can you please try and answer that also. Can more than one JobIntentService run parallelly if they are started by the same context? if Yes the how many JobIntentServices can run parallelly. – Rahulrr2602 May 09 '19 at 06:16
  • Nope we cannot run task(Job Intent Service) in parallel. Multiple intents are queued on the same worker thread. For this you can use RxJava that would manage in a pretty good way Or you can use Handler approach. Use this link to understand this. https://academy.realm.io/posts/android-threading-background-tasks/ – Waheed Nazir Jun 21 '19 at 20:41
  • You can also read this blog it would be a great help to understand threads in details. https://www.toptal.com/android/android-threading-all-you-need-to-know – Waheed Nazir Jun 21 '19 at 20:45
1

enter image description here

@Rahulrr2602 Please use this chart to understand the limitations of each.

Waheed Nazir
  • 584
  • 7
  • 11