Jump to content

how many letters can a string contain?


Xander Lopez
 Share

You are about to reply to a thread that has been inactive for 1445 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I created a string:

string names;

Now this string needs to have series of names concatenated by hyphen shown as below:

amy-richard-sandra-april

Okay so i am fine at this moment.. but now I have to enter 200 names in this one string.

So I am trying to see how many characters can a string contain???

 

 

Link to comment
Share on other sites

Essentially, string length is limited by the memory available. 200 names should be ok in a small script, but for a large one you might need to consider a second script that just handles the names (add, remove, find, list, or whatever) and communicates with the main script using link messages.

  • Like 1
Link to comment
Share on other sites

Do remember, however, that you can only llSay (or equivalent function) 1024 bytes of information from a string.  That's 1024 characters, unless some are UTF-8 characters such as á.  If your string is longer than that, the extra characters won't appear.

Link to comment
Share on other sites

31 minutes ago, Kyrah Abattoir said:

That being said, every time you pass a string to a function, you effectively make a temporary copy, so your memory usage doubles at that moment.

I know. And it's strange, since strings (and lists) are immutable. They should be passed by reference. You can't tell the difference from inside the script, and garbage collection would take care of recovering the space. Mono can certainly do that. It's probably a legacy misfeature from the pre-Mono script interpreter.

 

Link to comment
Share on other sites

4 hours ago, Kyrah Abattoir said:

Until you run out of memory.

That being said, everytime you pass a string to a function, you effectively make a temporary copy, so your memory usage doubles at that moment.

3 hours ago, animats said:

I know. And it's strange, since strings (and lists) are immutable. They should be passed by reference. You can't tell the difference from inside the script, and garbage collection would take care of recovering the space. Mono can certainly do that. It's probably a legacy misfeature from the pre-Mono script interpreter.

According to some ancient Linden talks from the past, Mono scripts are "allowed to exceed their memory allowance" for a few operations.

I don't recall the details, but I believe a single function call (eg. string/list parsing) won't cause an error unless the memory usage is still over the limit after that function returns.

Edited by Wulfie Reanimator
Link to comment
Share on other sites

6 hours ago, Wulfie Reanimator said:

I don't recall the details, but I believe a single function call (eg. string/list parsing) won't cause an error unless the memory usage is still over the limit after that function returns.

I once read the same statement and it said in a function call a script can exceed the 64k limit. After that operation a garbage collection is performed and if the memory usage is then under 64k the script can continue. (MONO only)

And MONO and LSL are both compilers and NOT interpreters. That is a huge difference.

 

  • Thanks 1
Link to comment
Share on other sites

4 hours ago, Nova Convair said:

I once read the same statement and it said in a function call a script can exceed the 64k limit. After that operation a garbage collection is performed and if the memory usage is then under 64k the script can continue. (MONO only)

And MONO and LSL are both compilers and NOT interpreters. That is a huge difference.

Easily proven, too:

default
{
    state_entry()
    {
        string kb = "1024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024102410241024";
        string memory;
        integer i;

        llSleep(0.022);
        llOwnerSay(llList2CSV(["Used", llGetUsedMemory(), "Free", llGetFreeMemory()]));

        while (i++ < 25)
        {
            memory += kb;
        }

        llSleep(0.022);
        llOwnerSay(llList2CSV(["Used", llGetUsedMemory(), "Free", llGetFreeMemory(), "String", llStringLength(memory)]));

        memory = llGetSubString(memory, 0, 0);

        llSleep(0.022);
        llOwnerSay(llList2CSV(["Used", llGetUsedMemory(), "Free", llGetFreeMemory(), "String", llStringLength(memory)]));
    }
}
[08:34:42] Object: Used, 9080, Free, 56456
[08:34:42] Object: Used, 60280, Free, 5256, String, 25600
[08:34:43] Object: Used, 9082, Free, 5256, String, 1

First part is the easy part, appending to a string. This technically requires a reference to itself / allocating more memory and copying the old string to create a new string.

Then we have a string that's 25600 bytes, with only 5256 bytes of free memory left. Then we pass the massive string to llGetSubString which would exceed our memory allowance since the function receives a separate copy of the 25600 byte string. The function does its thing and the string is replaced with a smaller string.

The llSleeps are there to let the garbage collector run before llGetUsedMemory. Without them the output is inconsistent, but the script never crashes.

Edit: One thing I didn't notice is that "free memory" is still reported very small. Interesting... It's not recovered even after sleeping 10 seconds or checking from another event.

Edited by Wulfie Reanimator
Link to comment
Share on other sites

13 hours ago, Wulfie Reanimator said:

Edit: One thing I didn't notice is that "free memory" is still reported very small. Interesting... It's not recovered even after sleeping 10 seconds or checking from another event.

Won't that only recover with actual memory reallocation such as with script reset/rerez or setting a new memory limit?

  • Thanks 1
Link to comment
Share on other sites

19 hours ago, Wulfie Reanimator said:

Script reset wouldn't exactly be practical, but re-rezzing seemed to work as well as setting a new memory limit. Why is it like that?

I assume it's defragging memory in some form. The same happens when a script changes regions or the region the script is in restarts, provided it is part of a sim state save.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1445 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...