What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Memory Useage In VB

Pages: (2): « First [ 1 ] 2 » Last »
Memory Useage In VB
Author: Message:
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. Huh?  Memory Useage In VB
I'm making a program in VB that will display the netstats of your computer. Every time it updates it stores the information in memory. Which every 5 seconds raises the program memery useage by 24bytes. I no it may seem like not much but when this program is ment to run when you start your computer and run in the background, 24 bytes adds up. So I need to know how to clear the memory of the information I've stored in it. Heres my code that adds the info in the memory.

code:
Public Sub RefreshStack()
Dim i As Long
    pDataRef = 0

For i = 0 To nRows '// read 24 bytes at a time
    CopyMemory nState, ByVal pTablePtr + (pDataRef + 4), 4
    CopyMemory nLocalAddr, ByVal pTablePtr + (pDataRef + 8), 4
    CopyMemory nLocalPort, ByVal pTablePtr + (pDataRef + 12), 4
    CopyMemory nRemoteAddr, ByVal pTablePtr + (pDataRef + 16), 4
    CopyMemory nRemotePort, ByVal pTablePtr + (pDataRef + 20), 4
    CopyMemory nProcId, ByVal pTablePtr + (pDataRef + 24), 4

    DoEvents

        'If nRemoteAddr <> 0 Or nRemotePort <> 0 Or nLocalPort <> 0 Then
            Connection(i).State = nState
            Connection(i).LocalHost = nLocalAddr
            Connection(i).LocalPort = nLocalPort
            Connection(i).RemoteHost = nRemoteAddr
            Connection(i).RemotePort = nRemotePort
            Connection(i).ProcessID = nProcId
            Connection(i).ProcessName = GetProcessName(nProcId)
           
        'End If
        pDataRef = pDataRef + 24

        DoEvents
        Next i
       


End Sub


Now how do I clear or remove the data so I can keep my memory free for other programs to use. And so it don't hog all the memory.

Thanks!

This post was edited on 08-29-2004 at 07:18 AM by DJeX.
[Image: top.gif]
08-29-2004 07:17 AM
Profile PM Web Find Quote Report
theRabbit83
New Member
*

Avatar

Posts: 4
40 / Male / –
Joined: Aug 2004
RE: Memory Useage In VB
Are you sure that code keeps adding up memory usage? I may be wrong but it seems that you are storing your info on the connection vector, and each time you are reusing it by going trough it from the 0 position to nRows... what memory (from wich variables) do you want to free?
[Image: pedro.tiago.png]
08-29-2004 08:28 AM
Profile E-Mail PM Web Find Quote Report
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. RE: Memory Useage In VB
No it adds 24 bytes ever time it udates. I can even see it when i go into the task manager and look at the programs mem useage.
[Image: top.gif]
08-29-2004 07:40 PM
Profile PM Web Find Quote Report
Choli
Elite Member
*****

Avatar
Choli

Posts: 4714
Reputation: 42
42 / Male / Flag
Joined: Jan 2003
RE: Memory Useage In VB
quote:
Originally posted by DJeX
it adds 24 bytes ever time it udates. I can even see it when i go into the task manager and look at the programs mem useage.
first of all: The values of memory usage shown in the task manager doesn't represent the real memory used by the programs, so don't worry very much about it. Are you sure it adds 24 bytes every update and forever? or does it stop at some value?



Well, about the code, I don't see anything wrong, however you don't posted all the code (in fact, I'm more interested in all the declarations of those variables and functions). First of all: Are you sure you need the calls to copymemory? i don't know where those pTablePtr and pDataRef come from, however maybe you could use a direct assignment.

And second thing: Again this is a supposition (because i don't know the declaration of the variables). What about using VarPrt? I mean:
CopyMemory VarPrt(nState), ByVal pTablePtr + (pDataRef + 4), 4
CopyMemory VarPrt(nLocalAddr), ByVal pTablePtr + (pDataRef + 8), 4
etc...
Well, if the code works, I suppose it's fine as you wrote it. (Also, at the first CopyMemory call, is it "pDataRef + 4" or just pDataRef? (and similar for other calls))
Messenger Plus! en espaņol:
<< http://www.msgpluslive.es/ >>
<< http://foro.msgpluslive.es/ >>
:plus4:
08-29-2004 08:58 PM
Profile PM Find Quote Report
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. RE: Memory Useage In VB
my whole modual

code:
Option Explicit

Type MIB_UDPROW
    dwLocalAddr As String * 4 'address on local computer
    dwLocalPort As String * 4 'port number on local computer
End Type

'Private Type MIB_TCPROW
'    dwState As Long
'    dwLocalAddr As Long
'    dwLocalPort As Long
'    dwRemoteAddr As Long
'    dwRemotePort As Long
'End Type

Type MIB_TCPROW
    dwState As Long        'state of the connection
    dwLocalAddr As String * 4    'address on local computer
    dwLocalPort As String * 4    'port number on local computer
    dwRemoteAddr As String * 4   'address on remote computer
    dwRemotePort As String * 4   'port number on remote computer
End Type

Private Declare Function GetProcessHeap Lib "kernel32" () As Long


Private Declare Function htons Lib "ws2_32.dll" (ByVal dwLong As Long) As Long


Public Declare Function AllocateAndGetTcpExTableFromStack Lib "iphlpapi.dll" (pTcpTableEx As Any, ByVal bOrder As Long, ByVal heap As Long, ByVal zero As Long, ByVal Flags As Long) As Long

Public Declare Function AllocateAndGetUdpExTableFromStack Lib "iphlpapi.dll" (pUdpTableEx As Any, ByVal bOrder As Long, ByVal heap As Long, ByVal zero As Long, ByVal Flags As Long) As Long


Private Declare Function SetTcpEntry Lib "iphlpapi.dll" (pTcpTableEx As MIB_TCPROW) As Long


Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private pTablePtr As Long
    Private pDataRef As Long
    Public nRows As Long
    Public oRows As Long
    Private nCurrentRow As Long
    Private udtRow As MIB_TCPROW
    Private nState As Long
    Private nLocalAddr As Long
    Private nLocalPort As Long
    Private nLocalAddr2 As Long
    Private nLocalPort2 As Long
    Private nRemoteAddr As Long
    Private nRemotePort As Long
    Private nProcId As Long
    Private nProcName As String
    Public nRet As Long

Private Type Connection_
    FileName As String
    ProcessID As Long
    ProcessName As String
    LocalPort As Long
    RemotePort As Long
    LocalHost As Long
    RemoteHost As Long
    State As String
End Type

Public Connection(2000) As Connection_

Public Function GetIPAddress(dwAddr As Long) As String
    Dim arrIpParts(3) As Byte
    CopyMemory arrIpParts(0), dwAddr, 4
    GetIPAddress = CStr(arrIpParts(0)) & "." & _
    CStr(arrIpParts(1)) & "." & _
    CStr(arrIpParts(2)) & "." & _
    CStr(arrIpParts(3))
End Function


Public Function GetPort(ByVal dwPort As Long) As Long
    GetPort = htons(dwPort)
End Function

Public Sub RefreshStack()
Dim i As Long
    pDataRef = 0

For i = 0 To nRows '// read 24 bytes at a time
    CopyMemory nState, ByVal pTablePtr + (pDataRef + 4), 4
    CopyMemory nLocalAddr, ByVal pTablePtr + (pDataRef + 8), 4
    CopyMemory nLocalPort, ByVal pTablePtr + (pDataRef + 12), 4
    CopyMemory nRemoteAddr, ByVal pTablePtr + (pDataRef + 16), 4
    CopyMemory nRemotePort, ByVal pTablePtr + (pDataRef + 20), 4
    CopyMemory nProcId, ByVal pTablePtr + (pDataRef + 24), 4

    DoEvents

        'If nRemoteAddr <> 0 Or nRemotePort <> 0 Or nLocalPort <> 0 Then
            Connection(i).State = nState
            Connection(i).LocalHost = nLocalAddr
            Connection(i).LocalPort = nLocalPort
            Connection(i).RemoteHost = nRemoteAddr
            Connection(i).RemotePort = nRemotePort
            Connection(i).ProcessID = nProcId
            Connection(i).ProcessName = GetProcessName(nProcId)
           
        'End If
        pDataRef = pDataRef + 24

        DoEvents
        Next i
       


End Sub

Public Function GetEntryCount() As Long
    GetEntryCount = nRows - 1 '// The last entry is always an EOF of sorts
End Function
Public Function TerminateThisConnection(rNum As Long) As Boolean
   
    On Error GoTo ErrorTrap
   
    udtRow.dwLocalAddr = Connection(rNum).LocalHost
    udtRow.dwLocalPort = Connection(rNum).LocalPort
    udtRow.dwRemoteAddr = Connection(rNum).RemoteHost
    udtRow.dwRemotePort = Connection(rNum).RemotePort
    udtRow.dwState = 12
    SetTcpEntry udtRow
   
    TerminateThisConnection = True
    Exit Function
   
ErrorTrap:
    TerminateThisConnection = False
End Function

Public Function GetRefreshTCP() As Boolean
    nRet = AllocateAndGetTcpExTableFromStack(pTablePtr, 0, GetProcessHeap, 0, 2)
   
    If nRet = 0 Then
        CopyMemory nRows, ByVal pTablePtr, 4
    Else
        GetRefreshTCP = False
        Exit Function
    End If

    If nRows = 0 Or pTablePtr = 0 Then
    GetRefreshTCP = False
    Exit Function
    End If

   If oRows <> nRows Then
   GetRefreshTCP = True
   Else
   GetRefreshTCP = False
   End If
   
oRows = nRows

End Function
Function d_state(s) As String
  Select Case s
  Case "0": d_state = "UNKNOWN"
  Case "1": d_state = "CLOSED"
  Case "2": d_state = "LISTENING"
  Case "3": d_state = "SYN_SENT"
  Case "4": d_state = "SYN_RCVD"
  Case "5": d_state = "ESTABLISHED"
  Case "6": d_state = "FIN_WAIT1"
  Case "7": d_state = "FIN_WAIT2"
  Case "8": d_state = "CLOSE_WAIT"
  Case "9": d_state = "CLOSING"
  Case "10": d_state = "LAST_ACK"
  Case "11": d_state = "TIME_WAIT"
  Case "12": d_state = "DELETE_TCB"
  End Select
End Function


[Image: top.gif]
08-29-2004 09:04 PM
Profile PM Web Find Quote Report
Choli
Elite Member
*****

Avatar
Choli

Posts: 4714
Reputation: 42
42 / Male / Flag
Joined: Jan 2003
RE: Memory Useage In VB
quote:
Originally posted by DJeX
nRet = AllocateAndGetTcpExTableFromStack(pTablePtr, 0, GetProcessHeap, 0, 2)
Well, I don't know what that function does (in fact now it's the first time that I see it), anyway, I think (reading its name) that it allocates (ie: creates, takes, ...) some memory. That means that once you've called AllocateAndGetTcpExTableFromStack, your program has some more memory used. If after using the table it creates (ie: the pTablePtr) you don't free that memory, it'll sty there forever. So: you call AllocateAndGetTcpExTableFromStack, use your RefreshStack function and then you have to free that memory using some API. Which one? Well, i don't know, but I'm sure it's inside the iphlpapi.dll library. you'll have to search it :)
Messenger Plus! en espaņol:
<< http://www.msgpluslive.es/ >>
<< http://foro.msgpluslive.es/ >>
:plus4:
08-29-2004 09:23 PM
Profile PM Find Quote Report
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. RE: Memory Useage In VB
What if I tryed to use this:

code:
Private Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As Long

Public Sub FreeMemory()
    LocalFree pTablePtr
End Sub



Would that work?
[Image: top.gif]
08-29-2004 11:59 PM
Profile PM Web Find Quote Report
Choli
Elite Member
*****

Avatar
Choli

Posts: 4714
Reputation: 42
42 / Male / Flag
Joined: Jan 2003
RE: Memory Useage In VB
quote:
Originally posted by DJeX
Would that work?
I don't know. Maybe. Note that when you pass the pointer pTablePtr (just allocated by AllocateAndGetTcpExTableFromStack) to LocalFree, that API (LocalFree) has to know how much memory it has to free, and it will only be able to know that if the memory was allocated by another API "relative" to it. I mean, if AllocateAndGetTcpExTableFromStack calls internally some API that LocalFree knows, then yes, you'll be able to use it else you can't.


Anyway, I've searched a bit, and I've found this. Try it. It looks nice, so I think it'll work, but i don't know:

code:
Declare Function HeapFree Lib "kernel32" Alias "HeapFree" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long
Declare Function GetProcessHeap Lib "kernel32" Alias "GetProcessHeap" () As Long

public sub freemem()
HeapFree GetProcessHeap(), 0&, ByVal pTablePtr
' if it doesn't work, try to remove the ByVal keyword
end sub

Messenger Plus! en espaņol:
<< http://www.msgpluslive.es/ >>
<< http://foro.msgpluslive.es/ >>
:plus4:
08-30-2004 09:23 PM
Profile PM Find Quote Report
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. RE: Memory Useage In VB
Works Great! Thanks!
[Image: top.gif]
09-01-2004 05:17 AM
Profile PM Web Find Quote Report
Choli
Elite Member
*****

Avatar
Choli

Posts: 4714
Reputation: 42
42 / Male / Flag
Joined: Jan 2003
RE: Memory Useage In VB
quote:
Originally posted by DJeX
Works Great! Thanks!
no problem ;)
quote:
Originally posted by Choli
HeapFree GetProcessHeap(), 0&, ByVal pTablePtr
' if it doesn't work, try to remove the ByVal keyword
does it work like I posted or did you remove the ByVal?
Messenger Plus! en espaņol:
<< http://www.msgpluslive.es/ >>
<< http://foro.msgpluslive.es/ >>
:plus4:
09-01-2004 08:02 PM
Profile PM Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On