- Home /
help with memory leak
first to clear something out, the function i wrote is probably not the best practice, but it was done fast and it works, but later i discovered that memory used increases every time i use this function...so i guess there is some kind of problem with arrays... can you help me out. memory used increases pretty fast it renders function useless....
here is the code:
//REMOVE ALL OBJECTS THAT ARE OUTSIDE CUT OFF DISTANCE function RemoveObjectsOutsideCutoffDistance(center : Vector3){
//ONLY REMOVE IF WE ARE IN NORMAL MODE if(currentViewState==viewState.normal || currentViewState==viewState.region){
var objectsToShow=new Array();
//GO THROUGH ALL SYSTEMS for(i=0;i<TrackingControl.Systems.length;i++){
//CHECK IF SYSTEM IS LOADED AND VISIBLE if(TrackingControl.Systems[i].isLoaded && TrackingControl.Systems[i].isVisible){
//GET THE COLLIDERS OF OBJECTS THAT ARE INSIDE THE SPHERE COLLIDER var colliders=Physics.OverlapSphere(center,TrackingControl.Systems[i].radius);
//MAKE AN ARRAY OF OBJECTS THAT WE ARE GOING TO SHOW
for(var e=0;e<colliders.length;e++){
if(colliders[e].tag==TrackingControl.Systems[i].tag){
if(!colliders[e].gameObject.GetComponent("Highlight").hidden){
objectsToShow.Push(colliders[e].transform);
}
}
}
//HIDE ALL OBJECTS IN THE SYSTEM
var Objects : GameObject[]=GameObject.FindGameObjectsWithTag(TrackingControl.Systems[i].tag);
for(k=0;k<Objects.length;k++){
Objects[k].renderer.enabled=false;
Objects[k].gameObject.layer=2;
}
}
}
//COPY "OBJECTS TO SHOW" INTO GLOBAL ARRAY THAT TRACKS VISIBLE OBJECTS IN THE SCENE
//WE CAN USE THAT ARRAY TO HANDLE WHAT OBJECT TO SHOW WHEN IN HIDDEN MODE
if(currentSeeThroughState==seeThroughState.hidden){
visibleObjects=objectsToShow;
}
//UNHIDE THE ONES THAT ARE INSIDE THE SPHERE
for(j=0;j<objectsToShow.length;j++){
if(!objectsToShow[j].gameObject.GetComponent("Highlight").hidden){
objectsToShow[j].renderer.enabled=true;
objectsToShow[j].gameObject.layer=0;
RemoveHighlight(objectsToShow[j].transform);
}
}
}
}
what is happening here is that i want to remove all objects from the scene that are not inside the sphere that has radius determined in other script. so i get all objects (from different systems in the scene- tracking control) that are in the sphere, copy all the objects in the array objectsToShow, then hide all objects in the scene and then unhide that ones that are in the objectsToShow. There is also a case where we copy that array to other global array, but that is irrelevant now, since memory goes up when we are outside that state.
it works fine but only things that worries me is that memory leaks....i do not know why, do i need to clear arrays? they should be alive only inside the function.
thanks in advance! any help appreciated!
cross post to forums: http://forum.unity3d.com/threads/79548-problem-with-memory-leak..-please-help-me-solve-it
Answer by efge · Feb 25, 2011 at 02:30 PM
You want to not render objects outside a given distance/radius?
Why not use the farClipPlane of your Camera for all objects or layerCullDistances for several objects assigned to layers?
because i want to remove object that are outside of sphere radius which is in the center of current selection not camera