Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Test Maximum Memory
#1
This code tries to assign a large arrays of memory..

If you have an upper range of memory, say, 32GB with 8GB free this may work:

In QB45 the "Out of memory" (error 7) would be displayed.
However, in QB64 the memory box will be displayed.

This the memory mem.h file:

Code: (Select All)
//mem.h memory function library.
#include<windows.h>
#include<stdio.h>
#include<tchar.h>

uint64 MemInUsePercent();
uint64 TotalPhysicalMem ();
uint64 FreePhysicalMem ();
uint64 TotalPagingFile ();
uint64 FreePagingFile ();
uint64 TotalVirtualMem ();
uint64 FreeVirtualMem ();
uint64 FreeExtendedMem ();

static float CalculateCPULoad();
static unsigned long long FileTimeToInt64();
float GetCPULoad();

uint64 MemInUsePercent () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return  statex.dwMemoryLoad;
}

uint64 TotalPhysicalMem () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullTotalPhys;
}

uint64 FreePhysicalMem () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullAvailPhys;
}

uint64 TotalPagingFile () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullTotalPageFile;
}

uint64 FreePagingFile () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullAvailPageFile;
}

uint64 TotalVirtualMem () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullTotalVirtual;
}

uint64 FreeVirtualMem () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullAvailVirtual;
}

uint64 FreeExtendedMem () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullAvailExtendedVirtual;
}

static float CalculateCPULoad(unsigned long long idleTicks, unsigned long long totalTicks)
{
    static unsigned long long _previousTotalTicks = 0;
    static unsigned long long _previousIdleTicks = 0;

    unsigned long long totalTicksSinceLastTime = totalTicks - _previousTotalTicks;
    unsigned long long idleTicksSinceLastTime = idleTicks - _previousIdleTicks;

    float ret = 1.0f - ((totalTicksSinceLastTime > 0) ? ((float)idleTicksSinceLastTime) / totalTicksSinceLastTime : 0);

    _previousTotalTicks = totalTicks;
    _previousIdleTicks = idleTicks;
    return ret;
}

static unsigned long long FileTimeToInt64(const FILETIME & ft)
{
    return (((unsigned long long)(ft.dwHighDateTime)) << 32) | ((unsigned long long)ft.dwLowDateTime);
}

float GetCPULoad()
{
    FILETIME idleTime, kernelTime, userTime;
    return GetSystemTimes(&idleTime, &kernelTime, &userTime) ? CalculateCPULoad(FileTimeToInt64(idleTime), FileTimeToInt64(kernelTime) + FileTimeToInt64(userTime)) : -1.0f;
}

This is the program:
Code: (Select All)
Rem $Dynamic
Declare Library "mem"
  Function FreePhysicalMem~&&
End Declare
Dim Memory2 As _Unsigned _Integer64
Dim Requested As _Unsigned _Integer64
_ScreenMove _Middle
Memory2 = FreePhysicalMem
Color 15
Print "Free Physical Memory:";
Print Using "###,###,###,###"; Memory2

x = 1024: y = 1024: z = 1024 ' array values
l = 2 ' length of int
Requested = 3 * x * y * z * l
Print "Requested Memory:";
Print Using "###,###,###,###"; Requested
If Requested > Memory2 Then
  Color 12
  Print "Not enough memory."
  Color 7
  End
End If
Color 14: Print "Requested memory array."
Dim z(x, y, z) As Integer
Dim z1(x, y, z) As Integer
Dim z2(x, y, z) As Integer
Color 14: Print "Requested memory assigned."
For i = 1 To 10
  For j = 1 To 10
      For k = 1 To 10
        z(i, j, k) = i * j * k
      Next
  Next
Next
Erase z, z1, z2
Color 7
End
Reply


Messages In This Thread
Test Maximum Memory - by eoredson - 04-26-2025, 04:01 AM
RE: Test Maximum Memory - by mdijkens - 04-26-2025, 09:22 AM
RE: Test Maximum Memory - by eoredson - 04-28-2025, 05:50 AM
RE: Test Maximum Memory - by DSMan195276 - 04-29-2025, 03:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Recall2 - a memory improvement game PhilOfPerth 15 3,038 04-06-2023, 11:17 PM
Last Post: PhilOfPerth
  Recall - a memory - test game PhilOfPerth 6 1,703 05-07-2022, 02:58 AM
Last Post: PhilOfPerth

Forum Jump:


Users browsing this thread: 1 Guest(s)