I have a .netcore app running in cloud foundry (4GB RAM). Following piece of code is throwing out of memory exception. I can't really find the record causing the issue because following code is being called inside a job.
I did find article over internet about "MemoryStream" that it keeps on un-allocate and re-allocate memory until object size matches with stream size But couldn't find solution to fix my problem.
IFormatter frm = new BinaryFormatter();
Stream sm = new MemoryStream();
using (sm)
{
frm.Serialize(sm, myobj);
sm.Seek(0, SeekOrigin.Begin);
return (T)frm.Deserialize(sm);
}
The code is called in a job which loops through a list of objects and tries to clone each object. Some of the objects can be really big but definitely not even close to 4 GB which is size of memory.
Environment.Is64BitOperatingSystem
is true
so I am definitely running in 64-bit mode.
More updates: I did some more investigation and found that there is one object. if I copy json from mongo to notepad it's size is 1 MB. Code is creating more than 7K clone of this object. what is the best way to handle it.
Resolved: Looked into logic and found a way to avoid cloning project. I guess that was the only way to solve my problem.