My application will have Lists. Each object in one list will have to be tied to an object in another list. My idea is to access them by an Index from Lists.
List<MyType1> listNrOne;
List<MyType2> listNrTwo;
foreach (int index in indexes)
{
someObj.SomeFunc(listNrOne[index], listNrTwo[index]);
}
IN ANOTHER WORDS listNrOne[index] and listNrTwo[index] will be tied to a real life object.
- Sometimes an object in a list wont be need at all - that indexed item will be null in order to keep the list at a certain order.
- Sometimes an object will be needed and created, used lets say for 10 seconds in a loop constantly updating and then wont be needed after that.
I want to keep the application memory efficient. So I wont let the GC collect the unnecessary objects and INSTEAD REUSE THEM. So:
// The application does not need listNrTen[11]
queueOfNotNeeded.Enqueue(listNrTen[11]);
listNrTen[11] = null;
// ...
// suddenly an object of the type is needed
// so instead of creating a new one
// the application will reuse existing ones (one that wasn't GCd)
// listNrTen[159] is currently null
listNrTen[159] = queue.Dequeue();
PROBLEM: Will this fragment the memory and how efficient will it stay over time ?
EDIT: The application is a game, so performance is important.