Application causes freeze/slowness after using for some time

Ankit Saxena 0 Reputation points
2024-09-11T03:54:14.4966667+00:00

Hi,

We have migrated our app into MAUI. In Android devices or even in emulator release mode if I use the application for some time it starts getting slow and eventually freezes or crashes. It generally happens when I click on a button, and some operation happens and redirects to another screen. I tried GC.Collect which prevents application crash but make it app slow.

And OnAppearing of the next page where it should redirects having this below code

`protected override void OnAppearing()
  {
      try
      {
          base.OnAppearing();
          ViewModel?.OnAppearing();
          SetupListeners();
          GC.Collect();
          GC.WaitForPendingFinalizers();
      }
      catch (Exception ex)
      {
          ReportError("AssignItemPage.OnAppearing: Failed.", ex);
          DisplayErrorAlert(ex.Message);
      }
  }`
`private async Task ProcesRecord()
{
    Stopwatch watch = new Stopwatch();
    try
    {
        watch.Start();
        if (_qtyRemain > 0)
        {
            Reset();
            return;
        }
        IsBusy = true;
        var dtSave = new DataTable("item_loc");
        dtSave.Columns.Add("id", typeof(string));       

        if (_isInd)
        {
            dtSave.Columns.Add("code", typeof(string));
            
        }

        var dtHold = new List<Test>();
        if (!_isInd)
        {
            foreach (var lvItem in Results)
            {
                if (!(lvItem.qty > 0)) continue;
                var dr = dtSave.NewRow();
                dr["id"] = _itemId; 
                dtSave.Rows.Add(dr);
            }
        }
        else
        {
            var results = (string.IsNullOrEmpty(_Id)) ? Results.ToList() : ItemList;
            foreach (var lvItem in results ?? [])
            {
                if (string.IsNullOrEmpty(Id) && !(lvItem.qty_assigned > 0)) continue;
                var dr = dtSave.NewRow();
                dr["id"] = lvItem.item_id.Trim();
               
             }
        var dsSave = new DataSet("item_loc");
        dsSave.Tables.Add(dtSave);
        dsSave.AcceptChanges();    
        
        dsSave.Dispose();
        dtSave.Dispose();
        dsSave = null;
        dtSave = null;
        var msg = await new ApiController().UpdateItem(model); 
        if (msg?.ToLowerInvariant() != "true")
        {
            string? errorMesg = msg?.Substring(msg.IndexOf("Message:")).Replace("Message:", "");
            if (errorMesg != null)
                await DisplayAlert("Item", errorMesg.Trim(), "Ok");
        }
        else
        {

            switch (_itemType)
            {
                case Type.Item:
                    {
                        var frm = new ItemTabbedPage(_itemType);
                        await GoTo(frm);
                        break;
                    }

                case Type.Accepted:
                    {
                        if (_parentForm != null && _parentForm == "Receive")
                        {
                            var frm = new ReceivePage();
                            await GoTo(frm);
                        }
                        else
                        {
                            var navigation = Application.Current?.MainPage?.Navigation;
                            if (navigation?.NavigationStack[navigation.NavigationStack.Count - 2] is AssignPage previousPage)
                            {
                                navigation.RemovePage(previousPage);
                            }

                            var tabbedPage = new CVAwayPage();
                            if (tabbedPage.Children[0] is CVReadyPage cvReadyPage)
                            {
                                if (_sendSeaBack)
                                {
                                    tabbedPage.Children[0] = new CVReadyPage(_itemId);  
                                }
                                else
                                {
                                    tabbedPage.Children[0] = new CVReadyPage();
                                }
                            }
                            await Application.Current!.MainPage!.Navigation.PushAsync(tabbedPage);                  
                        }

                        break;
                    }
               
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
    catch (Exception ex)
    {
        await DisplayErrorAlert(ex.Message);
        ReportError("Items: Failed.", ex);
    }
    finally
    {
        if (watch.IsRunning)
        {
          
            watch.Stop();
            string paramvalue = "Test";
            string count = Results != null ? Results.Count.ToString() : "0";
            int success = await InsertExecutionLogDetails("Items", paramvalue, count, watch.ElapsedMilliseconds.ToString());
           
        }
    }
}`
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,514 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.