Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a limit on the size of an array in ruby? [duplicate]

Tags:

arrays

ruby

Possible Duplicate:
Array size too big - ruby

Sorry if this has been asked, I looked around but didnt really find what I was looking for.

I am using ruby and mysql to create an array based off of a single column in the mysql table. So for example, say I have a column of user names:

 users = []
 users.clear
 
 # Update the list of users to follow
 res = dbh.query("SELECT user FROM usernameDB")
 while row = res.fetch_row do
   users << row[0] #adds each user to the array
 end

This has worked fine up until now, when we started recieving a lot more users. Now the code gives me unknown errors.

In an attempt to troubleshoot, I simply commented out most of it and built the array with just a couple of user names, and everything worked again. So my question is, is there a limit to the number or items in a ruby array?

Thanks!

like image 308
bradleygriffith Avatar asked Jul 28 '11 20:07

bradleygriffith


People also ask

Is there a limit to array size?

The theoretical maximum Java array size is 2,147,483,647 elements. To find the size of a Java array, query an array's length property.

Can size of array be increased?

Size of an array If you create an array by initializing its values directly, the size will be the number of elements in it. Thus the size of the array is determined at the time of its creation or, initialization once it is done you cannot change the size of the array.

What is the maximum size of an array in C#?

The array size is limited to a total of 4 billion elements, and to a maximum index of 0X7FEFFFFF in any given dimension (0X7FFFFFC7 for byte arrays and arrays of single-byte structures). . NET Framework only: By default, the maximum size of an Array is 2 gigabytes (GB).

How do you check if there are duplicates in an array Ruby?

select { array. count } is a nested loop, you're doing an O(n^2) complex algorithm for something which can be done in O(n). You're right, to solve this Skizit's question we can use in O(n); but in order to find out which elements are duplicated an O(n^2) algo is the only way I can think of so far.


1 Answers

Take a look at this other post: Array size too big - ruby. The size of 600 million was too big, but 500 million worked.

How big of an array are you working with? The problem may be that you're running out of memory!

like image 178
danmcardle Avatar answered Sep 18 '22 13:09

danmcardle