memory leak in Timer controls, and thread sleep

G

Guest

Hi,

We have find memory leaks when we tried to use timer to raise event at
specified
interval. The OS we use is: Windows XP Professional Version 2002, service
Pack 2,
IDE is: Visual Studio .NET2003. Microsoft .NET Framework 1.1, version
1.1.4322 SP1. To repeat this problem,

1. creat a VB windows solution
2. on the form, add a timer control, set the property interval to 100
3. add a button, name it "start"
4. add an event handler( command_click) to the button "start"
5. in the event handler, start the timer
6. watch in the "task manager", memory is prograssively increase,
7. to make this problem more clear, add another timer, set the interval to
10, again, when the button "start" clicks, start both the timer. this time
you probably will
notice the memory leaking faster, in 10 minutes, you will find it is leaking
around 1MB.


I tried to change the timer control to System.timers.timer, and use call
back functions at the specified interval, I have the same problem. Further
investigation into the memory leaking, we found if you put the thread into
sleep at certain amount of time, the memory leaks as well.

Is ther any one who knows why? This memory leak crashes our application.
I tried to force garbage colletion, things getting even worse.

Any suggestions are welcome.

Lihui
 
C

Chris Dunaway

I followed your steps and when I click the button to start the timer, I
see memory go up but it levels off after a short increase. I see this
in Task Manager and also in PerfMon. I don't see a leak on my machine.
If I minimize the app and restore, the memory goes back down,
presumably because Windows trims the working set at that time.

My system is XP but without SP2
VS.Net 2003
 
G

Guest

Chris,

thanks for your reply.
How many timers are you using? I tested again, using two timers, one
interval is
set to 10 ms, one is set to 100 ms. in 10 seconds, the memory usage has
increased from 15,208K to 15, 324K. If I minimize and restore, the memory
goes down to 3,180K, and start to grow again.

In my vb apps, I use a UI to accept user input, stored in an array, when
user click a button, it starts two timers, one is to send out the the input
to the server simulating incoming data, the other timer used to tell me how
many packages have been send out so far. It is a very simple program, but I
found out after the simulator send out around 8000 packages, it sends out
junks.

I wish I can debug into this and know why the data in the array has been
changed after sending 8000 identical data. Since I can only found out my
memory usage is increasing, and this increasing didn't change even if I
comment out all my code. I suspect that it has something to do with the
Timer.

Do you have any debugging clue to this?

very much thanks
 
W

Wayne

When watching memory in Taskmanager you need to make sure to turn on the
Virtual Memory Size. The Mem Usage that is there by default is just the
Physical Memory, when you minimize an app windows swaps out the physical
memory to the swap file. It is assumed that if you minimized the application
that you will not be using it so the physical memory is freed for other
applications.

When you restore the app and start using it, windows then starts swapping
memory from the swap file back into physical ram. This would be why you see
the count going up again. As for PerfMon I would also suggest making sure
you are not just monitoring physical memory.

--
Thanks
Wayne Sepega
Jacksonville, Fl


"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
P

Pete Davis

In addition to the other comments, keep in mind the garbage collection can
happen any time. If objects are created and go out of scope, the memory
isn't reclaimed right away. It's cleaned up whenever the garbage collector
decides it's time. Sometimes that can be a long time. I've had some
situations where the garbage collection had to be forced to keep the app
from eating up too much ram (though there have only been one or two times
that was the case).

You would be much better off using a memory profiler like this one:

http://www.scitech.se/memprofiler/

It will give you a much better pictures of what's going on with memory in
your app.

Pete
 
G

Guest

Chris,

Here is some more interesting findings, I run my app, when the app starts,
it uses memory 15,528K, then I min and restored it, memory goes down to
4,980K, min/restore again, goes to 2,040K.But then memory usage keeps on
growing each time by 8K, Remember I told you I can only send out packages
around 8K, this time I am testing, when I see memory goes above 5MB, I do a
min/restore, the memory goes down to below 2MB, I then can keep my app going
after 20, 000+, I terminated on purpose. Do you know what is going on?

Lily
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top