I have a C# application which controls and monitors a PMAC controller. This application should be able to run for a few days at least, but I see that its memory usage would increase all the time. At first the memory usage is about 230M, and it will increase with 25M each hour.
I have found that memory does not increase unless I call the ReadDataFromDPRam()
method.
I call this method 10msec intervals.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct PmacServoRead
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)MotorNo.All)]
public float[] CurPosition; // M4000,
[MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)MotorNo.All)]
public float[] CurVelocity;
public int AmpEnable;
public int PosLimit;
public int NegLimit;
public int HomeComp;
public int DesiredVelocityZero;
...
}
[DllImport("Pcomm32.dll")]
public static extern IntPtr PmacDPRGetMem(UInt32 dwDevice, UInt32 offset, UInt32 count, IntPtr val);
public IntPtr GetDpramMemory(UInt32 devNo, UInt32 offset, UInt32 count, IntPtr val)
{
return PmacDPRGetMem(devNo, offset, count, val);
}
private PmacServoRead m_PmacServoRead = new PmacServoRead();
private PmacInput m_PmacInput = new PmacInput();
int m_ReadSize = Marshal.SizeOf(typeof(PmacServoRead));
int m_ReadSizeDi = Marshal.SizeOf(typeof(PmacInput));
private int ReadDataFromDPRam()
{
if (IsOpen == true)
{
IntPtr ptr1 = Marshal.AllocHGlobal(m_ReadSize);
GetDpramMemory(DeviceNo, CcpPmacComm.DpramReadOffset, (uint)m_ReadSize, ptr1);
m_PmacServoRead =(PmacServoRead)Marshal.PtrToStructure(ptr1, typeof(PmacServoRead));
Marshal.DestroyStructure(ptr1, typeof(PmacServoRead));
Marshal.FreeHGlobal(ptr1);
ptr1 = Marshal.AllocHGlobal(m_ReadSizeDi);
GetDpramMemory(DeviceNo, CcpPmacComm.DpramInputReadOffset, (uint)m_ReadSizeDi, ptr1);
m_PmacInput = (PmacInput)Marshal.PtrToStructure(ptr1, typeof(PmacInput));
Marshal.DestroyStructure(ptr1, typeof(PmacInput));
Marshal.FreeHGlobal(ptr1);
}
return -1;
}
Please help me.