Closed scyzoryck closed 1 year ago
what happens if you call gc_collect_cycles
? does the memory go down to the expected value?
for ($i = 0; $i <= $this->iterations; $i++) {
$this->serializer->serialize($this->collection, $this->format, $this->createContext());
gc_collect_cycles();
}
+----------------------+----------+
| benchmark | mem_peak |
+----------------------+----------+
| JsonMultipleRunBench | 1.524mb |
| JsonSingleRunBench | 1.524mb |
+----------------------+----------+
for ($i = 0; $i <= $this->iterations; $i++) {
$this->serializer->serialize($this->collection, $this->format, $this->createContext());
}
gc_collect_cycles();
+----------------------+----------+
| benchmark | mem_peak |
+----------------------+----------+
| JsonMultipleRunBench | 21.114mb |
| JsonSingleRunBench | 1.524mb |
+----------------------+----------+
Thanks @simPod for feedback. Fixed.
I understand the issue here... thanks for working on it... the issue is that the context has the graph navigator instance and vice versa, creating a circular reference... and it does not get collected until the PHP's garbage collector kick's in... but the PHP garbage collector is layz and it waits a lot before starting to do its job.
Would it be an option to unset the context from the graph navigator at the end of the serialization process? (or vice versa, unsetting it from the context?)
Thanks for feedback. If I recall - I tried it, but didn't work. I will check it in upcoming days.
@goetas you were right. :) I pushed branch with unsetting properties. It looks like we can improve it without BC! :)
+----------------------+----------+
| benchmark | mem_peak |
+----------------------+----------+
| JsonMultipleRunBench | 3.525mb |
| JsonSingleRunBench | 3.523mb |
+----------------------+----------+
Background:
First step to fix the issues is to make it visible - so let's add some tests that will visualize the issue.
As we can see in results - after 10000 iterations memory usage is rising from
1.524mb
to :19.439mb
- +1275%. for Json serialization35.546mb
- +2332%. for XML serializationSource of the issue:
PHP is not handling well circular dependencies between classes - it requires Garbage Collector the cleanup them. In result some objects are not cleaned up after serialization what might be source of the issue for long running processes that are using this library (for example in Consumers).
Expected result
Memory usage should not rise when running serialization multiple time. I was able to achieve it with some PoC - however it needs some work to avoid BC breaks.