I have a class called TreeNode
public class TreeNode
{
public TreeNode Parent { get; set; }
public Guid Id { get; set; }
public List<TreeNode> ChildrenNodes { get; set; }
public TreeNode FindChildrenNodeById(Guid node_Id);
void RecursivelyFindNode();
}
I want to cache a list of TreeNode objects into memory.
Is that if i keep the FindChildrenNode method inside the class, will occupy more space into memory when cached?
Should i move the search functionality in a TreeNodeUtilities class?
Only fields consume memory per-instance of the class. Methods don’t. Properties don’t consume per-instance memory either, but their backing fields will.
Methods will consume a bit of memory for the code and metadata, but it’s small and doesn’t scale with the number of objects you created, so it can be neglected in most circumstances.
This means you won’t gain anything by moving search functionality to a separate class.
If you’re desperate for memory, changing the way the child list is stored (for example using a first-child, next-sibling system) or even turning it into a struct embedded in an array would gain you a bit. But I wouldn’t do either, unless it is clearly a major performance issue.