c# - 'System.OutOfMemoryException' was thrown when there is still plenty of memory free -
this code:
int size = 100000000; double sizeinmegabytes = (size * 8.0) / 1024.0 / 1024.0; //762 mb double[] randomnumbers = new double[size];
exception: exception of type 'system.outofmemoryexception' thrown.
i have 4gb memory on machine 2.5gb free when start running, there enough space on pc handle 762mb of 100000000 random numbers. need store many random numbers possible given available memory. when go production there 12gb on box , want make use of it.
does clr constrain me default max memory start with? , how request more?
update
i thought breaking smaller chunks , incrementally adding memory requirements if issue due memory fragmentation, doesn't i can't past total arraylist size of 256mb regardless of tweaking blocksize.
private static irandomgenerator rnd = new mersennetwister(); private static idistribution dist = new discretenormaldistribution(1048576); private static list<double> ndrandomnumbers = new list<double>(); private static void addndrandomnumbers(int numberofrandomnumbers) { (int = 0; < numberofrandomnumbers; i++) { ndrandomnumbers.add(dist.icdf(rnd.nextuniform())); } }
from main method:
int blocksize = 1000000; while (true) { try { addndrandomnumbers(blocksize); } catch (system.outofmemoryexception ex) { break; } } double arraytotalsizeinmegabytes = (ndrandomnumbers.count * 8.0) / 1024.0 / 1024.0;
you may want read this: "“out of memory” not refer physical memory" eric lippert.
in short, , simplified, "out of memory" not mean amount of available memory small. common reason within current address space, there no contiguous portion of memory large enough serve wanted allocation. if have 100 blocks, each 4 mb large, not going when need 1 5 mb block.
key points:
- the data storage call “process memory” in opinion best visualized massive file on disk.
- ram can seen merely performance optimization
- total amount of virtual memory program consumes not hugely relevant performance
- "running out of ram" seldom results in “out of memory” error. instead of error, results in bad performance because full cost of fact storage on disk becomes relevant.
Comments
Post a Comment