Solved

vb timer seem to increase mem usage, how can that be stopped?

Posted on 2008-06-17
16
309 Views
Last Modified: 2012-06-22
Hi. I am trying to read the color of a pixel on the computerscreen every 0.1 seconds. The problem i have is that the mem usage (seen in Windows Task Manager / processes) increases rapidly (especially when the screencolor changes) and after a while i get a system.outofmemory exception
Is there any way of clearing cache memory that is stacked or maybe someone has an even smarter solution.

Try for example this code:
Public Class Form1
    Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
    Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim testcolor As Integer
        testcolor = GetPixel(GetDC(0), 100, 100)
        Button1.BackColor = System.Drawing.ColorTranslator.FromOle(testcolor)
    End Sub
 
End Class

Open in new window

0
Comment
Question by:soriega
  • 6
  • 4
  • 3
  • +1
16 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 21807382
I can't imagine VB being fast enough to read it that quickly. (is this VB or Vb.NET?)

Also I think GetDC may take a while and you have to ReleaseDC?

You are probably getting out of memory for not doing a ReleaseDC.

You also may be reentering the Tick event before the prev event is processed.  Maybe a boolean flag that exits the tick event when set so that it doesn't process any more until it finishes the current one.

Here is an FAQ from Bob Powell on doing it in Vb.NET:

http://www.bobpowell.net/eyedropper.htm

0
 
LVL 21

Expert Comment

by:mastoo
ID: 21807426
Shouldn't you bet loading GetDC(0) into a variable and calling ReleaseDC on each timer tick?
0
 

Author Comment

by:soriega
ID: 21807508
OK thanks. I am not to experienced on using "releaseDC" , do you have a code snipplet maybe?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 21

Expert Comment

by:mastoo
ID: 21807626
If I take the shortcut of omitting error handling as an exercise for the student...

Public Class Form1
    Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
    Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
    Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Integer, ByVal iDc As Integer) As Integer
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim testcolor As Integer
        Dim iDc as integer = GetDC(0)
        testcolor = GetPixel(iDc, 100, 100)
        ReleaseDC( 0, iDc)
        Button1.BackColor = System.Drawing.ColorTranslator.FromOle(testcolor)
    End Sub
 
End Class
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21807648
Try this out:
    Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As IntPtr) As IntPtr
    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As IntPtr, ByVal hdc As Integer) As Integer
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim desktopHwnd As IntPtr = GetDesktopWindow()
        Dim desktopHdc As IntPtr = GetWindowDC(desktopHwnd)
        Dim testcolor As Integer = GetPixel(desktopHdc, 100, 100)
        ReleaseDC(desktopHwnd, desktopHdc)
        Button1.BackColor = System.Drawing.ColorTranslator.FromOle(testcolor)
    End Sub

Open in new window

0
 
LVL 25

Expert Comment

by:SStory
ID: 21807659
GetDC, like many graphic API calls returns a handle.  If you want to release it you need that handle.  I said this in the first post.

However, I wonder if the time that it takes to complete may be longer than 100ms, and if so, it may get called multiple times before the first time completes and that could cause problems.
0
 
LVL 21

Assisted Solution

by:mastoo
mastoo earned 100 total points
ID: 21807830
I don't think any of those calls yields to the message pump, so as long as the forms timer is used (as opposed to the threading timer) you won't get reentrancy.  Having said that, I'll certainly agree the guard variable is a good practice to get into.
0
 

Author Comment

by:soriega
ID: 21807904
Yepp i am aware about the problem that it might be "stacking" of timer things. However, if i increase the timer.interval=3000 it still keeps increaseing mem usage. It seems to increase more every time the color of the pixel changes? I have implemented both these presented solutions, however none of them fixes the problem with increased memory usage.
0
 

Author Comment

by:soriega
ID: 21808115
The easiest way to watch this problem is to open youtube.com and let the application read from a spot where the color changes on the screen. I can se Mem usage increasing slow and steady all the time...
0
 

Author Comment

by:soriega
ID: 21808182

Here is actually a solution to the problem ! Very weird, but this actually decreases the mem usage to a minimum:
            Me.WindowState = FormWindowState.Minimized
            Me.WindowState = FormWindowState.Normal

Not nice and clean, but it works
0
 
LVL 25

Assisted Solution

by:SStory
SStory earned 100 total points
ID: 21812248
Are you calling the ReleaseDC?

Here is an alternate way of getting pixels..a class for C# that I thought you might also find interesting:
http://www.mdibb.net/net/faster_pixel_manipulation_with_getpixel_and_setpixel_in_net/

I would have thought the solution by Idle_Mind would have worked.  Of course we all know that the memory reported in Task Manager is incorrect for DotNet apps, but I wouldn't think API calls made from DotNet would suffer this problem.  Maybe Vista has fixed the issue, but in XP you can't believe the numbers...which appear really huge.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 21812780
The out of memory exception should be solved by the ReleaseDC calls as DC's are a limited resource.

The memory usage decreasing after minimizing the window is indicative that things are working CORRECTLY.  If you had a true memory leak then the app would not return the resources it had used to the operating system.  The garbage collector does not run as often as most people feel it should...supposedly when the operating system as a whole needs memory the .Net framework will release some of its "hogged" resources automatically.

"My app is using huge amounts of memory and/or has a memory leak" is a very common symptom here at EE.  Most of the time it is determined that in fact there is NO memory leak though!
0
 

Author Closing Comment

by:soriega
ID: 31468127
I will try to use the Release thing. Any other things out of these where "release" style should be used?
GetWindowRect
FindWindow
mouse_event
SetWindowPos
EnumWindProc
FindWindowEx
GetWindowText
EnumChildWindows

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21813103
Those APIs are "safe"...  =)
0
 

Author Comment

by:soriega
ID: 21813326
Thanks
0
 
LVL 25

Expert Comment

by:SStory
ID: 21814227
As a follow up to your question about other API's...
I don't see any in that list that should cause you a problem.

Mainly when you get a DC, or  a resource like Bitmap, Fonts or other items to "Draw" with, you select them, store their prev values, and restore that when through.  Much of GDI works like this.  You get it, use it and give it up or you have memory leaks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Formula problem with Excel attachment 6 39
Import a txt file into a DataGridView and TextBox 20 52
HTML - Color not displaying correctly in EMAIL. 6 49
vb.net 1 month apart 11 35
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question