Hi,
Here is my problem:
- I am using Visual Studio 2013 with Update 4 on a Windows 7 SP 1 x64 machine
- I have this simple Windows Forms application whose source code is given below (Target Framework = .NET Framework 4, Platform = AnyCPU)
- whichever value I sent for "enabled" (true or false) in App.Config (also below), garbage collection always occur on a separate thread.
Now, the documentation of "gcConcurrent" says:
"Specifies whether the common language runtime runs garbage collection on a separate thread."
I probably misunderstand the meaning of this sentence, or it does not apply to my configuration, but I thought garbage collection would happen on the main thread.
What did I do or undestand wrong ?
The reason I am asking is because I have .NET wrapper classes written in mixed C++ (wrapping native C++ classes) and a multi-threaded GC causes multi-threading problems due to the fact that native destructors can perform non trivial actions which can go really wrong if the main thread also performs conflicting actions at the same time (and I have read somewhere that, during GC, threads are suspended only once they reach managed code).
Chris.
---------------------------------
Source code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace NoConcurrentGC
{
public partial class Form1 : Form
{
static int m_mainThreadId;
public Form1()
{
InitializeComponent();
m_mainThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
byte value = 0;
while (true)
{
Memory m = new Memory(value);
value = (byte)(value + 1);
}
}
private class Memory: IDisposable
{
private byte[] data = new byte[10000];
public void Dispose()
{
}
~Memory()
{
if (System.Threading.Thread.CurrentThread.ManagedThreadId != m_mainThreadId)
{
MessageBox.Show("Collection invoked on separate thread");
}
}
public Memory(byte value)
{
for (int n = 0; n < data.Length; n++) data[n] = value;
}
}
}
}
--------------------------------------
App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<gcConcurrent enabled="false"/>
</runtime>
</configuration>