Open XeClutch opened 1 year ago
Forgot to mention in the thread that I'm currently using v13.0.3 via NuGet.
I notice that you are serializing your object array to a string, then writing that string to disk:
private bool Export(object[] obj) { string json = Serialize(obj); // write "json" to disk }
Newtonsoft's documentation page Performance Tips: Optimize Memory Usage recommends that you should not do that and suggests serializing directly to a Stream
instead:
To keep an application consistently fast, it is important to minimize the amount of time the .NET framework spends performing garbage collection. Allocating too many objects or allocating very large objects can slow down or even halt an application while garbage collection is in progress.
To minimize memory usage and the number of objects allocated, Json.NET supports serializing and deserializing directly to a stream. Reading or writing JSON a piece at a time, instead of having the entire JSON string loaded into memory, is especially important when working with JSON documents greater than 85kb in size to avoid the JSON string ending up in the large object heap.
For examples of serializing to directly to files, see the documentation pages Serialize JSON to a file and Serializing and Deserializing JSON: JsonSerializer.
If you make the change as suggested by Newtonsoft, does that resolve your problem?
Please forgive the masking of the true names of my fields & data types. This is a piece of closed-source proprietary corporate software and I can't risk potentially exposing any of our trade secrets to our aggressive competition. The application where this code lives is our "secret sauce".
For some context, this normally works but since implementing multi-threading, we're seeing this occur after 3 hours and 10 minutes of continuous runtime.
NOTE: In the Diagnostic Tools window on the right-hand side of the screen the runtime is incorrect. The actual runtime is 3h 11m 15s. NOTE: In the "Process Memory" section of the Diagnostic Tools on the right-hand side, this has been moved over to the left and isn't showing the memory utilization at the time of the crash. See below for that screenshot.
During normal operation, the application does not use more than ~300MB of memory. Once we've been running for over 3 hours, this spikes to >3GB when calling
JsonConvert.SerializeObject
.IMPORTANT!: My primary development machine is Windows-based, with 16GB of physical memory. Our server where this software is typically deployed is a Linux machine with 8GB of physical memory. The issue persists on both.
Source/destination types
Source/destination JSON
Expected behavior
JsonConvert.SerializeObject
does not spike memory usage 10x.Actual behavior
When calling
JsonConvert.SerializeObject
, memory utilization spikes from ~250MB up to 3GB+, creating an OutOfMemory exception. On Linux, the process is killed and the system error log reports that the application was attempting to use over over 250GB of memory.Steps to reproduce
Regarding the comment made about increments of 1000, 2000, etc - This issue still occurs after roughly 3hrs regardless of the increment.