Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Loading assemblies in the background so that it does not block UI thread

mliumliu USMember ✭✭
edited March 2014 in Xamarin.Android

Hi,

I'm seeing an issue where the first time I press a button in my app that performs some operation (which is marked as an async method), the UI freezes for ~1 second while it program appears to be trying to load the referenced dlls. On the 2nd attempt and thereafter, the UI does not freeze. In the debug console I'm seeing what appears to be the program trying to load the referenced dlls:

[Mono] Remapped public key token of retargetable assembly System.Windows from 7cec85d7bea7798e to b03f5f7f11d50a3a
[Mono] The request to load the retargetable assembly System.Windows v2.0.5.0 was remapped to System.Windows v2.0.0.0
[Mono] Unloading image System.Windows.dll [0x7968b008].
[Mono] Image addref System.Windows[0x7968bbc0] -> System.Windows.dll[0x79b76030]: 5
[Mono] Assembly Ref addref Cirrious.MvvmCross[0x76e91250] -> System.Windows[0x79b76a20]: 4
[Mono] The request to load the assembly System v2.0.5.0 was remapped to v2.0.0.0
[Mono] Unloading image System.dll [0x797b8898].
[Mono] Image addref System[0x7968bc70] -> System.dll[0x77cbaab0]: 15
[Mono] Assembly Ref addref System.Net.Http[0x77fb6790] -> System[0x77cbb8f8]: 14
[Mono] Assembly Ref addref stocks.core[0x76ed98c8] -> System.Net.Primitives[0x770d24e0]: 2
[Mono] The request to load the assembly System v2.0.5.0 was remapped to v2.0.0.0
[Mono] Unloading image System.dll [0x797ba200].
[Mono] Image addref System[0x7968bc70] -> System.dll[0x77cbaab0]: 16
[Mono] Assembly Ref addref System.Net.Primitives[0x770d24e0] -> System[0x77cbb8f8]: 15
...
[Mono] The request to load the assembly Mono.Security v2.0.5.0 was remapped to v2.0.0.0
[Mono] Image addref Mono.Security[0x79803a68] -> Mono.Security.dll[0x798231a8]: 1
[Mono] Assembly Mono.Security[0x79803a68] added to domain RootDomain, ref_count=1
...
[Mono] Assembly Ref addref System[0x77cbb8f8] -> Mono.Security[0x79803a68]: 2
[Mono] The request to load the assembly mscorlib v2.0.5.0 was remapped to v2.0.0.0
[Mono] Unloading image mscorlib.dll [0x7995d4a0].
Loaded assembly: Mono.Security.dll [External]
...
(more dlls loaded)
...
[Choreographer] Skipped 61 frames!  The application may be doing too much work on its main thread.

I think the UI is freezing because these dlls are being loaded on the UI thread. The method being called when the button is clicked is already marked as an async method. Is there some way to force these dlls to load in the background so that it does not block the UI thread?

Posts

  • mliumliu USMember ✭✭

    Bump

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai
    edited March 2014

    Just because a method is marked with async does not mean that it is doing it's work on the background thread. At some point the code that executes in that method will need to spin up a thread and return a task that can be awaited on.

    You could wrap that method in a:

     private async Task DoStuff()
        {
          IsBusy = true;
          await IWillDoWorkAsync();
          IsBusy = false;
        }
    
        private Task IWillDoWorkAsync()
        {
          return Task.Factory.StartNew(() =>
          {
            /* do stuff */
          });
        }
    

    really great webinar: http://blog.xamarin.com/csharp-async-on-ios-and-android/

  • mliumliu USMember ✭✭
    edited March 2014

    Inside the async method I have a Task being returned and awaited on - it looks like this:

    public async Task Refresh()
    {
      ...
      HttpClient client = new HttpClient(messageHandler);
      ...
      await client.GetStringAsync(url);
      ...
    }
    

    However I don't think that's the problem. As I mentioned, the first time the method is executed the UI freezes and I see a bunch of dlls being loaded in the console. The second time and after the method is executed, the UI does not freeze since the dlls have already been loaded. It only freezes the first time I think due to the delay loading of dlls. If this method wasn't being executed in the background, it would freeze the UI the second and third time around as well.

    Am I correct here that xamarin does delay loading of dll dependencies? Is there a way to load these dlls in the background the first time around so that it doesn't freeze the UI thread?

  • TudouTudou USMember

    Hi I see a similar problem, the first time a button is pressed there are lots of Mono requests to load assembly messages in the log and the ui freezes.

    Would be great if that could load whilst waiting for the user to press the button.

  • alienpaperalienpaper GBMember ✭✭

    Would be nice to have some explanation from Xamarin about the DLL remapping. It has been asked several times.

  • mliumliu USMember ✭✭
    edited June 2014

    Can we get an answer on whether there is any workaround to this issue? Would be great if this limitation is documented somewhere. For my app, not having UI stutters is critical - if there is no workaround, I'll need to implement natively.

  • AlexeyShikovAlexeyShikov UAMember ✭✭

    Any news on this topic?

  • TomStandaert.0575TomStandaert.0575 BEMember ✭✭

    same question, on some assemblies the ui freezes that long (> 6 seconds) that android reports the app as unresponsive

  • DanielJGMacleanDanielJGMaclean USUniversity ✭✭

    Can't believe there is nothing further on this topic. I'm facing a similar problem where the first page in the App loads but when I navigate to the second it takes 6 seconds, but subsequent clicks load it in 0.4 seconds. Again, in the first instance there are "Assembly Ref address" references in the logs. I originally thought it was because of a slow DB implementation, but i've ruled that out.

  • ClowningClowning USMember ✭✭

    i have the same problem..

  • voidvoid DKBeta ✭✭✭

    I'm seeing a lot of these now on VSfM/XS Mono 5. release. It happens during startup on Android device that is now so slow on app startup that the OS asks to kill the app.

    Here's a SMALL subset :smile:

    [Mono] Config attempting to parse: 'System.Xml.ReaderWriter.dll.config'.
    [Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Xml.ReaderWriter/System.Xml.ReaderWriter.config'.
    [Mono] Unloading image System.Reflection.dll [0x713fcea800].
    [Mono] Config attempting to parse: 'System.Reflection.dll.config'.
    [Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Reflection/System.Reflection.config'.
    [Mono] The request to load the assembly System.Xml.ReaderWriter v4.0.0.0 was remapped to v2.0.5.0
    [Mono] Unloading image System.Xml.ReaderWriter.dll [0x713fcea800].
    [Mono] Config attempting to parse: 'System.Xml.ReaderWriter.dll.config'.
    [Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Xml.ReaderWriter/System.Xml.ReaderWriter.config'.
    [Mono] Unloading image System.Reflection.dll [0x713fcea800].
    [Mono] Config attempting to parse: 'System.Reflection.dll.config'.
    [Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Reflection/System.Reflection.config'.

  • DanielWeigert.4984DanielWeigert.4984 USMember ✭✭

    any new answers on this ?
    Facing the same problem after enabling Multidex for my Xamarin.Android project.....

  • braderbrader USMember ✭✭

    I'm having the same issue. I have some additional output in my logs where it searches and probes...

    05-26 11:40:15.085: D/Mono(28216): DllImport attempting to load: '__Internal'.
    05-26 11:40:15.085: D/Mono(28216): DllImport loaded library '(null)'.
    05-26 11:40:15.085: D/Mono(28216): DllImport searching in: '__Internal' ('(null)').
    05-26 11:40:15.085: D/Mono(28216): Searching for 'monodroid_get_system_property'.
    05-26 11:40:15.085: D/Mono(28216): Probing 'monodroid_get_system_property'.
    05-26 11:40:15.085: D/Mono(28216): Found as 'monodroid_get_system_property'.
    05-26 11:40:15.086: D/Mono(28216): DllImport searching in: '__Internal' ('(null)').
    05-26 11:40:15.086: D/Mono(28216): Searching for 'monodroid_free'.
    05-26 11:40:15.086: D/Mono(28216): Probing 'monodroid_free'.
    05-26 11:40:15.086: D/Mono(28216): Found as 'monodroid_free'.
    05-26 11:40:15.115: D/Mono(28216): DllImport searching in: 'libsqlite3_xamarin' ('libsqlite3_xamarin.so').
    05-26 11:40:15.115: D/Mono(28216): Searching for 'sqlite3_bind_int64'.
    05-26 11:40:15.115: D/Mono(28216): Probing 'sqlite3_bind_int64'.
    05-26 11:40:15.115: D/Mono(28216): Found as 'sqlite3_bind_int64'.
    05-26 11:40:15.116: D/Mono(28216): DllImport searching in: 'libsqlite3_xamarin' ('libsqlite3_xamarin.so').
    05-26 11:40:15.117: D/Mono(28216): Searching for 'sqlite3_reset'.
    05-26 11:40:15.117: D/Mono(28216): Probing 'sqlite3_reset'.
    05-26 11:40:15.117: D/Mono(28216): Found as 'sqlite3_reset'.
    05-26 11:40:15.117: D/Mono(28216): DllImport searching in: 'libsqlite3_xamarin' ('libsqlite3_xamarin.so').
    05-26 11:40:15.117: D/Mono(28216): Searching for 'sqlite3_last_insert_rowid'.
    05-26 11:40:15.117: D/Mono(28216): Probing 'sqlite3_last_insert_rowid'.
    05-26 11:40:15.117: D/Mono(28216): Found as 'sqlite3_last_insert_rowid'.
    05-26 11:40:15.159: D/Mono(28216): DllImport searching in: 'libsqlite3_xamarin' ('libsqlite3_xamarin.so').
    05-26 11:40:15.159: D/Mono(28216): Searching for 'sqlite3_bind_null'.
    05-26 11:40:15.159: D/Mono(28216): Probing 'sqlite3_bind_null'.
    05-26 11:40:15.159: D/Mono(28216): Found as 'sqlite3_bind_null'.
    ...
    (then lots of GC, one pasted below)
    ...
    05-26 11:40:14.751: D/Mono(28216): GC_BRIDGE: Complete, was running for 0.09ms
    05-26 11:40:14.751: D/Mono(28216): GC_MINOR: (Nursery full) time 2.07ms, stw 2.49ms promoted 16K major size: 5136K in use: 4339K los size: 2048K in use: 1090K
    05-26 11:40:14.966: D/Mono(28216): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 20 xref 1 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.07ms tarjan 0.02ms scc-setup 0.06ms gather-xref 0.00ms xref-setup 0.07ms cleanup 0.00ms
    ...
    05-26 11:40:30.821: I/Choreographer(28216): Skipped 1346 frames!  The application may be doing too much work on its main thread.
    
Sign In or Register to comment.