What are the size limits of variables in Ruby?

Asked

Viewed 808 times

10

I would like to know the size limit of variables of the following types:

  • String - How many characters can I have in a single string?
  • Integer and Float - What is the largest and smallest number that can be represented?
  • Array - What is the highest number of indexes I can have in a single array?
  • Hash - What the largest number of pairs (key:value) I can have in one hash?

2 answers

13


Since it sometimes depends on its architecture (32 or 64 bits), I set the limits as defined in the language code (when existing):

String:

Integer:

Float:

Array/Hash:

13

String => theoretical limit of 231 - 1 (32 bits) or 263 - 1 (64 bits). I want to see someone manage to allocate a string this size

Integer => In thesis Ruby can change the representation and have infinite values

Float => Usually limited by architecture. Generally between 1.7976931348623158e+308 and 2.2250738585072014e-308

Array => There is no theoretical limit. In practice you can’t even use near the limit. In 32 bits there is a total limit of 4GB virtual memory. You won’t be able to create one array with 231 - 1 even if each element contains only 1 byte and in Ruby as everything is reference, this is far from possible. In 64 bits if you have 263 - 1 elements, will need a lot underneath (certainly this volume is much bigger) 250EB (Exabytes). Forget it

Hash => essentially the same Although you need even more memory. But again, don’t worry so much about this limit, the practical limit comes first

  • Why the theoretical string limit is calculated as 2^(arquitetura - 1) - 1? (now yes!)

  • 2

    You know about the address limit, how this value is calculated, right? It’s 63 and not 64 bits because it disregards the signal. This is used in the operating system (virtual memory may even have negative value to identify whether it is in RAM or disk, but this is detail). The -1 is because it starts from 0, so the last number cannot be considered, is equal to a array. You better use a int than a uint, no sign numbers may behave more positive, but are difficult to deal with, especially when there is interaction with other types.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.