[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

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

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
soriega
Asked:
soriega
  • 6
  • 4
  • 3
  • +1
3 Solutions
 
SStoryCommented:
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
 
mastooCommented:
Shouldn't you bet loading GetDC(0) into a variable and calling ReleaseDC on each timer tick?
0
 
soriegaAuthor Commented:
OK thanks. I am not to experienced on using "releaseDC" , do you have a code snipplet maybe?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mastooCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
SStoryCommented:
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
 
mastooCommented:
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
 
soriegaAuthor Commented:
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
 
soriegaAuthor Commented:
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
 
soriegaAuthor Commented:

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
 
SStoryCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
soriegaAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Those APIs are "safe"...  =)
0
 
soriegaAuthor Commented:
Thanks
0
 
SStoryCommented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now