Click here to Skip to main content
15,909,591 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to move a Splitter control? Pin
Donald Blachly10-Dec-02 15:25
Donald Blachly10-Dec-02 15:25 
GeneralRe: How to move a Splitter control? Pin
Bog11-Dec-02 13:23
Bog11-Dec-02 13:23 
GeneralRe: How to move a Splitter control? Pin
Donald Blachly11-Dec-02 18:46
Donald Blachly11-Dec-02 18:46 
AnswerRe: How to move a Splitter control? Pin
Dominique Plante6-Mar-03 7:54
Dominique Plante6-Mar-03 7:54 
GeneralProblems with a memory leak Pin
Omega5017-Dec-02 11:10
Omega5017-Dec-02 11:10 
GeneralRe: Problems with a memory leak Pin
Burt Harris7-Dec-02 15:17
Burt Harris7-Dec-02 15:17 
GeneralRe: Problems with a memory leak Pin
Omega5017-Dec-02 16:27
Omega5017-Dec-02 16:27 
GeneralRe: Problems with a memory leak Pin
Burt Harris8-Dec-02 11:42
Burt Harris8-Dec-02 11:42 
With a call to Process.GetCurrentProcess() inside a timer event routine, I saw working set growth similar to what you described. The trick is every time you call Process.GetCurrentProcess, it's creating a new object, and it's a fairly heavyweight object...

Try moving the call to Process.GetCurrentProcess() outside of your timer event routine. I made it a member of my Form class and memory growth went away completely. Since its not going to change, you could even make it a static member.
static private Process p = Process.GetCurrentProcess()

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
    label3.Text = DateTime.Now.ToString("hh:mm:ss");
    label1.Text = p.PrivateMemorySize.ToString();
    label4.Text = p.WorkingSet.ToString();
}


Creating the Process object just once is the real fix for you, but the other thing to point out is that this object implements IDisposable. Whenever an object implements IDisposable, you'll get much better perormance if you manage it's lifetime, and call Dispose() on it when done. This is so important, the C# language added the using statement just for this.

Ignoring the advise of my second paragraph, I left the Process.GetCurrentProcess inside the timer event routine, but wrote it like this:
using (Process p = Process.GetCurrentProcess()) {
    label1.Text = p.PrivateMemorySize.ToString();
    label4.Text = p.WorkingSet.ToString();
}

Net result: Memory grew a little, but stabilized.

So here's what all this means. Creating a System.Diagnostics.Process directly or indirectly allocates a chunk of unmanaged memory. This unmanaged memory isn't released until either: 1) IDisposable.Dispose is called (implied in my using statement), or 2) the object is garbage collected.

Option 1 (Dispose) is far preferred to option 2, because 2 is far less predictable. But for this paricular case, option 0 (creating the object once and reusing it) is the best.

Burt Harris
GeneralRe: Problems with a memory leak Pin
Omega5019-Dec-02 10:52
Omega5019-Dec-02 10:52 
GeneralRe: Problems with a memory leak Pin
Burt Harris9-Dec-02 21:28
Burt Harris9-Dec-02 21:28 
GeneralRe: Problems with a memory leak Pin
Omega50111-Dec-02 22:14
Omega50111-Dec-02 22:14 
GeneralRe: Problems with a memory leak Pin
David Stone8-Dec-02 5:56
sitebuilderDavid Stone8-Dec-02 5:56 
GeneralRe: Problems with a memory leak Pin
Burt Harris8-Dec-02 10:50
Burt Harris8-Dec-02 10:50 
GeneralRe: Problems with a memory leak Pin
David Stone8-Dec-02 17:09
sitebuilderDavid Stone8-Dec-02 17:09 
GeneralEndDialog Pin
peterchen7-Dec-02 10:02
peterchen7-Dec-02 10:02 
GeneralRe: EndDialog Pin
Burt Harris7-Dec-02 10:13
Burt Harris7-Dec-02 10:13 
GeneralRe: EndDialog Pin
peterchen7-Dec-02 10:47
peterchen7-Dec-02 10:47 
Generalplz ans these Pin
imran_rafique7-Dec-02 9:23
imran_rafique7-Dec-02 9:23 
GeneralRe: plz ans these Pin
Burt Harris7-Dec-02 10:02
Burt Harris7-Dec-02 10:02 
GeneralCustom TextBox Drawing Pin
mikasa7-Dec-02 8:13
mikasa7-Dec-02 8:13 
GeneralRe: Custom TextBox Drawing Pin
Burt Harris7-Dec-02 10:05
Burt Harris7-Dec-02 10:05 
GeneralRe: Custom TextBox Drawing Pin
mikasa9-Dec-02 2:21
mikasa9-Dec-02 2:21 
GeneralInternet Connection Pin
Mazdak7-Dec-02 5:34
Mazdak7-Dec-02 5:34 
GeneralRe: Internet Connection Pin
mikasa7-Dec-02 8:15
mikasa7-Dec-02 8:15 
GeneralRe: Internet Connection Pin
leppie8-Dec-02 2:53
leppie8-Dec-02 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.