0

I am using multiprocessing.Process to load some images and store them in a shared memory as explained here. The problem is, sometimes my code crashes due to a huge memory spike at completely random times. I just had an idea of what might be causing this: the process does not have had enough time to copy the contents of the image into the shared memory in RAM by the time join(). To test my hypothesis I added time.sleep(0.015) after doing join() on each of my processes and this has already reduced the number of memory spikes by about 90% or more. However, I'm still not 100% certain whether not getting memory spikes as often is because that little amount of time could help the data to get fully transferred to the shared memory or not.

So I wonder, is there a way to make sure a child process has finished copying data to memory before .join() is called? I do not want to use a fixed number when calling time.sleep(). It would be great to to know when the data has been fully transferred to the shared memory and then do join().

Amir
  • 10,600
  • 9
  • 48
  • 75
  • 1
    post more context with a shared memory structure – RomanPerekhrest Jun 28 '19 at 20:23
  • @RomanPerekhrest Does [this](https://stackoverflow.com/questions/56745073/why-do-i-get-a-sudden-spike-in-memory-usage-when-using-multiprocessing-process-w) help? – Amir Jun 28 '19 at 20:24
  • How about using an event, and parent calls `join` once the event is received? – heemayl Jun 28 '19 at 20:26
  • @heemayl I've never used events. How can I do this? Does the event make sure the contents are fully transferred to the shared memory before I call `join()`? If so, can you provide some code for this? – Amir Jun 28 '19 at 20:28
  • @RomanPerekhrest I can definitely confirm that manually adding `time.sleep(0.015)` after `join()`ing a process has significantly reduced memory spikes; it still happens 10% of the time though. But I'm still not certain whether that is because that little amount of time could help the data to get fully transferred to the shared memory or not. – Amir Jun 29 '19 at 10:21
  • @Amir, did you check how's that adjusted time delay depends on different amount of data ? – RomanPerekhrest Jun 29 '19 at 10:31
  • @RomanPerekhrest I did a simple test for that and it seems that the higher the amount of data, the more likely I will run into the memory spike issue. – Amir Jun 29 '19 at 10:35
  • @RomanPerekhrest I wonder, should I manually lock an array when writing into it? I just saw some posts that seem to suggest people should start filling the `mp.Array` object using `with arr.get_lock()` statement. Although I'm never writing into the same array using multiple processes, but I'm afraid memory is not allocated to my jobs in the logical way that we think about it. So it's possible that sometimes two jobs on our server have overlapping physical memory addresses allocated to them? – Amir Jun 29 '19 at 10:38
  • I'm unsure how a sleep helps, `join`ing a process just waits (i.e. sleeps) until the target exits – Sam Mason Jun 29 '19 at 11:33

0 Answers0