Bursting of Memory (RELEASE)

Asked

Viewed 493 times

0

I’m having a little problem with my code... is eating a Braba memory... and is not pouring memory ... a simple application within my program... Label printing... every time I print a label the code takes the ID and generates a QR on the label... but it’s eating a lot of memory and I’m not getting to dump it ... any tips ? follows the Cod...

using MessagingToolkit.QRCode.Codec;
using System.Drawing;
using System.IO;

    public class QRCode
        {
            //guarda imagem QR em byte[]
            public static byte[] imgQR { get; set; }

            public static bool GerarQR(Invoices Invoice)
            {
                //tenta gerar o QR
                try
                {
                    QRCodeEncoder qrCodecEncoder = new QRCodeEncoder();
                    qrCodecEncoder.QRCodeBackgroundColor = System.Drawing.Color.White;
                    qrCodecEncoder.QRCodeForegroundColor = System.Drawing.Color.Black;
                    qrCodecEncoder.CharacterSet = "UTF-8";
                    qrCodecEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                    qrCodecEncoder.QRCodeScale = 6;
                    qrCodecEncoder.QRCodeVersion = 0;
                    qrCodecEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;

                    Image imageQRCode;
                    //string a ser gerada
                    string data = "Invoice: " + Invoice.InvoicesID.ToString(); // QR gerado pelo ID do Invoice
                    imageQRCode = qrCodecEncoder.Encode(data);

                    //converte a imagem QR em byte[]
                    MemoryStream ms = new MemoryStream();
                    imageQRCode.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                    imgQR = ms.ToArray();
                    ms.Dispose();

                    return true;
                }
                catch
                {
                    return false;
                }
            }        
        }

Here is the imprint that calls the class to generate the QR

[Description("Imprime as etiquetas referente a nota fiscal.")]
        public static bool PrintLabels(List<Invoices> ListInvoices)
        {
            // Tenta fazer
            try
            {
                // Abre tela para escolher impressora
                System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();

                // Se foi selecionado uma impressora
                if (printDialog.ShowDialog() != true)
                {
                    // Retorna como falso
                    return false;
                }

                // Cria um nova impressao
                Prints print = new Prints(Prints.PaperType.Labels);
                // Para cada nota fiscal da lista
                foreach (Invoices Invoice in ListInvoices)
                {
                    // Para cada volume da nota fiscal
                    for (int i = 1; i <= Invoice.Volume; i++)
                    {
                        //gera o código QR
                        QRCode.GerarQR(Invoice);

                        #region Temp Impressoras argox não recebem a altura do papel por algum motivo
                        // Se for argox
                        if (printDialog.PrintQueue.FullName.Contains("Argox"))
                        {
                            // Recebe o tamanho do documento
                            print.fixedDocument.DocumentPaginator.PageSize = new Size(455, 1125);

                            // Adiciona o controle da impressora
                            print.AddControl(new Controls.Labels(Invoice, i, Invoice.Volume), new Thickness(1, 1, 0, 0));
                        }
                        #endregion
                        else
                        {
                            // Coloca uma margina na folha
                            print.PageElements.Margin = new Thickness(15, 0, 0, 0);

                            // Variavel para posicoes dos elementos
                            Thickness Margin = new Thickness(0, 0, 0, 0);

                            // Adiciona imagem
                            print.AddImage(Margin, Invoice.Affiliate.Picture, 65, 150);

                            // Salta um espaco
                            Margin.Left += 156;

                            // Adiciona texto
                            print.AddText(Invoice.Affiliate.Name.Trim(), Margin, 14, true, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 14;

                            // Adiciona texto
                            print.AddText(Invoice.Affiliate.Address.Street.Trim(), Margin, 11, false, HorizontalAlignment.Left);

                            //Salta um espaço
                            Margin.Top += 11;

                            // Adiciona texto
                            print.AddText(Invoice.Affiliate.Address.District.Trim() + " - " + Invoice.Affiliate.Phone1, Margin, 11, false, HorizontalAlignment.Left);

                            //Salta um espaço
                            Margin.Top += 11;

                            // Adiciona texto
                            print.AddText(Invoice.Affiliate.Address.City.FullName + " - " + Invoice.Affiliate.Address.CEP, Margin, 11, false, HorizontalAlignment.Left);

                            //Salta um espaço
                            Margin.Top = 67;
                            Margin.Left = 176;

                            // Adiciona texto
                            print.AddText("VOLUMES", Margin, 11, false, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 11;

                            // Adiciona texto
                            print.AddText(i.ToString("000") + "/" + Invoice.Volume.ToString(), Margin, 15, true, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 15;

                            // Adiciona texto
                            print.AddText("NOTA FISCAL", Margin, 11, false, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 10;

                            // Adiciona texto
                            print.AddText(Invoice.Number.ToString(), Margin, 15, true, HorizontalAlignment.Left);

                            // Define posicao x e y
                            Margin.Top = 0;
                            Margin.Left = 0;

                            // Salta um espaço
                            Margin.Top = 67;

                            #region Dados do remetente, destinatario e destino

                            // Adiciona texto
                            print.AddText("REMETENTE", Margin, 11, false, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 10;

                            // Adiciona texto
                            print.AddText(Invoice.Sender.Name.Trim(), new Thickness(Margin.Left, Margin.Top, 210, Margin.Bottom), 12, true, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 12;

                            // Adiciona texto
                            print.AddText("DESTINATÁRIO", Margin, 11, false, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 10;

                            // Adiciona texto
                            print.AddText(Invoice.Recipient.Name.Trim(), new Thickness(Margin.Left, Margin.Top, 210, Margin.Bottom), 12, true, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 12;

                            // Adiciona texto
                            print.AddText("DESTINO", Margin, 11, false, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 10;

                            // Adiciona texto
                            print.AddText(Invoice.DeliveryAddress.City.FullName, Margin, 12, true, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 12;

                            // Adiciona texto
                            print.AddText("COLETOR", Margin, 11, false, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 10;

                            // Adiciona texto
                            print.AddText(Invoice.PeopleName, Margin, 12, true, HorizontalAlignment.Left);

                            // Volta uma linha
                            Margin.Top -= 12;
                            #endregion

                            // Define posicao x e y
                            Margin.Top = 45;
                            Margin.Left = 240;

                            // Se não achou a filial
                            if (Invoice.DeliveryAddress.City.Affiliate == null)
                            {
                                // Retorna erro
                                throw new Exception("Cidade destino não atendida.");
                            }

                            // Adiciona texto
                            print.AddText(Invoice.DeliveryAddress.City.FullAbbreviation, Margin, 70, true, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 77;

                            // Adiciona texto
                            print.AddText("SETOR", Margin, 11, false, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 10;

                            // Adiciona texto
                            print.AddText(Invoice.Sector.ToString(), Margin, 20, true, HorizontalAlignment.Left);

                            // Salta um espaço
                            Margin.Top += 77;

                            // Define posicao x e y
                            Margin.Top = 0;
                            Margin.Left = 0;

                            // Salta um espaço
                            Margin.Top += 130;
                            Margin.Left += 300;

                            // Adiciona QR na etiqueta
                            print.AddImage(Margin, QRCode.imgQR, 50, 50);

                        }

                        // Adiciona a pagina
                        print.AddPage();
                    }
                }

                // Manda imprimir
                printDialog.PrintDocument(print.fixedDocument.DocumentPaginator, "Imprimir");

                // Retorna como feito
                return true;
            }
            // Pega o erro
            catch (Exception Error)
            {
                // Mostra mensagem de erro
                webTR.Controls.CustomWindows.ShowMessage(Error.Message);

                // Retorna como nao feito
                return false;
            }
        }

PS I’m guessing is my DLL...

  • Mediu? are you sure this is the part that is consuming memory? Do you think the impression is causing or the generation? The QRCodeEncoder allocates resources? Are you generating an exception? This code really doesn’t look robust to handle problems. There are cases where consumption is normal.

  • every time I ask to print the label it will allocate memory and not pour after printing... and it will grow causing locking... when I use for 1 or 2 labels there is no difference anymore when it comes out 1000 labels at once the memory craves an absurd... I made a test printing 10, 10, 10,10,10,10,10,10,10,10,10 and the memory is unstable... more when I ask to print 1000 at once it bursts... no exception appears... more I see in the debug eating the memory

  • I was able to solve it... when I had many labels printed at once, it burst in my memory... but when I asked him to print it again, he cleaned the memory cache so he could use it for a new batch of prints...

  • I was able to solve it... when I had many labels printed at once, it burst in my memory... but when I asked him to print it again, he cleaned the memory cache so he could use it for a new batch of prints

  • Something tells me this is not a good solution, whatever it is, for me it doesn’t even answer the question.

No answers

Browser other questions tagged

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