Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max Array Length in Julia

Tags:

julia

I can create an array of a million elements like this:

Array(1:1_000_000)
Vector{Int64} with 1000000 elements

but if I try to create an array of a billion elements I get this:

Array(1:1_000_000_000)
Julia has exited.
Press Enter to start a new session.

Is Julia not able to handle a billion elements in an array or what am I doing wrong here?

like image 736
Georgery Avatar asked Apr 12 '20 13:04

Georgery


People also ask

What is the maximum possible length of an array?

A Java program can only allocate an array up to a certain size. It generally depends on the JVM that we're using and the platform. Since the index of the array is int, the approximate index value can be 2^31 – 1. Based on this approximation, we can say that the array can theoretically hold 2,147,483,647 elements.

How do I get the size of an array in Julia?

The size() is an inbuilt function in julia which is used to return a tuple containing the dimensions of the specified array. This returned tuple format is (a, b, c) where a is the rows, b is the columns and c is the height of the array.

What is the length size of array?

The length property of an Array object represents the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

How do you declare an array in Julia?

An Array in Julia can be created with the use of a pre-defined keyword Array() or by simply writing array elements within square brackets([]). There are different ways of creating different types of arrays.


1 Answers

You are creating an Array of Int64, each of which needs to be stored in memory:

julia> sizeof(3)
8

So at some point you're bound to run out of memory - this is not due to some inherent limit on the number of elements in an array, but rather the size of the overall array, which in turn depends on the size of each element. Consider:

julia> sizeof(Int8(3))
1

julia> [Int8(1) for _ in 1:1_000_000_000]
1000000000-element Array{Int8,1}:
 1
 1
 1
 ⋮
 1
 1
 1

so filling the array with a smaller data type (8-bit rather than 64-bit Integer) allows me to create an array with more elements.

like image 145
Nils Gudat Avatar answered Oct 01 '22 10:10

Nils Gudat