2

Today I've discovered by myself a strange thing experimenting things about the memory consumption, I can't find any documentation of this anywhere, but sure all developer experts knows about of what I want to talk here.

The thing is... When you compile a default WinForms in VB or also C#, when you move the mouse over the form, that action causes to increase the memory consumption to about 8-16 kb per second...

The most important thing is that memory will never be collected/freed!

So the longer you move the mouse over the form, the more RAM consumption will be generated and never will come down, thereby possibly causing an StackOverFlow error, and that's the main reason of my preoccupation...

I have a WinForms application where it needs to stay running over hours and the mouse need to be moved over the app from one point to another each second, so I need to perform a way to avoid this strange memory consumption problem which can produce itself an stackoverflow error.

I've tested the same thing in a Java application and the thing goes dramatical!, if you move the mouse over an empty window then you can see how the memory consumption increases MB0s per second! ...instead the few KB's per second like in VB/C#, and like in VB/C# Form that memory never goes down, there's no way back, it's True what the people says about Java and the memory consumption of that language...I think it sucks.

Then to make the same test on another language I've chosen C++ 'cause is the other one important, I don't have any C/C++ IDE to compile so what I did is to choose some official programs I have made in C/C++ like for example "Winamp" and this time the result is... DOES NOT HAPPENS ANYTHING WHEN MOVING THE MOUSE OVER THE C/C++ APPS! The memory consumption does not increase, absolutely Zero increase.

I've made this experiment with a default Windows Forms application (Empty Form1.vb Class), in C# and in VB, but I work only with VB. I've used .Net Framework 4.0 and 4.5. In Windows 8 x64.

Some expert developer can help me to understand all of this paranormal things?

· Why the memory goes up between 8-16 kb each second when moving the mouse in a VB/C# WinForm.

· Why that increase of memory never goes down again?

· Why the same problem does not happens in C/C++ apps? (I can understand C++ does not have the same engine (Framework) but anyways... I don't know if that's the reason.)

And the most important question...

· I can prevent that memory increase when moving the mouse over the form?, maybe overriding some native methods or...I don't know...exist a way to avoid it?

UPDATE:

The way how I've measured the memory consumption is just simply as seeying the memory in TaskManager.exe

The reason why I said "The memory never be collected" is because when moving the mouse over the form, the memory counter does not go down on taskmanager, never.

UPDATE 2

I uploaded a video explaining the problem, you can see it with your own eyes! ...I'm not crazy.

http://www.youtube.com/watch?v=sBxicL_x9HQ&feature=youtu.be

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • 1
    Please add information on how you measure memory usage and what is reasoning of your "that memory will never be collected" claim. – Alexei Levenkov Aug 08 '13 at 17:22
  • It's **memory consumption** - not *consumition* like you keep using over and over again – marc_s Aug 08 '13 at 17:22
  • @marc_s I did the correct modification just after posting, sorry my native language is not english. – ElektroStudios Aug 08 '13 at 17:23
  • http://blogs.msdn.com/b/oldnewthing/archive/2010/08/09/10047586.aspx – dtb Aug 08 '13 at 17:25
  • @Alexei Levenkov I added that specifications, thanks for comment. – ElektroStudios Aug 08 '13 at 17:26
  • 1
    just dont look at memory consumption; it will level out. Many many winforms apps run for weeks / months / years(?) with no problem – pm100 Aug 08 '13 at 17:27
  • 2
    'never' hasn't happened yet. You gave up too soon. – H H Aug 08 '13 at 17:28
  • @Henk Holterman Whats the problem?, You understanded it... never is never ...this time never is under a reasonable time lapsus of hours. – ElektroStudios Aug 08 '13 at 17:39
  • 1
    The problem is that strong claims need strong evidence. You should have researched it a little. This site is full of imaginary memory problems. But no exact dupe, I didn't close-vote. – H H Aug 08 '13 at 17:42
  • What thing do you expect I need to research if I don't know the root of the problem and also I don't know why that strange problem exists itselfs? you can contribute something about my questions, if you know something about that, thanks. – ElektroStudios Aug 08 '13 at 18:09
  • That's how .net works, memory is not cleared right away (like say c++, when done right). It clears the memory with it needs to. For fun, minimize the application and look at the memory. Also, check out user7116 link. – the_lotus Aug 08 '13 at 18:56

1 Answers1

8

Why the memory goes up between 8-16 kb each second when moving the mouse in a VB/C# WinForm.

There are messages that trigger for handling mouse movement, etc, which get processed by the form.

Why that increase of memory never goes down again?

It will. Eventually, you'll see your memory settle down. In C# and VB.Net, the garbage collector doesn't immediately clean up memory (by design), but lets it grow, and will clean up as needed. In general, you'll tend to see .NET applications grow in their memory usage, then drop dramatically, then grow again, then drop, etc. If you have a lot of memory in your system, the "drops" happen infrequently, since a garbage collection is expensive, and there's absolutely no disadvantage to using memory that's not needed elsewhere.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks for your response but I want to cite this part "It will. Eventually,", I know it will in the major cases, really I think it will in all cases ...until I did this experiment, in this case the GC will not drop down the consumption, please if someone is interested in do the same experiment he can see it by himself. Just pick a new form, compile it, and move the mouse over the form, see how the memory will grow up, and see how it won't go down "never", minutes, hours, days, never. I need to avoid that... – ElektroStudios Aug 08 '13 at 17:34
  • @ElektroHacker - then post the Out-Of-Memory exception with full details. No exception = no problem. – H H Aug 08 '13 at 17:44
  • 1
    @Henk Holterman A good programmer ever will take precautions to manage all possible errors, and that can be a reasonable problem for a PC with low memory capacity, I need to avoid it. – ElektroStudios Aug 08 '13 at 17:49
  • 1
    Yes, and you still haven't proven that there is a problem (even on a low mem system). I say there isn't one. – H H Aug 08 '13 at 17:52
  • 1
    @ElektroHacker If you're running on a 64bit system with a lot of memory, there's no need for it to collect until you're getting low on memory. "eventually" can be a VERY long time, and that's a good thing - using memory is a good feature, not a problem. Cleaning up memory is expensive - it only needs to happen if the memory is needed elsewhere. – Reed Copsey Aug 08 '13 at 17:55
  • 1
    @ElektroHacker as far as I know, while `GC` has still not collected the memory, if your code need more memory, all the memory which is still pending to be collected will be reused, that means there will not any more memory required. The memory may not be seen collected by another application but it is seen collected by your application and is always available (for your appliation). – King King Aug 08 '13 at 18:00
  • 1
    @ElectroHacker. Try it out on a low memory computer with a bunch of other high-mem processes running. I bet that the GC will do it's work before you experience issues. – APrough Aug 08 '13 at 18:13
  • @APough Then my next experiment will be this and I will comment it when I can realice it: 1. Compile a empty form and manually increase the mem usage moving the mouse to about the double mem usage when the Form was fully loaded. 2. Open a couple of high-mem processes as photoshop trying to get any type of mem exception or simply to see what happens with the mem usage of my compiled app. thnkyou all for the comments – ElektroStudios Aug 08 '13 at 18:21