为什么Erlang在大序列上崩溃? [英] Why is Erlang crashing on large sequences?

查看:169
本文介绍了为什么Erlang在大序列上崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Erlang,并且正在尝试一些Project Euler问题来开始。但是,我似乎能够对大序列进行任何操作,而不会破坏erlang shell。

I have just started learning Erlang and am trying out some Project Euler problems to get started. However, I seem to be able to do any operations on large sequences without crashing the erlang shell.

即使这样:

list:seq(1,64000000).

崩溃erlang,错误:

crashes erlang, with the error:

eheap_alloc:不能分配467078560个字节的内存(类型为heap)。

eheap_alloc: Cannot allocate 467078560 bytes of memory (of type "heap").

实际上字节数当然不同。

Actually # of bytes varies of course.

现在,一半的演出是很多内存,但是一个具有4个演示内存和足够的虚拟内存空间的系统应该能够处理它。

Now half a gig is a lot of memory, but a system with 4 gigs of RAM and plenty of space for virtual memory should be able to handle it.

有没有办法让erlang使用更多的内存?

Is there a way to let erlang use more memory?

推荐答案

您的操作系统可能会有一个默认的用户进程。在Linux上,您可以使用ulimit进行更改。

Your OS may have a default limit on the size of a user process. On Linux you can change this with ulimit.

您可能想要遍历这些64000000个数字,而不需要一次在内存中。 Lazy列表允许您编写类似于list-all-at-once代码的代码:

You probably want to iterate over these 64000000 numbers without needing them all in memory at once. Lazy lists let you write code similar in style to the list-all-at-once code:

-module(lazy).
-export([seq/2]).

seq(M, N) when M =< N ->
    fun() -> [M | seq(M+1, N)] end;
seq(_, _) ->
    fun () -> [] end.

1> Ns = lazy:seq(1, 64000000).
#Fun<lazy.0.26378159>
2> hd(Ns()).
1
3> Ns2 = tl(Ns()).
#Fun<lazy.0.26378159>
4> hd(Ns2()).
2

这篇关于为什么Erlang在大序列上崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆