in reply to Re: maximum length of scalar in theory
in thread maximum length of scalar in theory

Well, I have run a test on my Apple MacBook and SparkyLinux using Perl 5.40. But both of those systems are 64-bit with a modern Perl, and in each case, Perl seems to have limits but not sure why. I don't have unlimited memory in my computers, but I tried to create a string that is 4GB long, and Perl immediately said "Out of memory" and the program ended. So, it didn't even try to create the string. Then I tried to create a string that is 1GB, and after some time, it displayed a message that says "Killed", and then I was staring at the # prompt in Linux. So, then I tried to create an 800 MB string, and it worked. And this is how much memory I've got:
# free total used free shared buff/cache + available Mem: 3290320 1522528 1677380 152628 397604 + 1767792 Swap: 0 0 0

So, I can't really tell if my theory is right. I mean I would need a computer with 2 Exabytes of memory to see if it would work. But it seems I have more than 800 MB of free memory, yet it won't allow me to create a 1 GB string. I don't understand why it kills the process. Well, anyway, I just wanted to know what's a safe size for a string. So, it seems that if I create a program that works with 1 GB strings, it will run on most modern machines. But if I write a program that relies on creating and working with 2GB or 4GB strings, then that might fail on some systems. I guess, the bigger the string, the fewer computers will be able to handle it.

Btw I also checked the Perl machine used in this online site https://www.onlinegdb.com/online_perl_compiler, and I was able to create a 1 GB string with no problem! Yay! Now, when I tried to create a 1.5 GB string, my program just stopped working without any error message. So, I guess, they limit how much memory they allocate for anonymous users online and if a process tries to use more, it gets killed silently.

Replies are listed 'Best First'.
Re^3: maximum length of scalar in theory
by hippo (Archbishop) on Sep 30, 2024 at 08:41 UTC
    Perl seems to have limits but not sure why. I don't have unlimited memory in my computers

    The second statement is true for everyone. And that rather shows the error in the first. You have assumed that Perl has limits when in fact all your demonstration shows is that your computer (and/or operating system) has limits. You can't put 10 litres of anything in a 1 litre bottle.

    But it seems I have more than 800 MB of free memory, yet it won't allow me to create a 1 GB string.

    You have 1767792kB of free memory. Even a cursory inspection shows that this will allow the allocation of 800MB x 2 but not 1GB x 2. I therefore posit that your unshown code actually uses twice the amount of RAM that you think it should. Try adding 400MB of swap space and be amazed that your program will now run with a 1GB string instead.

    Well, anyway, I just wanted to know what's a safe size for a string. So, it seems that if I create a program that works with 1 GB strings, it will run on most modern machines. But if I write a program that relies on creating and working with 2GB or 4GB strings, then that might fail on some systems. I guess, the bigger the string, the fewer computers will be able to handle it.

    That isn't how publicly distributed programs are written. You cannot assume anything about the platform on which your code will be run, so don't even try.


    🦛