Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Runtime error 7 - Out of memory

Status
Not open for further replies.

mrdance

Programmer
Joined
Apr 17, 2001
Messages
308
Location
SE
How does this problem occur? I get this error when trying to open a form when I have to many windows open. This problem seems to occur more often on windows 98. What kind of memory are we talking about and how can you prevent this?

thanks

--- - Internet Solutions ---
 
Hi Mr. Dance:

There are many causes to Error 7 - Out of Memory. These include:
1. Uncontrolled recursion.
2. Dimensioning arrays too large.
3. Too many instances of classes.
4. Forgetting to release memory resources when done with them.

During the testing phase of software development, one should use a performance monitor, such as perfmon.exe that comes with the .NET framework, to monitor memory allocations. (In the really early stages of testing, I use Resource Meter that comes with Windows. It's not great, but gives me a quick indication of major problems.)

Cassandra
 
Another method I use for Windows 98 programs is the following:

Code:
Public Const GFSR_SYSTEMRESOURCES = 0
Public Const GFSR_GDIRESOURCES = 1
Public Const GFSR_USERRESOURCES = 2

Public Declare Function GetFreeResources Lib "RSRC32" _
    Alias "_MyGetFreeSystemResources32@4" (ByVal lWhat As Long) As Long

'----------------

Private Function MemoryCheck() As Boolean

    If Not m_blnWindows98 Then
        MemoryCheck = False
        Exit Function
    End If

    MemoryCheck = True
    
    If GetFreeResources(GFSR_SYSTEMRESOURCES) < 20 Then
        MsgBox " Windows' ""Free System Resources"" have dropped " & _
            vbCrLf & " below 20 per cent. " & vbCrLf & _
            vbCrLf & " MyProgram cannot open another record. Please close " & _
            vbCrLf & " one or more records or another Windows program before " & _
            vbCrLf & " opening another record.", _
            vbExclamation, "MyProgram Windows 98 Resource Tracker"
        Exit Function
    End If
    
    If GetFreeResources(GFSR_GDIRESOURCES) < 20 Then
        MsgBox " Windows' ""Free Graphical Device Interface (GDI) Resources"" " & _
            vbCrLf & " have dropped below 20 per cent. " & vbCrLf & _
            vbCrLf & " MyProgram cannot open another record. Please close " & _
            vbCrLf & " one or more records or another Windows program before " & _
            vbCrLf & " opening another record.", _
            vbExclamation, "MyProgram Windows 98 Resource Tracker"
        Exit Function
    End If
    
    If GetFreeResources(GFSR_USERRESOURCES) < 20 Then
        MsgBox " Windows' ""Free User Resources"" have dropped " & _
            vbCrLf & " below 20 per cent. " & vbCrLf & _
            vbCrLf & " MyProgram cannot open another record. Please close " & _
            vbCrLf & " one or more records or another Windows program before " & _
            vbCrLf & " opening another record.", _
            vbExclamation, "MyProgram Windows 98 Resource Tracker"
        Exit Function
    End If

    MemoryCheck = False

End Function

The function returns false if there is more memory allocatable. It returns true if memory resources drops too low. The calling subroutine can avoid trying to allocate more memory if there is a shortage of memory.

Cassandra
 
Oops! Sorry but I missed two lines of code:
Code:
Private m_blnWindows98 as Boolean ' In form's module variables.

'---------------------
'In Form_Load Sub

    m_blnWindows98 = (osvWindows.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS) And _
        ((osvWindows.dwMajorVersion > 4) Or _
        ((osvWindows.dwMajorVersion = 4) And (osvWindows.dwMinorVersion > 0)))

Cassandra
[blush]
 
If you are a VB programmer, then, under Form_Unload of your main form, remembering to set objects and forms to nothing and closing all files you've opened may help:

Set objects = Nothing
Close MyFile#, or, Close All
Set Myform = Nothing

Everytime you run your VB projects without doing this puts more and more memory out of use, until the system buckles.

Hope this helps,

Ortho


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top