I want to create a List<MyClass> with
class MyClass
{
public string Name;
public List<MyClass> Children;
}
where the user decides how many objects should be created. Depending on the tree depth (all obejcts in the list have equal number of children) I get out of memory exception with different number of objects in the list. Is there a possibility to approximate the maximal number of objects?
No. There isn’t such possibility.
Event if you could calculate something like
sizeoffor managed object, and get amount of free memory, you couldn’t rely on this numbers, because managed code knows nothing about current memory fragmentation in its managed heap, it can’t stop other processes in system (and other threads in current process) to query new memory allocations from system.