I have a windows forms app running on a remote server. When I first start this app, the memory usage on task manager was 9876 KB.This app copies screen shots for every 1 minute. When it starts copying screen shots, the memory usage increases to 23993 KB. How can I avoid memory usage? It seems like memory usage will increase much more.
Best Regards.
Here is my code sample:
...//Get Memory Usage int memKB = (int)System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1024; Microsoft.Win32.SystemEvents.SessionSwitch += new
Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch); if (flag) { String time1 = DateTime.Now.ToString("HH:mm:ss"); Bitmap b = new Bitmap(ScreenWidth, ScreenHeight); g = Graphics.FromImage(b); g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size); time1 = time1.Replace(':', '_'); if (flag_Credentials) { IntPtr token = IntPtr.Zero; bool isSuccess = LogonUser("User", "Domain", "Pass", LOGON32_LOGON_NEW_CREDENTIALS,
LOGON32_PROVIDER_DEFAULT, ref token); if (isSuccess) { using (WindowsImpersonationContext person = new WindowsIdentity(token).Impersonate()) { b.Save(@"\\IP\logs\shot" + name + "_" + time1 + "__" + memKB.ToString() + ".jpg",
System.Drawing.Imaging.ImageFormat.Jpeg); g.Dispose(); person.Undo(); } } } else { b.Save(@"\\IP\logs\shot" + name + "_" + time1 + "__" + memKB.ToString() + ".jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose(); } }
...
VulpesPosted Mar 9, 2012, 5:04 AM
VulpesPosted Mar 9, 2012, 11:27 AM
RaysefoPosted Mar 9, 2012, 6:52 AM
I also add your code to some other places and now memory is around 11000 KB
RaysefoPosted Mar 9, 2012, 6:08 AM
When I add the code you suggested, memory consumption was 9980 KB at first and after the first screen shot it increases to 20000 KB then after couple of minutes it was 21400 KB. Seems like stable but I am not sure.