{"id":1733,"date":"2012-01-16T22:42:02","date_gmt":"2012-01-16T22:42:02","guid":{"rendered":"http:\/\/sl.governormarley.com\/?p=1733"},"modified":"2012-01-16T22:52:06","modified_gmt":"2012-01-16T22:52:06","slug":"llsetmemorylimit-mono-and-a-bit-of-confusion","status":"publish","type":"post","link":"https:\/\/sl.governormarley.com\/?p=1733","title":{"rendered":"llSetMemoryLimit Mono And A Bit Of Confusion"},"content":{"rendered":"<p>I&#8217;m going to talk here about a relatively new scripting function, <a title=\"llSetMemoryLimit\" href=\"http:\/\/wiki.secondlife.com\/wiki\/LlSetMemoryLimit\" target=\"_blank\">llSetMemoryLimit<\/a>, this may not work the way one thinks it does, but it basically sets the upper memory limit your script can use. I&#8217;m far from being a scripting expert but this looks like a function that people should get used to using where they can, even if it might not actually save memory for a sim the way we may think it should, I&#8217;m not sure how the server handles the space.<\/p>\n<p>Anyway, when you create a new script, if Mono is ticked, it has an upper memory limit of 64kb, if you don&#8217;t compile the script as Mono, it compiles as LSO, and that has an upper memory limit of 16kb, with LSO it&#8217;s a hard rule, all LSO scripts have an upper limit of 16kb, llSetMemoryLimit won&#8217;t work with LSO, with Mono however you can set a lower limit. In this post I&#8217;ll show how this works with a very basic script, a notecard giver.<\/p>\n<p>I&#8217;ll start by visiting <a title=\"LSL Wiki\" href=\"http:\/\/lslwiki.net\/lslwiki\/wakka.php?wakka=HomePage\" target=\"_blank\">LSL Wiki <\/a>and going to the <a title=\"llGiveInventory\" href=\"http:\/\/lslwiki.net\/lslwiki\/wakka.php?wakka=llGiveInventory\" target=\"_blank\">llGiveInventory section<\/a>. LSL Wiki isn&#8217;t as up to date as the <a title=\"LSL Portal\" href=\"http:\/\/wiki.secondlife.com\/wiki\/LSL_Portal\" target=\"_blank\">LSL portal <\/a>but I often find the discussions and examples\u00a0on LSL Wiki\u00a0more helpful. So all I want is a notecard giver, I rez a prim, go to the contents tab, click new script, get the default hello world script, delete everything and replace it with this (with apologies for the horrid coding style):<\/p>\n<p><em>default<\/em><br \/>\n<em>{<\/em><br \/>\n<em>state_entry()<\/em><br \/>\n<em>{<\/em><br \/>\n<em>}<\/em><br \/>\n<em>touch_start(integer total_number)<\/em><br \/>\n<em>{<\/em><br \/>\n<em>llGiveInventory(llDetectedKey(0),llGetInventoryName(INVENTORY_NOTECARD, 0));<\/em><br \/>\n<em>}<\/em><\/p>\n<p>Then I make sure there&#8217;s a notecard in the contents of the prim and when I touch it, I&#8217;ll get a Notecard. However this is a Mono script so it defaults to an upper memory limit of 64kb:<\/p>\n<p><a title=\"Default Mono Script Limit by Ciaran Laval, on Flickr\" href=\"http:\/\/www.flickr.com\/photos\/23556228@N08\/6710136013\/\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/farm8.staticflickr.com\/7164\/6710136013_4ba3d2c73e_o.jpg?resize=351%2C241\" alt=\"Default Mono Script Limit\" width=\"351\" height=\"241\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>So let&#8217;s look at how we can reduce that using llSetMemoryLimit()<\/p>\n<p><!--more--><\/p>\n<p>Now the first thing to note here is that I wouldn&#8217;t put a for sale script out there using this method, but it gives you an idea of how things work. There are a few ways of getting an idea of how much memory a script uses but what I&#8217;m going to do here is use <a title=\"llScriptProfiler\" href=\"http:\/\/wiki.secondlife.com\/wiki\/LlScriptProfiler\" target=\"_blank\">llScriptProfiler<\/a> and base my test on the example there. Note the caveats there, having the profiler on adds to overheads. I&#8217;m going to run the profiler, find out how much memory the script uses then set my memory limit to that figure, I wouldn&#8217;t advise people do this for production scripts, I&#8217;d add some spare capacity, but this gives you an idea on what you can do. My new script will look like this:<\/p>\n<p><em>integer limit = 64000;<\/em><br \/>\n<em>{<\/em><br \/>\n<em>default<\/em><br \/>\n<em>{<\/em><br \/>\n<em>state_entry()<\/em><br \/>\n<em>{<\/em><br \/>\n<em>llSetMemoryLimit(limit);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>touch_start(integer total_number)<\/em><br \/>\n<em>{<\/em><br \/>\n<em>llScriptProfiler(PROFILE_SCRIPT_MEMORY);<\/em><br \/>\n<em>llGiveInventory(llDetectedKey(0),llGetInventoryName(INVENTORY_NOTECARD, 0));<\/em><br \/>\n<em>llScriptProfiler(PROFILE_NONE);<\/em><br \/>\n<em>llOwnerSay(&#8220;This script used at most &#8221; + (string)llGetSPMaxMemory() + &#8221; bytes of memory during Touching.&#8221;);<\/em><br \/>\n<em>limit = llGetSPMaxMemory();<\/em><br \/>\n<em>llSetMemoryLimit(limit);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>}<\/em><\/p>\n<p>Now when I touch the script, I&#8217;ll see this:<\/p>\n<p><a title=\"Message telling me memory usage by Ciaran Laval, on Flickr\" href=\"http:\/\/www.flickr.com\/photos\/23556228@N08\/6710136175\/\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/farm8.staticflickr.com\/7172\/6710136175_c6ffea8488_o.jpg?resize=295%2C41\" alt=\"Message telling me memory usage\" width=\"295\" height=\"41\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>Then when I go to about land and look at script info under the general tab, I&#8217;ll see that it is indeed only reporting my script as 4kb:<\/p>\n<p><a title=\"Notecard giver after setting the limit by Ciaran Laval, on Flickr\" href=\"http:\/\/www.flickr.com\/photos\/23556228@N08\/6710136065\/\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/farm8.staticflickr.com\/7149\/6710136065_633411ddbe_o.jpg?resize=357%2C242\" alt=\"Notecard giver after setting the limit\" width=\"357\" height=\"242\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>Then I go a bit mad and rez another three of these objects, touch them a couple of times to make sure they don&#8217;t crash and then find that about land\/script info now shows this:<\/p>\n<p><a title=\"4 Notecard Givers rezzed by Ciaran Laval, on Flickr\" href=\"http:\/\/www.flickr.com\/photos\/23556228@N08\/6710136305\/\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/farm8.staticflickr.com\/7027\/6710136305_2be38e7d63_o.jpg?resize=349%2C281\" alt=\"4 Notecard Givers rezzed\" width=\"349\" height=\"281\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>Now is the point where I get confused, I&#8217;m not sure what this actually does in terms of saving memory allocations or whether it does indeed do that. Yes I can reduce the way my script gets reported, but my notecard script would never use more than the amount llScriptProfiler reported, indeed it uses more than it needs to because I&#8217;m running the profiler. Someone like Kelly Linden would be needed to explain how this actually works, whether it saves any memory and whether it&#8217;s actually useful, I see it as a good practice tool.<\/p>\n<p>However, depending upon your script, you&#8217;d need to be careful with this, for simple scripts it&#8217;s fine but for scripts where\u00a0users may be adding variables or data to lists, you&#8217;re going to want to leave room for your script to grow and just how much memory you need for that, only time will tell.<\/p>\n<p>So as it stands I&#8217;d treat this with caution, but it&#8217;s worth noting and worth using when you can just to get into the habit of using it and knowing yourself that your scripts aren&#8217;t eating up huge amounts of memory.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m going to talk here about a relatively new scripting function, llSetMemoryLimit, this may not work the way one thinks it does, but it basically sets the upper memory limit your script can use. I&#8217;m far from being a scripting expert but this looks like a function that people should get used to using where &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/sl.governormarley.com\/?p=1733\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;llSetMemoryLimit Mono And A Bit Of Confusion&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[3,14],"tags":[19,272,321,322,1022],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6rJUK-rX","jetpack-related-posts":[],"jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/sl.governormarley.com\/index.php?rest_route=\/wp\/v2\/posts\/1733"}],"collection":[{"href":"https:\/\/sl.governormarley.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sl.governormarley.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sl.governormarley.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sl.governormarley.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1733"}],"version-history":[{"count":4,"href":"https:\/\/sl.governormarley.com\/index.php?rest_route=\/wp\/v2\/posts\/1733\/revisions"}],"predecessor-version":[{"id":1735,"href":"https:\/\/sl.governormarley.com\/index.php?rest_route=\/wp\/v2\/posts\/1733\/revisions\/1735"}],"wp:attachment":[{"href":"https:\/\/sl.governormarley.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sl.governormarley.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sl.governormarley.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}