如何分配 RAM 容纳不下的单个数组
我以为我永远不会说这句话。我想让我的机器运行得超慢。
这是我想要执行此操作的方式: 我想在 F# (Array.init) 中分配一个大数组。它应该太大,以至于对数组的随机访问会产生页面错误。我有 4GB RAM,并以 64 位模式运行。当我分配 2^29 4 字节整数时,运行时抛出内存不足异常。
> Array.init (1 <<< 28) (fun i->i);;
val it : int [] =
[|0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20;
21; 22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39;
40; 41; 42; 43; 44; 45; 46; 47; 48; 49; 50; 51; 52; 53; 54; 55; 56; 57; 58;
59; 60; 61; 62; 63; 64; 65; 66; 67; 68; 69; 70; 71; 72; 73; 74; 75; 76; 77;
78; 79; 80; 81; 82; 83; 84; 85; 86; 87; 88; 89; 90; 91; 92; 93; 94; 95; 96;
97; 98; 99; ...|]
> Array.init (1 <<< 29) (fun i->i);;
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at <StartupCode$FSI_0003>.$FSI_0003.main@()
Stopped due to error
>
I thought I'd never say this. I would like to make my machine run super-slow.
Here's the manner in which I'd like to do this:
I'd like to allocate a single large array in F# (Array.init). It should be so large that random accesses into the array should generate page faults. I have 4GB RAM, and running in 64-bit mode. When I allocate 2^29 4-byte integers, runtime throws an out of memory exception.
> Array.init (1 <<< 28) (fun i->i);;
val it : int [] =
[|0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20;
21; 22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39;
40; 41; 42; 43; 44; 45; 46; 47; 48; 49; 50; 51; 52; 53; 54; 55; 56; 57; 58;
59; 60; 61; 62; 63; 64; 65; 66; 67; 68; 69; 70; 71; 72; 73; 74; 75; 76; 77;
78; 79; 80; 81; 82; 83; 84; 85; 86; 87; 88; 89; 90; 91; 92; 93; 94; 95; 96;
97; 98; 99; ...|]
> Array.init (1 <<< 29) (fun i->i);;
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at <StartupCode$FSI_0003>.$FSI_0003.main@()
Stopped due to error
>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Microsoft CLR 允许的单个对象的最大大小为 2GB,无论您是在 32 位还是 64 位平台上运行。
当您尝试分配
2**29
整数数组时,您就会遇到这个硬限制。 (数组的数据正好是 2GB,但对象还需要一些额外的字节来进行管理等,从而使您超过 2GB。)尝试分配一个稍微较小的数组,以允许额外的几个字节的开销。我不记得它需要小多少——用
(2**29)-3
整数、(2**29)-4< 进行一些实验/code> 整数、
(2**29)-5
整数等应该很快就能告诉你。The maximum size allowed by the Microsoft CLR for a single object is 2GB, regardless of whether you're running on a 32-bit or 64-bit platform.
You're running up against this hard limit when you try to allocate the array of
2**29
integers. (The array's data would be exactly 2GB, but objects also need a few extra bytes for housekeeping etc, pushing you over 2GB.)Try allocating a slightly smaller array to allow for those extra few bytes of overhead. I can't remember exactly how much smaller it'd need to be -- a few experiments with
(2**29)-3
integers,(2**29)-4
integers,(2**29)-5
integers etc should tell you pretty quickly.