dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

MAUI very slow on physical android devices #18838

Open OpterJohan-Hallberg opened 10 months ago

OpterJohan-Hallberg commented 10 months ago

Description

MAUI Android runs very well on emulators, but on physical devices, both debug and release it runs very slow. I believe its due to an error that gets throw due to a variety of reasons, that gets worse the more content a project has. I'm our current project just switching to another page can take up to 10 or so seconds, on a new MAUI project it isn't noticeable, but its there still. This did not use to take that long i Xamarin. Its unusable with that much delay on pretty much everything.

The errors always look something similar to this: [monodroid-assembly] typemap: unable to find mapping to a managed type from Java type 'androidx/fragment/app/FragmentManagerImpl' [monodroid-assembly] typemap: called from [monodroid-assembly] at Java.Interop.TypeManager.GetJavaToManagedType(String class_name) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 228 [monodroid-assembly] at Java.Interop.TypeManager.CreateInstance(IntPtr handle, JniHandleOwnership transfer, Type targetType) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 274 [monodroid-assembly] at Java.Lang.Object.GetObject(IntPtr handle, JniHandleOwnership transfer, Type type) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 303 [monodroid-assembly] at Java.Lang.Object._GetObject[Frag

It can be reproduced in a completely new and untouched MAUI project too: [monodroid-assembly] typemap: unable to find mapping to a managed type from Java type 'com/android/internal/policy/PhoneLayoutInflater' [monodroid-assembly] typemap: called from [monodroid-assembly] at Java.Interop.TypeManager.GetJavaToManagedType(String class_name) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 230 [monodroid-assembly] at Java.Interop.TypeManager.CreateInstance(IntPtr handle, JniHandleOwnership transfer, Type targetType) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 274 [monodroid-assembly] at Java.Lang.Object.GetObject(IntPtr handle, JniHandleOwnership transfer, Type type) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 304 [monodroid-assembly] at Java.Lang.Object._GetObject[LayoutInflater](IntPtr handle, JniHandleOwnership transfer) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 290 [monodroid-assembly] at Java.Lang.Object.GetObject[LayoutInflater](IntPtr handle, JniHandleOwnership transfer) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 281 [monodroid-assembly] at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.Fragment.cs:line 2028 [monodroid-assembly] at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 352 [monodroid-assembly] typemap: unable to find mapping to a managed type from Java type 'androidx/fragment/app/FragmentManagerImpl' [monodroid-assembly] typemap: called from [monodroid-assembly] at Java.Interop.TypeManager.GetJavaToManagedType(String class_name) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 230 [monodroid-assembly] at Java.Interop.TypeManager.CreateInstance(IntPtr handle, JniHandleOwnership transfer, Type targetType) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 274 [monodroid-assembly] at Java.Lang.Object.GetObject(IntPtr handle, JniHandleOwnership transfer, Type type) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 304 [monodroid-assembly] at Java.Lang.Object._GetObject[FragmentManager](IntPtr handle, JniHandleOwnership transfer) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 290 [monodroid-assembly] at Java.Lang.Object.GetObject[FragmentManager](IntPtr handle, JniHandleOwnership transfer) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 281 [monodroid-assembly] at AndroidX.Fragment.App.Fragment.get_ChildFragmentManager() in /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.Fragment.cs:line 416 [monodroid-assembly] at Microsoft.Maui.Controls.Platform.Compatibility.ShellFragmentContainer.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellFragmentContainer.cs:line 48 [monodroid-assembly] at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.Fragment.cs:line 2031 [monodroid-assembly] at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 352 [Mono] Running class .cctor for Microsoft.Maui.Handlers.PageHandler from '/data/data/com.companyname.mauiapp3test/files/.__override__/Microsoft.Maui.dll' [Mono] Running class .cctor for Microsoft.Maui.Handlers.ContentViewHandler from '/data/data/com.companyname.mauiapp3test/files/.__override__/Microsoft.Maui.dll'

Steps to Reproduce

  1. Create a new .Net MAUI App
  2. Start debug to physical device.
  3. Give i a moment after it launches
  4. `[monodroid-assembly] typemap: unable to find mapping to a managed type from Java type 'com/android/internal/policy/PhoneLayoutInflater' error will appear in debug log.

Link to public reproduction project repository

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12 and up at least.

Did you find any workaround?

We have not been able to find a workaround.

Relevant log output

[monodroid-assembly] typemap: unable to find mapping to a managed type from Java type 'com/android/internal/policy/PhoneLayoutInflater'
[monodroid-assembly] typemap: called from
[monodroid-assembly] at Java.Interop.TypeManager.GetJavaToManagedType(String class_name) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 230
[monodroid-assembly]    at Java.Interop.TypeManager.CreateInstance(IntPtr handle, JniHandleOwnership transfer, Type targetType) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 274
[monodroid-assembly]    at Java.Lang.Object.GetObject(IntPtr handle, JniHandleOwnership transfer, Type type) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 304
[monodroid-assembly]    at Java.Lang.Object._GetObject[LayoutInflater](IntPtr handle, JniHandleOwnership transfer) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 290
[monodroid-assembly]    at Java.Lang.Object.GetObject[LayoutInflater](IntPtr handle, JniHandleOwnership transfer) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 281
[monodroid-assembly]    at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.Fragment.cs:line 2028
[monodroid-assembly]    at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 352
[monodroid-assembly] typemap: unable to find mapping to a managed type from Java type 'androidx/fragment/app/FragmentManagerImpl'
[monodroid-assembly] typemap: called from
[monodroid-assembly] at Java.Interop.TypeManager.GetJavaToManagedType(String class_name) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 230
[monodroid-assembly]    at Java.Interop.TypeManager.CreateInstance(IntPtr handle, JniHandleOwnership transfer, Type targetType) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 274
[monodroid-assembly]    at Java.Lang.Object.GetObject(IntPtr handle, JniHandleOwnership transfer, Type type) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 304
[monodroid-assembly]    at Java.Lang.Object._GetObject[FragmentManager](IntPtr handle, JniHandleOwnership transfer) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 290
[monodroid-assembly]    at Java.Lang.Object.GetObject[FragmentManager](IntPtr handle, JniHandleOwnership transfer) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 281
[monodroid-assembly]    at AndroidX.Fragment.App.Fragment.get_ChildFragmentManager() in /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.Fragment.cs:line 416
[monodroid-assembly]    at Microsoft.Maui.Controls.Platform.Compatibility.ShellFragmentContainer.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellFragmentContainer.cs:line 48
[monodroid-assembly]    at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.Fragment.cs:line 2031
[monodroid-assembly]    at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 352
VNGames commented 9 months ago

For me it is also very slow without any prints like reporter has here, on Android 12. I migrated (manually = creating the same app by moving pieces of code and rewriting renderers into handlers etc) an app from Xamarin and it is very slow on device. Even a simple page takes a few seconds (5 and more) to open. And the only prints Debug does during that time is GC Explicit concurrent copying GC freed 7731(487KB) AllocSpace objects, 0(0B) LOS objects, 80% free, 6090KB/29MB, paused 57us,41us total 35.242ms Which is not critical and isn't a reason to delay for 5 sec for a simple page. I use NET 8 and MAUI 8.0.3. On Emulator it runs ok.

In DEBUG. In RELEASE everything is ok. Turning off Hot reload didn't help.

asi-evin commented 8 months ago

I can confirm I am getting these messages all the time in debug. Common ones include (but not limited to):

That last one happens VERY often as it's triggered by getting the main display DPI in a looped animation (at Microsoft.Maui.Devices.DeviceDisplayImplementationBase.get_MainDisplayInfo()).

It makes it nearly impossible to find debug messages in the console.

Edit: Thought I should add, it appears to be device specific: My TCL 30XE running Android 12 does not have this problem, but my Samsung A13 running Android 13 excretes this issue like sweat in a texas summer.

Edit 2: Nvm, my TCL now has these issues...

South2AK commented 7 months ago

Having the same problem. Any news to this?

pboisso commented 6 months ago

Same here.

rudyspano commented 6 months ago

Forcing xaml compilation results in same performance as Xamarin Forms in our side in Debug. (Breaks Xaml Hot Reload for the moment) https://www.sharpnado.com/net-maui-painfully-slow-debugging/?fbclid=IwAR0_aHJXi0OBoh8VO7gdn_u2AJcRBETKqgM05MnsaomaCmsFCnbq-GDGaBI

clintcambier commented 6 months ago

Also from our end there is a major difference in which physical Android devices are about 5 times slower than Android emulators AND iOS physical devices.

This is a critical problem that does not seem to be on the radar at all. Who should we contact to escalate this further?

QiteSimon commented 6 months ago

I'm having trouble with this as well. My app, which was migrated from Xamarin, is MUCH slower in MAUI than in Xamarin. At least it is on Android. iOS seems to be running just fine.

VNGames commented 6 months ago

@clintcambier @QiteSimon Did you try solution in the post above yours?

Adding this for DEBUG should solve slowness <_MauiForceXamlCForDebug>true</_MauiForceXamlCForDebug> (I didn't try yet, Just asking but it seems like this should be the solution. In Production my app isn't slow at all. I would say even faster than Xamarin, But during DEBUG it is slow on a physical device Android and article above says it is because in MAUI xaml compilation is turned off by default)

QiteSimon commented 6 months ago

The performance while debugging is acceptable (slower yes, but ok). It's the production build that goes much slower and is not acceptable for our users.

catnip050 commented 5 months ago

The same problem exists. <_MauiForceXamlCForDebug>true</_MauiForceXamlCForDebug> did not solve the problem. Has anyone made any progress?

mnxamdev commented 5 months ago

I'm having trouble with this as well. My app, which was migrated from Xamarin, is MUCH slower in MAUI than in Xamarin. At least it is on Android. iOS seems to be running just fine.

Same here but to a lesser degree on iOS as it's definitely not as snappy as Xamarin.Forms on iOS. Our project on Xamarin.Forms in Android is much more performant yet Xamarin.Forms support is End of Life in 5 days. Menu drawer closing while navigating is supper laggy as well as everything else so much so that we can't even develop properly. Again, these issues are also apparent in Release mode and we can't release to production with these serious performance issues. They exist in our app in debug and release in MAUI but not Xamarin.Forms. Microsoft needs to extend Xamarin.Forms support until MAUI is stable and performant as the product they intend to be replacing.

clintcambier commented 5 months ago

Fully agree that support should be extended... Any update about that?

Op vr 26 apr. 2024 18:08 schreef mnxamdev @.***>:

I'm having trouble with this as well. My app, which was migrated from Xamarin, is MUCH slower in MAUI than in Xamarin. At least it is on Android. iOS seems to be running just fine.

Same here but to a lesser degree on iOS as it's definitely not as snappy as Xamarin.Forms on iOS. Our project on Xamarin.Forms in Android is much more performant yet Xamarin.Forms support is End of Life in 5 days. Menu drawer closing while navigating is supper laggy as well as everything else so much so that we can't even develop properly. Again, these issues are also apparent in Release mode and we can't release to production with these serious performance issues. They exist in our app in debug and release in MAUI but not Xamarin.Forms. Microsoft needs to extend Xamarin.Forms support until MAUI is stable and performant as the product they intend to be replacing.

— Reply to this email directly, view it on GitHub https://github.com/dotnet/maui/issues/18838#issuecomment-2079683978, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB32IPUDSJ4GAS2QPIZXAVLY7J3YDAVCNFSM6AAAAAA7PZDY3OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANZZGY4DGOJXHA . You are receiving this because you were mentioned.Message ID: @.***>

rudyspano commented 5 months ago

@mnxamdev, my advice: I know that can be time consuming but you should create small projects to isolate each problem that you suspect. I personnaly don't use "Menu Drawer" for instance. If you can reproduce your performance issues in small projects that you can share and create Github issue Microsoft and the community will help you...

Zhanglirong-Winnie commented 5 months ago

Verified this issue with Visual Studio 17.10 Preview 5 (8.0.21&8.0.3). Can repro on android device platform. image

PureWeen commented 5 months ago

Can you attach a logcat file? https://learn.microsoft.com/en-us/xamarin/android/deploy-test/debugging/android-debug-log?tabs=windows

You can also grab a speedscope to see if that reveals anything https://github.com/dotnet/maui/wiki/Profiling-.NET-MAUI-Apps#feature-xyz-was-faster-in-xamarinforms

PureWeen commented 4 months ago

@jonathanpeppers thoughts?

SKeehnen commented 4 months ago

A solution would be great, not only makes it the app slower but also produces an unreadable debug window. I'm using many settings through the Preferences, so my debug window is completely filled with these messages:

[monodroid-assembly] typemap: unable to find mapping to a managed type from Java type 'android/app/SharedPreferencesImpl' [monodroid-assembly] typemap: called from [monodroid-assembly] at Java.Interop.TypeManager.GetJavaToManagedType(String class_name) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 228 [monodroid-assembly] at Java.Interop.TypeManager.CreateInstance(IntPtr handle, JniHandleOwnership transfer, Type targetType) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:line 274 [monodroid-assembly] at Java.Lang.Object.GetObject(IntPtr handle, JniHandleOwnership transfer, Type type) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 303 [monodroid-assembly] at Java.Lang.Object._GetObject[ISharedPreferences](IntPtr handle, JniHandleOwnership transfer) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 289 [monodroid-assembly] at Java.Lang.Object.GetObject[ISharedPreferences](IntPtr handle, JniHandleOwnership transfer) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:line 281 [monodroid-assembly] at Android.Preferences.PreferenceManager.GetDefaultSharedPreferences(Context context) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Preferences.PreferenceManager.cs:line 886 [monodroid-assembly] at Microsoft.Maui.Storage.PreferencesImplementation.GetSharedPreferences(String sharedName) in D:\a\_work\1\s\src\Essentials\src\Preferences\Preferences.android.cs:line 160 [monodroid-assembly] at Microsoft.Maui.Storage.PreferencesImplementation.Get[Int32](String key, Int32 defaultValue, String sharedName) in D:\a\_work\1\s\src\Essentials\src\Preferences\Preferences.android.cs:line 99 [monodroid-assembly] at Microsoft.Maui.Storage.Preferences.Get(String key, Int32 defaultValue, String sharedName) in D:\a\_work\1\s\src\Essentials\src\Preferences\Preferences.shared.cs:line 185 [monodroid-assembly] at Microsoft.Maui.Storage.Preferences.Get(String key, Int32 defaultValue) in D:\a\_work\1\s\src\Essentials\src\Preferences\Preferences.shared.cs:line 116

jonathanpeppers commented 4 months ago

The monodroid-assembly log messages are not on by default. Did you run a command like this to enable them?

adb shell setprop debug.mono.log assembly

Simply enabling log messages like these will make the app slower. setprop doesn't clear unless you reboot the device, but you can clear it with:

adb shell setprop debug.mono.log "''"
SKeehnen commented 3 months ago

Thanks @jonathanpeppers that worked perfect! At least the debug window is readable again and the performance is better, but still worse then running in debug mode using Xamarin.

wagaana commented 3 months ago

My app was working well in .net 7.0 preview, but after upgrading it to .net 8.0, the app got slow on Android.

jonathanpeppers commented 3 months ago

@wagaana did you profile your app? If you could share a sample project or .speedscope file: https://aka.ms/profile-maui

I don't know of any performance regression going from .NET 7 to .NET 8.

irshadka commented 3 months ago

Any fixes for this…… iOS running perfectly but in Android full lag.

marcmarsinach commented 1 month ago

I'm having trouble with this as well. My app, which was migrated from Xamarin, is MUCH slower in MAUI than in Xamarin. At least it is on Android. iOS seems to be running just fine.

Same problem here

jonathanpeppers commented 1 month ago

@marcmarsinach did you already review some of the instructions at the top here: https://aka.ms/profile-maui

schaudhary-extron commented 1 month ago

Is this issue resolved? I am still having the issue with application not finding mapping and slow debugging.

zkolar commented 4 weeks ago

Any news here? Have same problem, in emulator is ok, but on physical android device unusable

jonathanpeppers commented 4 weeks ago

Any news here? Have same problem, in emulator is ok, but on physical android device unusable

This sounds backwards to me: my experience is that something like even a Pixel 5 (few years old) is faster than an emulator.

If someone is able to review https://aka.ms/profile-maui and share .speedscope files of the slowdown we can investigate. Thanks!

RafaRioFal4699 commented 3 weeks ago

Same here, since I migrated my project from Xamarin.Forms to MAUI, I've noticed a big drop in productivity. Despite the fact that MAUI was relased two years ago, it feels a very beta (almost alpha) version of the product that Microsoft promised. Is two, three or four steps behind the obsolete version of Xamarin.Forms.