none
Insufficient memory to continue the execution of the program RRS feed

  • Question

  • Hello,

    I keep getting those 2 errors in my win form application. The application works for about 5-10 minutes without the error. After that, those errors appears every 3 seconds all the time:

    System.OutOfMemoryException: Insufficient memory to continue the execution of the program.

    System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.

    The error points to those 2 rows in the code: (objectLIST in next piece of code contains around 150.000 elements in total). 

    So what I do is serializing those 150.000 lines to send them with SendMessage to other winform applications. The serialization happens about every 3 seconds over and over again:

    objects = Convert.ToBase64String(ms.ToArray());
    cds.lpData = Marshal.StringToHGlobalAnsi(objects);

    When the error occurs, below is the system use: (Computer has 24 cores. Application is compiled in 64-bit)

    Threads: 2180
    Processes: 114
    Total RAM 32 GB
    Avaliable: 23 GB
    Free: 16 GB

    Visual Studio: 1.4 GB
    20 winform instances totals: 1 GB

    More code can be seen below. First I don't know if this really is the base problem or anything else in the application and how it is possible to find what causes the memory exceptions?

    private const int WM_COPYDATA = 0x004A; [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref COPYDATASTRUCT lParam); [StructLayout(LayoutKind.Sequential)] public struct COPYDATASTRUCT { public IntPtr dwData; public Int32 cbData; public IntPtr lpData; } //Serialize String objects = "";

    List<object> objectLIST = new List<object>(); //Contains lists. Totals 150.000 elements using (var ms = new MemoryStream()) { var bf = new BinaryFormatter(); bf.Serialize(ms, objectLIST); objects = Convert.ToBase64String(ms.ToArray()); //System.OutOfMemoryException' was thrown } //Send COPYDATASTRUCT cds; cds.dwData = IntPtr.Zero; cds.lpData = Marshal.StringToHGlobalAnsi(objects); //Insufficient memory to continue the execution of the program cds.cbData = objects.Length; //now send the RF_TESTMESSAGE to the running instance SendMessage(p.MainWindowHandle, WM_COPYDATA, IntPtr.Zero, ref cds);








    • Edited by Silvers2 Tuesday, March 29, 2016 9:22 PM
    Tuesday, March 29, 2016 8:50 PM

Answers

  • Try getting rid of the base 64 stuff. I told you in a previous thread that it isn't needed, serialize basically produces a byte array, just send the byte array without converting it to base 64.
    • Marked as answer by Silvers2 Thursday, March 31, 2016 12:59 AM
    Wednesday, March 30, 2016 4:42 AM
    Moderator

All replies

  • Try getting rid of the base 64 stuff. I told you in a previous thread that it isn't needed, serialize basically produces a byte array, just send the byte array without converting it to base 64.
    • Marked as answer by Silvers2 Thursday, March 31, 2016 12:59 AM
    Wednesday, March 30, 2016 4:42 AM
    Moderator
  • Yes, I think I missed it in the other thread. So you are right, I did take that away and 

    it seems to work out now.

    Thank you for your help!

    Thursday, March 31, 2016 12:59 AM