How can I allocate all the availble memory in visual studio for my application?

前端 未结 3 558
离开以前
离开以前 2020-12-16 04:36

I want to render 4 millions triangles in my windows based software which is written in Visual Studio C++ 2010 (Build in Release Mode). When I render 3.9 millions triangles,

相关标签:
3条回答
  • 2020-12-16 04:57

    It is one of the Great Myths of Windows programming, a process can never run out of RAM. Windows is a demand-paged virtual memory operating system, if a process needs more RAM then the operating system makes room by paging out other memory pages, owned by other processes. Or the process itself, swapping pages out that haven't been used for a while.

    That myth is encouraged by the way Task Manager reports memory usage for a process with its default settings. It shows working set, the actual number of bytes of the process that are in RAM. A value that's usually much smaller than the amount of virtual memory allocated by the process. A process dies on OOM when it can't allocate virtual memory anymore. Another statistic in Taskmgr, the VM size value. And it usually dies not because all VM was used but because there isn't a hole left that's big enough. The SysInternals' VMMap utility is a good way to see how a process uses its address space.

    Getting a larger virtual memory address space requires a pretty fundamental overhaul. Albeit that it is easy today, just target x64 as the platform target. A 64-bit process has massive amounts of address space available, limited only by the maximum size of the paging file. You could limp along in 32-bit mode, as long as you can count on actually running on a 64-bit operating system, by using the /LARGEADDRESSAWARE linker option. Which increases the VM size from 2 GB to 4 GB on a 64-bit operating system.

    0 讨论(0)
  • 2020-12-16 04:57

    And one more question in my mind is, c++ "new" operator for memory allocation giving me >error, how about c "malloc" operator ? can "malloc" fix this issue, is there any diffirence >between these two?

    There are differences between malloc and new, for example, new will initialize your memory and call the constructor of the class automatically. Or initialize if they are primitive types(such as float, int, char etc). Also the memory allocated by new should be deleted with the delete keyword which calls the destructor.

    C's malloc() as well as new operator in Visual Studio internally call HeapAlloc(). HeapAlloc() calls VirtualAlloc() if the memory required is too large, or is shared between processes. So, it will not necessarily fix your issue. Infact if you are using C++ stick to using new.

    0 讨论(0)
  • 2020-12-16 05:06

    For one of the questions:

    is there any diffirence between these two?

    the different between new and malloc is as follows:

    1. malloc is used in C, malloc allocates uninitialized memory. The allocated memory has to be released with free.

    2. new initializes the allocated memory by calling the corresponding constructor. Memory allocated with new should be released with delete (which calls the destructor). You don't need to give the size of memory block in order to release the allocated memory.

    It is not clear whether new and malloc are related according to the standard (it depends on whether a specific compiler implements new using malloc or not), so the issue may or may not be resolved by simply replacing new with malloc.

    From the code you showed, it is difficult to spot the cause of error. You may try to replace the dynamic array with vector to see if it solves your problem. Meanwhile, you may use valgrind to check whether you have memory leak in your code (if you can somehow port your code to Linux with makefiles since unfortunately valgrind is not available on Windows.).

    0 讨论(0)
提交回复
热议问题