How can I compute the necessary memory limit for my Fortran code? - memory-management

I have a Fortran 90 code. I have tried to allocate a r(5000000,5000,3) array with real numbers in my code. After compiling with double precision (I am using gfortran compiler), I received this error:
Operating system error: cannot allocate memory.
Allocation would exceed memory limit
My computer has a 16GB RAM.
What should I do for this problem?
I do not know what to do.

Related

Total amount of static memory on VS2015 solution

I am using VS2015 on a 64 machine, sometimes when I allocate too much static memory I get the error described here. Can I know in advance what is the total amount of static memory allocated in a solution when I don't get an allocation error? (i.e. When the solution successfully compiled ).

What are the limitations with malloc() on a 64 bit process on macOS?

A fellow programmer claims that his 64 bit Mac program keeps running out of memory due to memory fragmentation. I countered that this is not possible, based on the knowledge that the program only allocates about 1-2 TB of memory in total and most allocations being in the range of 40-200 bytes, even if they're in the millions.
I believe it simply is not possible to fragment the 64 bit address space in such a way that an allocation request would fail because the memory allocator cannot find a free gap of the requested size in the address space.
My belief is based on the understanding that nearly the entire address space (64 bit) is available. Maybe it's only 62 bit.
So I wonder if there are other restrictions that could be causing his problems. E.g, is the entire 64 bit address space really available, or is there only a much smaller subset of the address space that can be used for allocations? Or is there a limit on the total number of virtual pages?
Apple's Kernel developer guide does not appear to provide any information on this topic.
I found some hints on limitations in How can a moderately sized memory allocation fail in a 64 bit process on Mac OS X? - but the suggestions such as that there's a total limit around 128 TB were not supported with references. E.g, does this limit refer to a total amount that can be allocated to any process, or is that a total for all processes together, and is that about the allocated total, or is that the address range that can be allocated?
I.e, if 128 TB is the total address range that can be returned by malloc, and if that's one range for all processes, then I could imagine this causing said problems. But is that really it?
The final answer can probably be found in the darwin source code. Has someone a grip on it and can summarize it here?

Problems using GlobalMemoryStatusEX on Win32

I am facing the following problem: I need to find the available memory on my system. GlobalMemoryStatusEX works fine when built on x64. But gives wrong answer when built on Win32. I am using Intel Visual Fortran 2010 on Windows 7 64-bit.
Here is a sample of my code:
program test
use kernel32
use ifwinty
implicit none
type(t_memorystatusex) :: status
integer :: RetVal
status%dwLength = sizeof(status)
RetVal = GlobalMemoryStatusEX(status)
end program test
Thank you very much!
Your program doesn't ever display the values returned, and you don't say what is "wrong", so I don't know what problem you're referring to. Intel Visual Fortran supplies a MemoryStatus example program (in the Win32 sample collection) that uses GlobalMemoryStatusEx. When I run it on my 64-bit system in 32-bit mode it displays:
48% of memory is in use
15.99GB total physical memory
8.28GB available physical memory
31.98GB total pageable memory
24.42GB available pageable memory
2.00GB total virtual memory
1.98GB available virtual memory
and when run in 64-bit mode:
48% of memory is in use
15.99GB total physical memory
8.28GB available physical memory
31.98GB total pageable memory
24.42GB available pageable memory
8192.00GB total virtual memory
8191.99GB available virtual memory
Notice that the only difference is in virtual memory, correctly reflecting the difference between 32-bit and 64-bit mode.
In most cases, you don't need to find the amount of available memory - pretty much every indication you can get is not going to be helpful during run of a program. In particular, available virtual memory doesn't necessarily mean you can allocate a single chunk of data that size, as the memory pool may be somewhat fragmented.
So my answer is that your code, what you showed of it, is the correct way to find out what memory, physical and virtual, is available to Windows. You just need to know how to interpret it correctly.

Executable memory within 32 bits displacement of code area

Writing a JIT compiler in C++ on 64-bit Windows, generated code will sometimes need to call run-time functions that are written in C++. At the moment I'm allocating memory in which to place the generated code with VirtualAlloc(0, bytes, MEM_COMMIT, PAGE_EXECUTE_READWRITE); the last flag is important because allocated memory is not otherwise executable.
VirtualAlloc could presumably return memory anywhere in the 64-bit address space, which is fine for data (of which in general more than 4 gigabytes will be needed, so it does need 64-bit addressing), but the most efficient form of the x64 call instruction wants a 32-bit IP-relative offset, and since the amount of generated code will be less than 4 gigabytes, it would be preferable to locate it within a 32-bit displacement of the code compiled from C++.
Is there a way to arrange this?
You can specify a virtual address near which you want the allocation to happen as the first argument. To increase the chance of getting the allocation within the boundaries you want to could reserve the virtual memory region first and then request for committed memory as and when needed from the reserved space.
Allocation by default happens bottom unless MEM_TOP_DOWN is specified or system is configured to perform memory layout top down to catch pointer truncation problems. Gist is that you can only increase the chance of having allocation within the boundary but should have code to handle when allocation is out of boundary.

memory allocation issues

i have following question
i have RAM 2.5 GB in my computer what i want is if it is possible that in case of allocate totally memory to some process or for example
char * buffer=malloc(2.4GB) , no more process ( google chrome, microsoft games in computer..etc) can run?
Probably not. First, your operating system will have protections ie, malloc eventually becomes a system call in your OS so it will fail instead of killing everything.
Second, because of virtual memory you can have more allocated memory than RAM so that even if your OS were to let you allocate 2.5 gigs it will still be able to function and run processes.
While it is OS and compiler dependent, on Visual C++ under 32 bits windows, you will typically be unable to malloc more than 512MB at a time. This controlled by the preprocessor constant _HEAP_MAXREQ. For details of the approach I used to work around this limitatation, see the following thread If you go to 64 bits, this also ceases to be an issue, although you might end up using much more virtual memory than you would expect.
In a OS like Windows where each process gets a 4GB (assuming 32 bit OS) virtual address space, it doesn't matter how much RAM you are having. In such a case malloc(2.4GB) will surely fail as the user address space is limited to 2GB only. Even allocating 2GB will most probably fail as the system has to allocate 2GB of continuos virtual address space for malloc. This much continous free memory is nearly impossible due to fragmentation.
Computer works with virtual memory, this has no relation to a real size of RAM.

Resources