Liberty BASIC Community Forum
« CLS and FLUSH confusion »

Welcome Guest. Please Login or Register.
Jan 22nd, 2018, 5:57pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1 2 3  Notify Send Topic Print
 veryhotthread  Author  Topic: CLS and FLUSH confusion  (Read 845 times)
Brandon Parker
Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1123
xx Re: CLS and FLUSH confusion
« Reply #30 on: Aug 23rd, 2017, 1:08pm »

Here is some code that you can add to your program to periodically retrieve memory attributes. Right now it is set to return the Pagefile Usage, but you can change things around if you want.

Here is a link to the GetProcessMemoryInfo function at MSDN.

This is what I use to monitor my program; returns Pagefile Usage in MB ....

Code:
Struct ProcessMemoryCounters, cb                         As ulong,_
                              PageFaultCount             As ulong,_
                              PeakWorkingSetSize         As ulong,_
                              WorkingSetSize             As ulong,_
                              QuotaPeakPagedPoolUsage    As ulong,_
                              QuotaPagedPoolUsage        As ulong,_
                              QuotaPeakNonPagedPoolUsage As ulong,_
                              QuotaNonPagedPoolUsage     As ulong,_
                              PagefileUsage              As ulong,_
                              PeakPagefileUsage          As ulong

Open "psapi.dll" For DLL As #psapi

Print GetUsedMemory(ErrorMsg$)
Print ErrorMsg$

Close #psapi
End


    Function GetUsedMemory(ByRef ErrorMsg$)
        'returns the memory used by the current process
        'returns -1 if an error is encountered
        'the error message is stored in the submitted variable
        ErrorMsg$ = ""
        ProcessMemoryCounters.cb.struct = len(ProcessMemoryCounters.struct)
        CallDLL #kernel32, "GetCurrentProcess", hProcess As ulong
        cb = len(ProcessMemoryCounters.struct)
        CallDLL #psapi, "GetProcessMemoryInfo", hProcess              As ulong,_
                                                ProcessMemoryCounters As struct,_
                                                cb                    As ulong,_
                                                result                As long
        If result Then
            GetUsedMemory = Val(Using("####.####", (ProcessMemoryCounters.PagefileUsage.struct/1024/1024)))
        Else
            ErrorMsg$ = GetLastError$(ErrorMsg$)
            GetUsedMemory = -1
        End If
    End Function

'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________

    Function GetLastError$(Message$)
        dwMessageID = GetLastError()
        dwFlags = _FORMAT_MESSAGE_FROM_SYSTEM
        nSize = 1024
        lpBuffer$ = Space$(nSize) + chr$(0)
        CallDLL #kernel32, "FormatMessageA", dwFlags      As ulong, _
                                             lpSource     As ulong, _
                                             dwMessageID  As ulong, _
                                             dwLanguageID As ulong, _
                                             lpBuffer$    As ptr, _
                                             nSize        As ulong, _
                                             Arguments    As ulong, _
                                             result       As ulong
        GetLastError$ = Left$(lpBuffer$, result)
    End Function

'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________

    Function GetLastError()
        CallDLL #kernel32, "GetLastError", GetLastError As ulong
    End Function 



{:0)

Brandon Parker

« Last Edit: Aug 24th, 2017, 08:38am by Brandon Parker » User IP Logged

Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5618
xx Re: CLS and FLUSH confusion
« Reply #31 on: Aug 24th, 2017, 02:45am »

Yes, use what Brandon offers. Windows is incredibly lazy about recovering memory from programs. If you clear an array that does not mean that the memory space it was using will be released. It it will remain in the pool assigned to Liberty until Windows is looking for memory for another process. So the memory use you see in Task Manager will change in fits and starts as the Windows garbage recovery dictates.

So you should only be worried if there is an ever increasing use of memory.

I think you should be skeptical of what your memory management program is reporting.

But we don't even know it is a memory issue. You need to slow down the changes you are making and run more often till it fails and report what errors are occurring and where in your program. Run in debug mode, if it is not a crash to the desktop you will see what line of code it falters on. If this is a different line every time that tells us something.
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 187
xx Re: CLS and FLUSH confusion
« Reply #32 on: Aug 24th, 2017, 08:52am »

I wish I could post some pertinent code, and realize that descriptions are a poor substitute, but there are just too many large sections of code that all have to work together to make the program work at all. Sticking with generalities is the best I can do until I've isolated things down to a specific portion that can be posted. At least I think the crashing on large number of sims has something to do with storing new data in an array, periodically dumping to HDD, and repeating. That still doesn't answer why it crashes at all or some of the error log entries, but I'm working on it.

I guess a generic question might be: What's a good way to store sim data that can be as small as 4 digits or over a million and that can be interrupted at any time?
User IP Logged

Brandon Parker
Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1123
xx Re: CLS and FLUSH confusion
« Reply #33 on: Aug 24th, 2017, 09:32am »

How large is the program?

In the event that you could share the entire program I could grab some time over the weekend to dig into it and see if I can help find the problem.


{:0)

Brandon Parker
User IP Logged

Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 187
xx Re: CLS and FLUSH confusion
« Reply #34 on: Aug 24th, 2017, 1:36pm »

Brandon: Thanks for the offer. PM sent.
User IP Logged

Pages: 1 2 3  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls