C# - Application in Xamarin causing slowness after some time

Asked

Viewed 87 times

1

Hello, All right?

I am creating an application in Xamarin Forms, however I am having some problems of slowness. In this application, I use a lot of parallel processing, and the application works well to a certain extent, but the more it is used, the slower it gets (it opens and closes continuously, it is one of the USB events). The Application needs to remain open indefinitely, that is to say after a 9 hours it already begins to present problems.

I make a connection to the Android USB, and transfer information continuously. To make this transfer, I use a recursive Task. It was one of the ways I found to do serial data transfer without slowing down the application.

I use USING whenever I can and always am giving Dipose on some element when it is possible. After some modifications the application is faster, but I’m afraid that this situation remains happening after a while.

My question is this. Should I release the resources of an asynchronous task? I took a close look, and all I found is that it is not necessary to release the resources of the Task, that the Garbage Collector itself makes this work.

In a very cool explanation of Maniero, what is said that I should not use GC.Collect() if I do not know very well how it works, and I honestly do not know as well the tool.

Garbage Collector

Considering all this, I clean the lists that I go through in the tasks that I create, and the operation of the application has improved about 100%, probably if I give a Dispose in the Task, following this same hypothesis, should increase the performance as well. I did not bother to empty the lists/objects I had in these tasks, precisely because it was to be used and when finishing would be cleaned what apparently is not happening.

How would you advise me to follow up on this? It follows a part of the code, which I identified that causes slowness.

 public async void SendCommands()
    {
        Config.Started = true; // Verifica se a Tarefa começou
        List<Command> NCommands = new List<Command>(); // Cria uma Lista dos comandos que serão enviados para a porta serial
        List<string> files = Directory.GetFiles(folder + "/Commands/").ToList(); 
        if (files.Count > 0)
            foreach (string f in files)
            {
                try
                {
                    /*
                     Lê o arquivo e deserializa o JSON. Verifica se o arquivo está em uso, se não estiver prossegue e deleta o arquivo.
                     */
                    FileInfo file = new FileInfo(f);
                    Command C = OnDeserializeC(f);
                    NCommands.Add(C);
                    while (IsFileLocked(file))
                    {
                        await Task.Delay(1);
                    }
                    try
                    {
                        if (file.Exists == true)
                            file.Delete();

                    }
                    catch (Exception ex)
                    {

                        Log.Info("SBI>", ex.Message);
                    }
                }
                catch (Exception e)
                {
                    Log.Error("SBI", e.Message);
                }
            }

        if (NCommands.Count > 0)
        {
            /*
             Ordena os comandos a serem enviados.
             */
            NCommands.OrderBy(c => c.commandPriority);
            foreach (Command C in NCommands.ToList())
            {
                if (Config.USB)
                {
                    if (SendCommand(C))
                    {
                        try
                        {
                            NCommands.Remove(C);
                        }
                        catch (Exception e)
                        {
                            Log.Info("SBI", "Comandos não enviados");
                        }
                    }
                }
                else
                {
                    Log.Info("SBI", "Comandos não enviados");
                }
                await Task.Delay(525);

            }
        }

        await Task.Delay(200);
        Config.Started = false; // Se a Tarefa finalizou, pode iniciar uma nova Tarefa. Faço dessa maneira, pois 
        //toda vez que o programa inicia, ele inicia essa mesma tarefa, isso impede que uma Tarefa seja criada mais de uma vez
        if (Config.USB && !Config.Started)
            Task.Factory.StartNew(() => SendCommands());
        NCommands.Clear(); //Apago os itens da lista, antes de adicionar //essa parte, o aplicativo estava ficando lento ao passar do tempo.
        files.Clear(); 

Thanks!

  • There it is not said that you should not use GC if you do not understand well about it, even because you are using GC always (including in Android GC is up to another), should not use the GC.Collect(). But you need to identify the slowness, it may have nothing to do with memory, in the current form there is no way to answer the question, the only way is to take your application and test to find the problem.

  • I added the part of the code that causes the slowness in the application I’m developing. Also, I made the part of Garbage Collector that I mentioned clearer, because I was using GC.Collect with Waitfinalizers, but after I read that part there, I’ll probably remove it. Thanks for the quick response!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.