-3

I need to build an object which consists of almost 20000 nested objects (in multiple levels). Each object is a simple database entity with 1-5 fields or a list of entities. I am using inline object initializer to initiate my root object.

new OUTPUT() { XREF_CATALOG_MATERIALS = xrefCatalogMaterials.Find(x => x.MATERIAL.PART_NUM.Equals("xxxx")), FUNCTION = new FUNCTION() {...

I tried running on both x86 and x64 mode and in both cases I get stackoverflow exception. The same code and logic works fine on the other cases that my object is not that big (around 6000 nested objects)

Is there any way to increase .Net applicationheap size? any suggestion that I can use to solve that issue?

Ehsan
  • 357
  • 5
  • 22
  • 2
    You'd be far better served to only load what you need, as you need it. I find it very doubtful that you need all 20,000 objects every time you load that object. – krillgar Apr 27 '16 at 19:09
  • Increasing the stack size (not the heap size, it's not a *heap* overflow) doesn't seem like it would be much of a solution. The problem is the design, not the host system. You have too many nested function calls. – David Apr 27 '16 at 19:10
  • @krillgar I agree with design having problem , I inherited the code from another developer and wanted to fix it quickly if it was possible. – Ehsan Apr 27 '16 at 19:15
  • 1
    That's always fun, and I'm sorry to hear that. Your best bet is to spend some extra time (which might be monumental) to get it right. Plead with your supervisor for that time, otherwise you'll be spending 10x the effort later to fix other bugs that continue to pop up out of nowhere later one. – krillgar Apr 27 '16 at 19:19

1 Answers1

4

from that description you don't have a problem with heap size. you have a problem with stack size. looks like you're trying to invoke too many nested functions. every function call has an effect on stack. Stack is much smaller than heap and it is relatively easy to overflow it. Easiest way is a recursion.

https://msdn.microsoft.com/en-us/library/system.stackoverflowexception(v=vs.110).aspx

StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion.
vmg
  • 9,920
  • 13
  • 61
  • 90