I do not believe that there is any API that will give you the true size of an object, but it can be determined by measuring the amount of memory used before and after creating the object; however, it is not very thread safe.
You can use System.Runtime.InteropServices.Marshal.SizeOf (), although this will not give you the exact size, it will give you the exact comparison point so that you can say something like: if the size of the object increases by X%, then delete Y amount of elements.
In addition, you can try to save the total quantity, but if the items in the collection are editable, you will need to make sure that every addition, deletion and editing is included in the calculation. You still have the problem of correctly measuring the elements you add and remove, and, as stated above, I don't believe there is an API that will do this for sure.
source share