簡體   English   中英

執行一個小時后出現System.OutOfMemoryException

[英]System.OutOfMemoryException while after execution for an hour

為什么我得到這個“ System.OutOfMemoryException

此代碼提供了從智能卡讀取器讀取智能卡並將結果放在任何應用程序中都集中的文本框中的服務。 當它運行(超過60分鍾)時,出現錯誤提示。 我該怎么辦?

請指教

class Program
    {
        static bool infinity = true;
        static WinSCard sCard;

        public static void Main(string[] args)
        {
            try
            {
                sCard = new WinSCard();

                while (infinity) {

                    sCard.EstablishContext();                   //establis smart card reader resourete manager
                    sCard.ListReaders();                        //get list of smart card reader

                    string readerName = sCard.ReaderNames[0];   //the first smart card reader in the list

                    sCard.WaitForCardPresent(readerName);       //block execution until smart card was attached to the reader
                    sCard.Connect(readerName);                  //connect to a smart card

//                  Console.WriteLine( "ATR: 0x" + sCard.AtrString );


                    byte[] cmdApdu = { 0xFF, 0xCA, 0x00, 0x00, 00 };    // get card UID ...
                    byte[] respApdu = new byte[6];
                    int respLength = respApdu.Length;

                    //submit command to smart card and get a return result
                    sCard.Transmit(cmdApdu, cmdApdu.Length, respApdu, ref respLength);

                    //Convert Bute[] to HexString and finally Integer
                    int num = Int32.Parse(ByteArrayToString(respApdu, respLength), System.Globalization.NumberStyles.HexNumber);
                    Console.WriteLine("GET CARD UID ==> " + num);

                    //Send Result to any input that currently got focus
                    SendKeys.SendWait(num.ToString());

                    sCard.WaitForCardRemoval(readerName);   //block execution until smart card was removed to the reader
                    sCard.Disconnect();                     //terminated connection to a smart card
                    sCard.ReleaseContext();                 //freely any resource allocated under the context
                }
            }
            catch (WinSCardException ex)
            {
                Console.WriteLine( ex.WinSCardFunctionName + " 0x" + ex.Status.ToString( "X08" ) + " " + ex.Message );
            }
            finally
            {
                Console.WriteLine("Please press any key...");
                Console.ReadLine();
            }
        }

        public static string ByteArrayToString(byte[] ba, int length)
        {
            StringBuilder hex = new StringBuilder(length * 2);
            foreach (byte b in ba)
                hex.AppendFormat("{0:x2}", b);
            return hex.ToString().Replace("9000","");
        }
    }

您的代碼中是否有任何實現IDisposable類,而您沒有處置它們,或者它們的任何PInvoke調用,它們需要其他調用來清理由它們分配的任何內存。

我的猜測是sCard.ListReaders()會引起您的問題,因此我將在此處詳細分析代碼。 我假設有一些內部集合永遠不會清除,並且您的sCard是靜態變量。 您可以調試/編寫單元測試,也可以使用內存分析工具(例如ANTS Memory Profiler)(創建初始快照,然后在10分鍾后比較哪個實例消耗了您的內存)

這可能有助於避免在循環中重復使用WinSCard對象。 只需在每個開始時創建一個新實例。 而且您不需要將此對象放入靜態成員,也可以將其用作局部變量。

    public static void Main(string[] args)
    {
        try
        {
            while (infinity) {

                var sCard = new WinSCard();

                sCard.EstablishContext();                   //establis smart card reader resourete manager
                sCard.ListReaders();                        //get list of smart card reader

如果WinSCard實現了IDisposable則將其包裝在using語句中,以確保釋放可能不再使用的資源:

                using(var sCard = new WinSCard())
                {
                    sCard.EstablishContext();                   //establis smart card reader resourete manager
                    sCard.ListReaders();                        //get list of smart card reader

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM