c# - Can't figure out how this method produces garbage and how to fix it -
i've got method i'm calling ... well, often. , gc becoming problem, because apparently method creating garbage, can't seem figure out why.
item item; foreach (point3d p in point3d.alldirections) { item = grid[gridposition + p]; if (item != null && item.state != state.initialized) else return; } othermethod(this);
this method inside item class , figures out state of neighbouring items. gridposition variable position of item inside grid , point3d struct contains 3 ints, 1 each axis. contains few predefined static arrays such alldirections contains point3ds.
now, state of items can change @ time , once neighbours have specific state (or rather aren't in initialized state) other stuff (othermethod), changes state of item method no longer called. such produced garbage problem when neighbours don't change state (for example when there no user input). guess think of substitution event driven system.
i admit have rather limited understanding of memory management , tend understand things doing them, reading on didn't me problem. however, after reading got impression every value type, defined within method , doesn't leave it, collected upon leaving method. have little trouble figureing out what's left after return.
any ideas on what's generating garbage here ? of course best case produce no garbage @ all, if @ possible.
(just in case relevant, i'm working in unity , method in question monobehaviour update)
edit comments: holy cow guys quick. so... @ jeppe stig nielsen: there nothing between if , else, there once didn't need anymore. it's quick way leave if no neighbour found. also, indexer ! forgot that, here is:
public item this[point3d gridposition] { { item item; items.trygetvalue(gridposition, out item); return item; } }
could trygetvalue reason ?
@ luaan: tried normal loop, didn't change anything. looked this:
(int = 0; < 6; i++) { item = grid[gridposition + point3d.alldirections[i]]; }
if enumerator, should've fixed it, no ?
@ rufus: yes that's better way write if, fix that. whole thing work in progress right now, that's remnant of past experimentation. there nothing else in if statement.
@ tom jonckheere: there isn't else. it's unitys update() followed 2 if statements state. , that's because work 2 states have setup more, someday there switch instead of ifs.
@ adam houldsworth: yes, problem call often. odd thing is, amount of garbage produced varies wildly. far can tell it's in range of 28 bytes 1.8kb. whole red herring thing, don't think that's case. profiler points update method, contains 2 ifs, 1 code above, other 1 isn't being used when no state changes occur. , when testing, there no state changes, except intial setup.
also, sorry wrong tag, first time i've visited site. looks i'll have reading :)
foreach
calls getenumerator
, which, in turn, creates new instance of enumerator every time it's called.
Comments
Post a Comment