#Lag spikes with Internal ScriptContext

1 messages · Page 1 of 1 (latest)

limber finch
#

In my developer console whilst playing, the Internal ScriptContext starts at 140, then progressively builds to 5000, then it causes a huge lag spike and resets to 140. Any reason for this, or any fix??

south marsh
limber finch
#

I dont understand what internal script context even is

cloud estuary
#

screenshot where

split sparrow
#

i agree with ve

limber finch
#

Theres no error code, and i dont know where in the code this is being caused

#

I dont know what internal scriptcontext is

#

and its spiking in the dev console

cloud estuary
#

screenshot that

limber finch
#

okay 1 sec

#

lemme play so i can get it to spike

#

rn its kind of normal ish

#

its only doing 1000 spikes

#

but when i play the game through roblox and get past wave 10 it starts to do 5000 spikes, lag and reset

cloud estuary
#

memory usage?

limber finch
#

i think memory was above 2500

#

let me get to wave 10 when it starts spiking, then ill send across all info thats spiking

cloud estuary
limber finch
#

its under the first tab

#

which is the memory tab

#

this section

cloud estuary
limber finch
#

rn on wave 5

#

1800 spike

#

goes back down to 80

#

back up to 2000

cloud estuary
#

it looks like garbage collection cycles, you have a memory leak until roblox streams out some things

#

due to low memory situation

limber finch
#

this is the highest ones onthe list

limber finch
#

it was happening to my playtesters too

#

rn its peaking at 4000

cloud estuary
#

ya thats what memory leak looks like

limber finch
#

its hella laggy

cloud estuary
#

you probably don't dispose of some instances until low memory situation causes roblox to clear them out

limber finch
#

is there a way to test for things that arnt being disposed?

cloud estuary
#

could be anything

#

you'll have to search for it

limber finch
cloud estuary
#

any strong reference to an instance will keep it in memory

limber finch
#

Is it just instances that dont get destroyed or what causes memory leaks?

cloud estuary
#

destroyed instances stay in memory if there is a strong reference to it

limber finch
cloud estuary
limber finch
#

how do i know which is weak or strong, and what is the difference?

cloud estuary
#

a reference is strong when it isnt weak

limber finch
cloud estuary
#

local x; x=instance.new()

limber finch
#

is that a strong reference?

cloud estuary
#

references are strong by default

limber finch
#

how do you make a reference weak?

cloud estuary
#

metatable

limber finch
#

okay understanding

#

so lets say im storing something in memory, if i destroy it, its still in memory, so how do i remove it?

cloud estuary
#

or let it go out of scope

limber finch
#

so even after destroying it i have to set it to nil?

cloud estuary
#

most of the time yes

limber finch
#

so hold on

#

other than towerSpawn, would i need to set the other to nil?

#

and or, is it good practice to always set it to nil anyway, even if its not needed

cloud estuary
#

depends what it is and where

limber finch
#

okay, so im not good at finding memory leaks as iv just been introduced to in roblox, BUT

cloud estuary
#

lots of different things can cause it

limber finch
#

i think iv found it

#

and i need some clarification from you if thats okay

cloud estuary
#

common one is not clearing tables of bad data

limber finch
#

local blacklist = {}

cloud estuary
#

idk what that is

limber finch
#

wait

#

would this be the issue

#

im adding to the list via renderstepped

cloud estuary
# limber finch

if you never remove things from the blacklist then yes this is a huge issue major memory leak

limber finch
#

okay, then that could be the issue

#

how do i remove things from the table?

cloud estuary
#

it's not even the parts are being destroyed, it's that you are inserting duplicates and repeated copies of everything, every frame

#

RenderStepped:Connect( table.move(blacklist,1,#blacklist,#blacklist,blacklist:clone()) )

limber finch
#

okay, so, for my game i need this to run everyframe and to add to it as when you are placing towers, it adds to blacklist based on what your hovering over when you are spawning a tower

cloud estuary
#

basically you're inserting a full copy of whatever is in the blacklist into the blacklist again every frame

#

meaning the size of that thing go crazy

#

and you never clear it

limber finch
#

thats probably why it scales more at later waves

cloud estuary
limber finch
#

as there are more towers to add and not remove

#

is there a better way around it, the table.inser for towerSpawn blacklists the tower itself to prevent it from flying at the camera

cloud estuary
#

quickest fix i can see is just reset the table at the start of each frame to rebuild it, but tbh this pattern is pretty bad, but eh you're learning and this is how you learn; you try something, it fails, you learn why, and do better the next time.

limber finch
#

the selectedTower one removes anything that will interfere

cloud estuary
#

other options are something like checking if something is already in the table before adding it

limber finch
cloud estuary
#

listening for instance destroy to remove it from the table

#

etc

#

or use weaktable

#

etc etc

cloud estuary
limber finch
limber finch
cloud estuary
#

you don't need a weaktable here

#

because you're inserting duplicates. weaktables is for a different problem; if you had dead instances in a table but kept the reference alive

#

i.e, tbl[any] = instance and you just left it there without removing the entry

#

or tbl[instance] will keep it alive too.

#

but again... that's not the issue you're having

limber finch
#

iv never used table.move before

#

nor used it on a connect

cloud estuary
limber finch
#

also, not to glaze, but you have been a massive help every time you have came into my threads, even if its annoying that sometimes you leave me at a point that forces me to resolve the situation myself without further help, i can tell it has benefitted me

limber finch
cloud estuary
#

you're adding a copy of the entire blacklist table onto the end of the blacklist table, so things like players are in there 1000 times for no reason

#

after enough time this will eat 100% of memory and if it weren't for failsafes, this would crash roblox and/or the user's device.

limber finch
#

but if there are any i cant move out of renderstepped, how would i go around clearing the table

cloud estuary
#

like if all you did was add blacklist={} to the top of renderstepped before populating it again you wouldn't have a memory leak

#

because the contents of the table from the previous frame(s) get cleared

#

table.clear() is another function for this

#

same same

limber finch
cloud estuary
# limber finch also, not to glaze, but you have been a massive help every time you have came in...

well ya i can't do everything for you. and if i hold your hand the entire way, where are you going to have your own small victories? robbing you of that experience is the most unhelpful thing someone can do; at worst, outright giving you working code and you learn nothing, yet there are those who genuinely believe making you entirely dependent on them to complete scripting tasks is what 'help' is.

#

so i'm glad you have it in you to solve your own problems, that's scripting 👍

limber finch
#

if i move the blacklist creation back into the renderstepped, then how would i use it across the rest of my script

cloud estuary
#

and because you relied on this pattern everywhere, now you have to refactor hehe or do some cheap trick just to make it work

limber finch
#

realistically, does the table.insert for blacklist NEED to be in renderstepped, surely theres a way to do it once, or just do 2 seperate blacklists

cloud estuary
#

this is known as technical debt

limber finch
#

but how do you join tables to make 2 seperate ones

cloud estuary
#

where you need to go back and fix bad decisions you made much earlier in development

cloud estuary
#

the longer your code gets, the more debt you have, and it is a skill to deal with it well. the only way to do that is by making exactly these mistakes, over and over again.

#

sometimes you can get a hint

cloud estuary
#

sometimes someone might be like "oh do it like this, it's better this way, trust me bro" and you either go along with it and it just works out and you never learn why, or you do your own thing, and then you become the person telling other what specific way to do it, because you know why it should be done one way and not the other.

#

it's a, you actually understand why, instead of 'some guy told me to do it this way so i just kept doing it and it kept working idk why it works'

#

im sure you've seen a lot of those

limber finch
#

originally i was streaming my coding, and it was fun, i reached a new peak viewers of 30 and was getting a consistent 20

#

but the problem was, EVERYONE was always "This isnt the most optimal way"

#

and the chat was always arguing about the ACTUAL way

#

and it was so so so overstimulating to deal with

#

i much prefer learning my own way

#

create my own method

#

my own optimum method

#

collaboration has less synergy then indie development

cloud estuary
#

it's easier to argue microoptimizations than actually build anything hehe

cloud estuary
#

or just to shut your brain off and simply be told how to do everything

#

there's a lot of those people around

limber finch
#

literally wouldnt do anything until they were TOLD to

#

like a dog

cloud estuary
#

'you could do x or you could do y'

#

'yeah but which one is better and which one should I do?'

#

😮‍💨

limber finch
cloud estuary
#

and half the time, the best decision often changes as needs change, or you end up needing a part mix of both, different situations call for different decisions

#

or on occasion there's no difference other than preference in how you work with it in other places later down the line... but those people usually don't get that far down the line for it to ever make a difference, so they wouldn't know

#

anyway, memory leaks like this aren't too common, normally it's keeping dead instances in a table like a table of players and you don't remove the player who left

#

but now you know to keep an eye out for both, and to not infinitely fill a table without some limits on it

limber finch
limber finch
#

im gonna keep this thread open incase i rerun into another issue whilst fixing this.

#

But thanks for all the help and the chat too, was refreshing