android-branch-deep-linking-attribution icon indicating copy to clipboard operation
android-branch-deep-linking-attribution copied to clipboard

StrictMode violations on initialization

Open ShaishavGandhi opened this issue 6 years ago • 11 comments

Calling Branch.getAutoInstance(context) during app start causes multiple StrictMode violations (all DiskReadViolations). Here's the logs:

2018-05-18 09:53:44.589 6898-6898/com.glassdoor.app D/StrictMode: StrictMode policy violation; ~duration=164 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=1114175 violation=2 at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1440) at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251) at java.io.File.exists(File.java:807) at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:572) at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:563) at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:519) at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:714) at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:368) at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:167) at io.branch.referral.PrefHelper.<init>(PrefHelper.java:154) at io.branch.referral.PrefHelper.getInstance(PrefHelper.java:172) at io.branch.referral.Branch.<init>(Branch.java:424) at io.branch.referral.Branch.initInstance(Branch.java:744) at io.branch.referral.Branch.getBranchInstance(Branch.java:565) at io.branch.referral.Branch.getAutoInstance(Branch.java:645) at com.glassdoor.gdandroid2.app.GDApplication.initBranch(GDApplication.java:540) at com.glassdoor.gdandroid2.app.GDApplication.onCreate(GDApplication.java:312) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5740) at android.app.ActivityThread.-wrap1(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

2018-05-18 09:53:44.590 6898-6898/com.glassdoor.app D/StrictMode: StrictMode policy violation; ~duration=136 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=1114175 violation=2 at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1440) at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251) at java.io.File.exists(File.java:807) at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:572) at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:563) at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:519) at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:714) at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:368) at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:167) at io.branch.referral.ServerRequestQueue.<init>(ServerRequestQueue.java:61) at io.branch.referral.ServerRequestQueue.getInstance(ServerRequestQueue.java:46) at io.branch.referral.Branch.<init>(Branch.java:427) at io.branch.referral.Branch.initInstance(Branch.java:744) at io.branch.referral.Branch.getBranchInstance(Branch.java:565) at io.branch.referral.Branch.getAutoInstance(Branch.java:645) at com.glassdoor.gdandroid2.app.GDApplication.initBranch(GDApplication.java:540) at com.glassdoor.gdandroid2.app.GDApplication.onCreate(GDApplication.java:312) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5740) at android.app.ActivityThread.-wrap1(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

2018-05-18 09:44:50.363 6173-6173/? D/StrictMode: StrictMode policy violation; ~duration=129 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=1114175 violation=2 at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1440) at android.app.SharedPreferencesImpl.awaitLoadedLocked(SharedPreferencesImpl.java:234) at android.app.SharedPreferencesImpl.edit(SharedPreferencesImpl.java:315) at io.branch.referral.ServerRequestQueue.<init>(ServerRequestQueue.java:62) at io.branch.referral.ServerRequestQueue.getInstance(ServerRequestQueue.java:46) at io.branch.referral.Branch.<init>(Branch.java:427) at io.branch.referral.Branch.initInstance(Branch.java:744) at io.branch.referral.Branch.getBranchInstance(Branch.java:565) at io.branch.referral.Branch.getAutoInstance(Branch.java:645) at com.glassdoor.gdandroid2.app.GDApplication.initBranch(GDApplication.java:540) at com.glassdoor.gdandroid2.app.GDApplication.onCreate(GDApplication.java:312) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5740) at android.app.ActivityThread.-wrap1(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)

ShaishavGandhi avatar May 18 '18 17:05 ShaishavGandhi

@shaishavgandhi05 Thanks for reaching out. Android strict mode warns all kind of suspicious disk reads or network calls just for dev to assure or fix. In the above case the Strict mode warning is regarding accessing application preference from the main thread and it is completely legal and thread safe in Android. As it is mentioned In Google's doc for strict mode.

Don't feel compelled to fix everything that StrictMode finds. In particular, many cases of disk access are often necessary during the normal activity lifecycle. Use StrictMode to find things you did by accident. Network requests on the UI thread are almost always a problem, though.

From our side we made sure we read the disk only for preference operations. Do you think this help to mitigate the concern?

sojanpr avatar May 18 '18 19:05 sojanpr

While I agree, you can see that those are three StrictMode violations on a single Branch.getAutoInstance(). If you see the log, each StrictMode violation is 100ms+. Since this call is made at app start, that theoretically adds 300ms overhead to Application initialization.

With new Play Store rankings depending a lot on performance metrics (including app start times), you can see why this might be a concern.

ShaishavGandhi avatar May 19 '18 00:05 ShaishavGandhi

@shaishavgandhi05 We tested and confirmed that the disk read is not taking this much time even though it shows in the strict mode violation. Definitely we will add optimization in the next version to reduce the disk read on start up

sojanpr avatar May 22 '18 18:05 sojanpr

Sounds good! Thanks!

ShaishavGandhi avatar May 22 '18 18:05 ShaishavGandhi

Any updates on this? We're facing the same issue and on old devices it often adds several 100ms to to the startup time. Can't you run the I/O in a background thread and then use callbacks to the UI thread? Anything we can assist with to implement that?

Yrlec avatar Dec 01 '18 11:12 Yrlec

Any updates on this? we are also facing the same issue

GaurawNegi avatar Aug 05 '19 11:08 GaurawNegi

implementation 'io.branch.sdk.android:library:4.1.1'

Same problem here. This issue should be reopened.

dbacinski avatar Oct 24 '19 17:10 dbacinski

Jira Ticket Filed: SDK-580

apeterson-branch avatar Oct 24 '19 18:10 apeterson-branch

This is still a problem. In our case we're seeing a ~200ms delay due to disk reads in BranchActivityLifeCycleObserver.onActivityStarted.

Turnsole avatar Nov 16 '22 16:11 Turnsole

Hi @Turnsole, we will be investigating further this sprint. We will keep this thread up to date with our findings and conclusions.

jf-branch avatar Nov 16 '22 17:11 jf-branch