Speed issues with ReaderWriterLockSlim and Garbage Collection
I ha开发者_高级运维ve an example piece of code the illustrates issues in my code when GC.Collect is carried out on a class having a ReaderWriterLockSlim member variable. The GC.Collect takes between 2 and 3 seconds to run. I need to carry out GC at regular intervals because my applicaton is extremely memory intensive.
namespace WpfApplication12
{
public class DataItem
{
private readonly ReaderWriterLockSlim m_propertyLock = new ReaderWriterLockSlim();
public DataItem()
{
}
}
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<DataItem> dataItemList = new List<DataItem>();
for (int i = 0; i < 100000; i++)
{
dataItemList.Add(new DataItem());
}
Debug.WriteLine(DateTime.Now.ToString());
GC.Collect();
Debug.WriteLine(DateTime.Now.ToString());
}
}
}
Has anyone had similar problems?
Thanks Ian
I'd ask if you really need a ReaderWriterLockSlim
for each of your DataItem
classes?
Seems like bad design to me to have that many handles floating about. After-all, that's what will be causing the delay...
The memory issue may be caused if the readerwriterlockslim is called from multiple threads. I believe it will store some information of the threads which can cause the memory consumption to bloat. I would recommend trying to figure out a solution where you can bring down the number of thread that are calling the readerwriterlockslim.
精彩评论