How big of NEW() can I safely do

In Discuss.SV softvelocity.clarion.third_party - Get memory used by a process - @jslarve asked the question: I need a “How big of NEW() can I safely do right now without blowing
up”.

I took my Memory Status example and added a Find Max button. I started coding with VirtualAlloc() but decided it would be much simpler to use NEW() since it fails nicely by returning NULL. Started with the Available Virtual Memory and decremented by 1 MB until it worked. So a brute force search, nothing elegant but it is fast.

This Memory Status window shows 1790 MB of available VM. Click the “Find Max” button and it finds NEW() works with 1284 MB, so that’s the largest segment.

Before running the above SysInternals VMMap showed the largest free block was 1,315,904 K / 1024 = 1285 MB so that matches my result.

When the Find Max window closes I rerun GlobalMemoryStatus and show that Available memory went down 1286 MB. Not exactly 1284 but there must be some overhead. Also note that Used Virtual went from 257.77 MB to 1544.38 MB which is 1286.61 MB so about right.

Refreshing VMMap shows Private Bytes went from 2312 K to 1,316,904 K,


If I run Find Max again it can get only 125 MB more. That’s a little more than the last VMMAP largest block of 122,688 K / 1024 = 120 K

Note after the NEW works a Message opens with the option to DISPOSE that new string. You can then return to the memory status and see the effect on VM. In my tests it was usually Zero.


4GB Large Memory

If you rename MemMaxCheck_Remove_For_4GB_.EXP to MemMaxCheck.EXP then LARGE_ADDRESS will be active in the EXP. Build and run and the Process will have 3.75 available VM.

The maximum NEW you can get is 2036 MB leaving 1.75 GB available. If you run again you can get another 1255 MB segment, run again gets 137 MB. These amounts will be less for a program of size.


Below I attached the test project I made MemMaxCheck that is based on my GitHub LargeAddressMemoryStatus Example. This gives you a way to test your own logic.

I don’t think there is a need to use GlobalMemoryStatus() to get available memory, just start at 2000 MB and decrement down until New works. An alternative that does not commit memory to your process is to scan all memory using VirtualQuery() against every block as is done by VMMap.

Jeff for reading CSV I think you can just NEW a String the size of the file and see if fails by checking &= NULL. Your project should specify LARGE_ADDRESS so it can get almost a 2GB segment plus have still more. One caution is those memory addresses over 2GB in a LONG will be negative. Some code may assume a negative address is bad.

HowBigOfNewCanWeDo.zip (23.6 KB)

6 Likes

Very interesting, Carl. I really appreciate that you took the time to do this.