Forum Xamarin.Forms
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.

Xamarin.form gradually slows down

Techno_DevelopTechno_Develop Member ✭✭✭
edited October 26 in Xamarin.Forms

Xamarin.form is slowly slowing down.
For example, if you repeat camera startup → shooting, the operation will slow down the 3rd to 4th time, and the application will not work.

Is there any way to improve it?
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/deploy-test/performance
I tried ↑, but it doesn't improve!

Best Answer

Answers

  • Techno_DevelopTechno_Develop Member ✭✭✭

    Xamarin forms: Ver.4.6.0.1180

    try
    {
    
        await Plugin.Media.CrossMedia.Current.Initialize();
    
        if (!Plugin.Media.CrossMedia.Current.IsCameraAvailable || !Plugin.Media.CrossMedia.Current.IsTakePhotoSupported)
        {
            return;
        }
    
        DateTime dt = DateTime.Now;
        string result = dt.ToString("yyyyMMddHHmmss");
        result += ".jpg";
    
        var file = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            Name = result
    
        });
    
        if (file == null)
            return;
    
        var bytes = new Queue<byte>();
        using (var s = file.GetStream())
        {
            var length = s.Length;
            int b;
            while ((b = s.ReadByte()) != -1)
                bytes.Enqueue((byte)b);
        }
    
        File.Copy(file.Path, csreadwrite.ReadPath + csreadwrite.ReadDataPath + csreadwrite.ReadChikuPath + "/Pic");
    
    }
    catch (Exception ex)
    {
    }
    
  • JohnHardmanJohnHardman GBUniversity admin

    @Techno_Develop

    I don't use that media plugin, but I would assume that you need to dispose of file once it is no longer required.

    I'm assuming the code that you posted is incomplete as well, as bytes doesn't seem to be used for anything.

  • Techno_DevelopTechno_Develop Member ✭✭✭
    edited October 27

    After shooting with the camera,

    var config = new LoadingConfig();
    Configurations.LoadingConfig = config;
    Loading.Instance.Show("…");
    

    (AiForms) is performed to register other data.

    Is it the cause of the slow Loading.Instance.Show ("…") ;?

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    I tried with Plugin.Media.CrossMedia and xamarin.forms 4.6.0.1141. Here is runnning gif.

    It is running normally, So you issue is related to the following part. If you execute the I/O operation in a short time without release the stream, it will slows down

     var bytes = new Queue<byte>();
        using (var s = file.GetStream())
        {
            var length = s.Length;
            int b;
            while ((b = s.ReadByte()) != -1)
                bytes.Enqueue((byte)b);
        }
    
        File.Copy(file.Path, csreadwrite.ReadPath + csreadwrite.ReadDataPath + csreadwrite.ReadChikuPath + "/Pic");
    
  • JohnHardmanJohnHardman GBUniversity admin

    @LeonLu - The stream is wrapped in a using.

    @Techno_Develop - have you tried disposing file (you can use using) and removing the code relating to bytes, as per my previous post?

  • Techno_DevelopTechno_Develop Member ✭✭✭

    @JohnHardman

    try
    {
    
        await Plugin.Media.CrossMedia.Current.Initialize();
    
        if (!Plugin.Media.CrossMedia.Current.IsCameraAvailable || !Plugin.Media.CrossMedia.Current.IsTakePhotoSupported)
        {
            return;
        }
    
        DateTime dt = DateTime.Now;
        string result = dt.ToString("yyyyMMddHHmmss");
        result += ".jpg";
    
        var file = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            Name = result
    
        });
    
        if (file == null)
            return;
    
        File.Copy(file.Path, csreadwrite.ReadPath + csreadwrite.ReadDataPath + csreadwrite.ReadChikuPath + "/Pic");
    
        file.Dispose();
    
    }
    catch (Exception ex)
    {
    }
    
  • JohnHardmanJohnHardman GBUniversity admin

    @Techno_Develop said:
    @JohnHardman

    try
    {
    
        await Plugin.Media.CrossMedia.Current.Initialize();
    
        if (!Plugin.Media.CrossMedia.Current.IsCameraAvailable || !Plugin.Media.CrossMedia.Current.IsTakePhotoSupported)
        {
            return;
        }
    
        DateTime dt = DateTime.Now;
        string result = dt.ToString("yyyyMMddHHmmss");
        result += ".jpg";
    
        var file = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            Name = result
    
        });
    
        if (file == null)
            return;
    
        File.Copy(file.Path, csreadwrite.ReadPath + csreadwrite.ReadDataPath + csreadwrite.ReadChikuPath + "/Pic");
      
        file.Dispose();
      
    }
    catch (Exception ex)
    {
    }
    

    Has that made any difference regarding the slow down? If not, use the Stopwatch class to confirm that it is something in this code that is slowing down when used repeatedly (and if it is, which bit).

    BTW - Is it necessary to call await Plugin.Media.CrossMedia.Current.Initialize(); each time this code is executed, or is that a one-time only operation?

    Also, I'm assuming that no exceptions are being thrown. Is that the case?

Sign In or Register to comment.