How to free memory every time we open and close the company via integration engines

1

Good morning,

My scenario is as follows. I notice that in a webservice that I have to use the integration engines, with each invocation in which I do AbreCompany and DateCompany the memory is rarely released. I know there will be a way to streamline this question, "using the Marshal." This scenario was spoken in a Spring training, as I was suggested to raise the question here by the trainer Sergio Sereno. Here's the order. Tks.

So here's a source code excerpt, though I think it's the same as many others. The negative rating was due to what specifically?

 var motor = new ErpBS();
            try
            {
                motor.AbreEmpresaTrabalho(EnumTipoPlataforma.tpProfissional, credenciaisPrimavera.Empresa, credenciaisPrimavera.Login, credenciaisPrimavera.Password);

                GcpBEDocumentoInterno docInternoPrimavera =TransformaDocumentoInterno(motor, documento);
                string erro = String.Empty;
                motor.Comercial.Internos.Actualiza(docInternoPrimavera);
            }
            catch (Exception e)
            {
                log.Error("InserirDocumento failed", e);
                resultado.Estado = new EstadoOperacao(CodigoEstado.OperacaoFalhou, e.Message);
            }
            finally
            {
              if(motor!= null)  motor.FechaEmpresaTrabalho();
            }
    
asked by anonymous 11.06.2018 / 12:46

2 answers

0

The code required is this:

        /// <summary>
        /// Release all the resources.
        /// </summary>
        public static void Termina()
        {

            if (motor!= null)
            {
                motor.FechaEmpresaTrabalho();
                FinalizeConnector(motor);
            }

        }

        #endregion

        #region Private Methods
        /// <summary>
        /// Finalizes the connector.
        /// </summary>
        private static void FinalizeConnector(dynamic ObjCom)
        {
            try
            {
                if (ObjCom != null)
                {
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(ObjCom);
                    ObjCom = null;
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        #endregion

    }
}
    
11.06.2018 / 13:17
1

Here are some lines of code that have helped me to free up SQL memory space when these situations occur:

motor.Consulta("use PRI" + Empresa + "
 DBCC FREESESSIONCACHE
 DBCC FREEPROCCACHE
 DBCC DROPCLEANBUFFERS
")
    
23.11.2018 / 07:26