问题
I have an application written in C# that works well, but occasionally in the field gives errors which we believe are due to low memory conditions, or interactions with the garbage collector.
If anyone is interested, it is described here:
Unable to cast object of type 'NHibernate.Impl.ExpandedQueryExpression' to type 'NHibernate.Linq.NhLinqExpression'
I want to try and reproduce this for debugging, but my development machine has too much memory.
I've removed the pagefile so my virtual memory is limited to the 12GBs of physical memory so aside from physically removing ram, does anyone have any suggestions on how to simulate a low memory condition in a development environment?
EDIT:
Removed asking about tools which monitor the garbage collector?
回答1:
You could use a Virtual Machine (VPC, VMWare or Virtual Box) and tune the memory down.
That is more reliable than a bug.
EDIT
This suggestion is a way of simulating a PC with less physical memory. As stated in comments and in other answers, if you are looking to tune down virtual memory 'eating away' the heap at the start of the process would be a solution.
回答2:
The amount of RAM you have is not relevant on a virtual memory operating system like Windows. Not having enough only slows down the program. What matters is the size of the virtual memory address space, 2 gigabytes on a 32-bit operating system. Set the target platform on your EXE project to x86 if you have a 64-bit operating system.
You can arbitrary increase memory pressure by calling Marshal.AllocHGlobal() at the start of your program. Allocate a chunk of, say, 500 megabytes. Not more, that will fail easily. Grab more by allocating 90 MB chunks.
回答3:
Not an answer, but a very cool utility I found at The Code Project - Memory Allocation Tool
Introduction
Sometimes it is very helpful to test your applications in extreme situations like
low resources, full hard disk, or low memory conditions.
This tool covers just the last - memory.
It allows you to allocate as much memory as might be available.

回答4:
You can:
Make Windows pretend it has less memory.
Use AppVerifier (only mentioned here for completeness, only works for unmanaged code)
Or, if you just want to look, just monitor the collector.
回答5:
Why not using a bug like a tool?
http://connect.microsoft.com/VisualStudio/feedback/details/521147/large-object-heap-fragmentation-causes-outofmemoryexception
回答6:
To monitor the activity of your garbage collector and your processes memory usage you can use windows performance counters.
To fill your RAM why not allocate a rather large array from a c# program?
回答7:
You can use a simple C program to allocate or try to allocate arbitrary amounts of memory on the heap:
#include <stdio.h>
#include <stdlib.h>
#define MB (1024*1024)
#define DEFAULT_ALLOC ((size_t) (512*MB));
int main(int argc, char *argv[]) {
char buffer[2];
char *chunk;
char *endp;
size_t howmuch;
if ( argc < 2 ) {
howmuch = DEFAULT_ALLOC;
}
else {
howmuch = strtoul(argv[1], &endp, 10);
if ( *endp ) {
fputs("Failed to parse command line argument", stderr);
howmuch = DEFAULT_ALLOC;
}
else {
howmuch *= MB;
}
}
chunk = calloc(howmuch, 1);
if ( chunk == NULL ) {
fputs("Memory allocation error", stderr);
exit(EXIT_FAILURE);
}
puts("Memory allocated.\nPress ENTER to terminate program");
fgets(buffer, 2, stdin);
return EXIT_SUCCESS;
}
回答8:
This is a very simple program that we use to test low memory conditions on our linux servers. I have never compiled it on windows, but it should work.
https://github.com/julman99/eatmemory
I Hope it works for you
回答9:
Found this utility that is now FreeWare: http://www.soft.tahionic.com/download-memalloc/
来源:https://stackoverflow.com/questions/7827716/how-can-i-simulate-a-low-memory-condition-in-windows-7