problem with memory leak.. please help me solve it

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:

[SIZE="2"][LEFT]//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);
		}
	}
}
}[/SIZE][/LEFT]

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!

It doesn’t look like there is any reason why this method would be leaking… Are you sure this is the script/method causing the problem? You are calling the “RemoveHighlight” method, what’s going on in there?

I concur with Antitheory. My only other thought is the fact that the garbage collector runs periodically and it’s not guaranteed to keep memory at a minimum. Sometimes it will keep additional (unused) memory reserved for quick use later. How much does your memory increase per run? If you let the game run for a while and continue to run this method, does the memory usage drop back down?

Finally, are you running/testing with the debugger attached? I’ve found in the past with some platforms (can’t speak for Unity though) and their debuggers that it might bloat memory usage and keep a lot of additional information.

antitheory is right, commenting the RemoveHighlight call indeed solves memory leak…
i have to investigate RemoveHighlight to discover what is going on… thanks!