# Experiment crashes when it runs for too long

**URL:** <https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656>\
**Category:** Coding\
**Created:** [June 7, 2018, 12:43pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656 "2018-06-07T12:43:11Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_Leak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sam_leak/32/2347_2.png) [@Sam\_Leak](https://discourse.psychopy.org/u/Sam_Leak)\
**Post date:** [June 7, 2018, 12:43pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/1 "2018-06-07T12:43:11Z")

</div>

My experiment crashes when it is run for a long-ish time (c.25-30 mins). In a previous post ([Crashes after 30 mins/ wavs have clicks/ can’t use MIDI files](https://discourse.psychopy.org/t/crashes-after-30-mins-wavs-have-clicks-cant-use-midi-files/4311) ) I considered the possibility that it was because of the amount of data being saved. However I’ve now got it printing the data to an excel file after every 5 blocks, and resetting the experiment handler after it’s done this. I’ve noticed that if I complete the experiment quickly, I can get up to 10 blocks before crashing (with 2 excel files thus saved), but that if I complete it slowly it won’t even reach 5 blocks. As such, I don’t think it’s an issue with the amount of data being saved…

Trouble shooting, what other cause might there be for this? Perhaps something to do with clocks? Has anyone else had the experience of their experiment crashing after a certain period of time?

It’s basically an experiment that plays participants a series of sounds and asks them to recognise them.

---

<div class="post-metadata">

**Author:** ![frank.papenmeier](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@frank.papenmeier](https://discourse.psychopy.org/u/frank.papenmeier)\
**Post date:** [June 7, 2018, 2:06pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/2 "2018-06-07T14:06:44Z")

</div>

Have you checked whether there is a memory leak? Open the task manager and check whether the amount of RAM used by your experiment increases over time. Once the RAM is filled up, your experiment will crash and the the RAM will be released (if your issue is caused by a memory leak).

---

<div class="post-metadata">

**Author:** ![Sam\_Leak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sam_leak/32/2347_2.png) [@Sam\_Leak](https://discourse.psychopy.org/u/Sam_Leak)\
**Post date:** [June 9, 2018, 12:10pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/3 "2018-06-09T12:10:48Z")

</div>

Thanks for that. Yes the memory usage does seem to be continually ramping up as I use it. When it eventually crashes it crashes the whole computer, so it’s relatively severe!

Any idea as to what might be causing a memory leak? I tried writing in code that saves the data from the experiment handler, then resets the experiment handler, every 5 sets of trials - doesn’t appear to have fixed anything…

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [June 10, 2018, 3:44am UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/4 "2018-06-10T03:44:59Z")

</div>

It seems very unlikely that it is the accumulation of data that is causing the problem. For most people, the experiment handler is just storing a relatively small amount of text/numeric data per trial, which isn’t going to tax your computer’s memory.

But somehow you seem to have isolated the memory memory leak to the ExperimentHandler? If that is the case, I wonder if you are inadvertently storing some of your actual sound stimuli within the ExperimentHandler, rather than just the names of their associated files? I tried to look at the code for the minimal reproducible example you posted here:

> [@Crashes after 30 mins/ wavs have clicks/ can't use MIDI files](https://discourse.psychopy.org/t/crashes-after-30-mins-wavs-have-clicks-cant-use-midi-files/4311/13):
>
> Here you go everyone: [https://we.tl/zwNrl9kwlg](https://we.tl/zwNrl9kwlg) I’ve isolated the (large) bit of code that’s causing me all of the problems, and I’ve reduced the number of .wav files (significantly) to the smallest number at which the experiment structure makes sense. The error message, possibly due to ordered dicts, is still appearing if I open it in PsychoPy v1.90.1, and it does not appear if I open it in v1.85.6 - so hopefully you’ll find that useful @Michael and @dvbridges ? Jonathan - the variable that ke…

but unfortunately that link is no longer live.

---

<div class="post-metadata">

**Author:** ![Sam\_Leak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sam_leak/32/2347_2.png) [@Sam\_Leak](https://discourse.psychopy.org/u/Sam_Leak)\
**Post date:** [June 10, 2018, 1:08pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/5 "2018-06-10T13:08:36Z")

</div>

Hi Michael

The ExperimentHandler was just a guess! What would you suggest me first port of call would be for working out where the memory leak is?

Thanks 🙂

Sam

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [June 10, 2018, 9:44pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/6 "2018-06-10T21:44:53Z")

</div>

As a first step, post your error messages here.

---

<div class="post-metadata">

**Author:** ![Sam\_Leak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sam_leak/32/2347_2.png) [@Sam\_Leak](https://discourse.psychopy.org/u/Sam_Leak)\
**Post date:** [June 11, 2018, 2:20pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/7 "2018-06-11T14:20:45Z")

</div>

Thanks Michael

I just ran if for 20 minutes before pressing escape to exit, hoping to collect some error messages, but didn’t get anything?

Weirdly it seems like I got some from a previous attempt though:

########## Running: /Users/sam/Downloads/AP\_experiment\_1/AP\_exp\_1.py ###########  
pyo version 0.8.6 (uses single precision)  
2018-06-09 17:58:58.264 python[3307:160605] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/k8/grc8crt12fg\_6cq4rwvvcl7r0000gn/T/org.psychopy.PsychoPy2.savedState  
14.4518 WARNING t of last frame was 20.02ms (=1/49)  
15.8141 WARNING t of last frame was 34.33ms (=1/29)  
15.9307 WARNING Couldn’t measure a consistent frame rate.

- Is your graphics card set to sync to vertical blank?
- Are you running other processes on your computer?

18.8743 WARNING t of last frame was 2943.81ms (=1/0)  
19.3572 WARNING t of last frame was 19.91ms (=1/50)  
20.6185 WARNING Multiple dropped frames have occurred - I’ll stop bothering you about them!

Any thoughts?

* * *

This is the actual code, in case that’s of any help (it’s prob too much info to be useful…) - [AP\_exp\_1\_simplified.py](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/1/1e295fcb83c50a6c674eaf55a0a5084ab722d907.py) (48.5 KB)

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [June 11, 2018, 11:26pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/8 "2018-06-11T23:26:29Z")

</div>

Those are warnings about graphics underperformance. We really need to see actual error messages.

Not too much point looking at the code until then, as these error messages might help zoom in on the source of the problem. But on an initial pass, this looks like a Builder-generated script. Are you just adding a code component to a Builder experiment, or have you done more major modifications to the script?

---

<div class="post-metadata">

**Author:** ![Sam\_Leak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sam_leak/32/2347_2.png) [@Sam\_Leak](https://discourse.psychopy.org/u/Sam_Leak)\
**Post date:** [June 12, 2018, 10:01am UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/9 "2018-06-12T10:01:27Z")

</div>

Hi Michael

I think the reason I don’t get error messages is that when it crashes it doesn’t just crash the experiment but actually crashes the whole computer losing everything… any thoughts?

I used the builder code as a starting place, but that’s about it - it’s mostly new code…

---

<div class="post-metadata">

**Author:** ![Sam\_Leak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sam_leak/32/2347_2.png) [@Sam\_Leak](https://discourse.psychopy.org/u/Sam_Leak)\
**Post date:** [June 12, 2018, 10:46am UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/10 "2018-06-12T10:46:37Z")

</div>

Perhaps the errors are saved somewhere in the .log file?!

The .log file is here, if that helps (too large to direct upload unfortunately (7.1MB)):

[https://we.tl/VSTczasP36](https://we.tl/VSTczasP36)

---

<div class="post-metadata">

**Author:** ![Sam\_Leak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sam_leak/32/2347_2.png) [@Sam\_Leak](https://discourse.psychopy.org/u/Sam_Leak)\
**Post date:** [June 15, 2018, 5:01pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/11 "2018-06-15T17:01:52Z")

</div>

Any thoughts on this anyone? I’m really struggling with it…

Thanks! Sam

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [June 16, 2018, 2:56am UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/12 "2018-06-16T02:56:07Z")

</div>

Anyone else will struggle with this too: given the size of the code and the lack of any error messages, it is basically impossible for anyone to suggest much when working remotely.

The usual suggestion here is to get things down to the absolute minimum in which you can reliably produce the error. I’d suggest working with a standard Builder file, with code added via a code component, rather than directly modifying the script. There’s just too much that can go wrong with manual edits like that, making things impossible to narrow down.

---

<div class="post-metadata">

**Author:** ![Sam\_Leak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sam_leak/32/2347_2.png) [@Sam\_Leak](https://discourse.psychopy.org/u/Sam_Leak)\
**Post date:** [June 16, 2018, 3:08pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/13 "2018-06-16T15:08:20Z")

</div>

Sure thing - thanks for your message. I wasn’t expecting you to get a lot from the code itself seeing as there is a lot of it, I only really posted that here cause you asked.

The issue is that you’d like to see the error messages, but I can’t get error messages cause it shuts down the whole computer when it crashes. So question 1 is: is there a foreseeable way in which I can get code that crashes the whole computer to generate error messages, before it does so, that you’d find helpful?

Question 2 is, presuming I can’t generate these error messages for you, if I’m going to troubleshoot this what are the standard kind of things that might cause a memory leak like this? (I take your point about going through it bit-by-bit etc, but it would be nice to have some inkling of a clue for what sort of things I might be looking for…)

Thanks

Sam

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [June 17, 2018, 4:19am UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/14 "2018-06-17T04:19:48Z")

</div>

> [@Sam\_Leak](#):
>
> in which I can get code that crashes the whole computer to generate error messages,

To be honest, it’s highly unusual to get a crash that brings down the whole computer. Modern operating systems are designed to constrain issues like this so that only the affected process gets terminated. So we’re flying a little blind here.

Similarly, on your second issue, memory leaks are also pretty rare in Python, as memory allocation is something that gets abstracted away from the programmer. It is possible to build long lists of large objects, though, and not get rid of the references to them after they are used. I seem to recall that you had a very long list of sound files? It would be worth checking that you aren’t creating a new sound stimulus for each, but instead just create a single stimulus, and point it to a new sound file each time as required. That ensures that you aren’t ever maintaining a reference to more than one sound object.

---

<div class="post-metadata">

**Author:** ![Sam\_Leak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sam_leak/32/2347_2.png) [@Sam\_Leak](https://discourse.psychopy.org/u/Sam_Leak)\
**Post date:** [July 16, 2018, 4:56pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/15 "2018-07-16T16:56:17Z")

</div>

I’m pretty sure I’m just creating a single stimulus

The paths to the sound files are specified in the trial handler:

```auto
trials = data.TrialHandler(nReps= repetitions, method='sequential', extraInfo=expInfo, originPath=-1,trialList= condition_selector(excel_file2, no_of_octaves, the_keys) , seed=None, name='trials')

```

condition\_selector is a function that I’ve written that takes a dictionary containing all of the possible conditions, and then picks a few of them for me. This is ignorable I think, but for interest’s sake here is the function:

```auto
def condition_selector(a_conditions_file, octaves, keys):
    new_conditions = []
    audioStimuli = []
    shuffle(a_conditions_file) 
    last_corrAns = 'blank'
    for hh in range(0, octaves):     
        shuffle(keys) 
        while keys[0] == last_corrAns: 
            shuffle(keys)
        for nn in keys:
            for jj in a_conditions_file:
                if jj['corrAns'] == nn and jj['corrAns'] != last_corrAns and jj['audioStim'] not in audioStimuli: 
                    new_conditions.append(jj)
                    audioStimuli.append( jj['audioStim'] )
                    last_corrAns = jj['corrAns']
    
    return new_conditions

```

In terms of actually summoning each audio file I just use the following within the loop for each individual trial:

```auto
audio_stimulus.setSound(audioStim, secs=-1)

```

I think that resets the sound for audio\_stimulus, thus clearing out the previous sound from it. As such I don’t think there’s anywhere where the sound files could be cumulating, causing a memory leak? What do you think? It’s a logical enough idea for explaining the memory leak, as I am indeed handling a lot of files, but I think each of them replaces the last on each iteration of the loop.

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [July 16, 2018, 10:17pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/16 "2018-07-16T22:17:32Z")

</div>

That sounds like you are doing the right thing then.

I think from here you need to look at using a memory profiler to show you the memory usage for various objects in your code. I’m not too familiar with them but there are a number of alternatives to use. If you are running this code from with the standalone PsychoPy, you will probably want to choose one that you call from within your code, rather than use it to run your code.

---

<div class="post-metadata">

**Author:** ![Sam\_Leak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sam_leak/32/2347_2.png) [@Sam\_Leak](https://discourse.psychopy.org/u/Sam_Leak)\
**Post date:** [August 3, 2018, 4:17pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/17 "2018-08-03T16:17:03Z")

</div>

So…

I first tried tracking down the size of the variables on each time round the trial loop as follows:

```auto
all_variables = dir()
for currentVariable in all_variables:
   print currentVariable
   print(sys.getsizeof(currentVariable))

```

All of the variables stayed the same size throughout, whilst the memory usage still ramped up…

Now attempting to find a suitable memory profiler…

I’ve decided I’ll have a go with objgraph (although if anyone has better recommendations then let me know) - [https://mg.pov.lt/objgraph/](https://mg.pov.lt/objgraph/)

I downloaded objgraph using pip on the main terminal (following this: [https://www.youtube.com/watch?v=yBdZZGPpYxg](https://www.youtube.com/watch?v=yBdZZGPpYxg) )

objgraph now works in IDLE, but if I call it in PsychoPy it doesn’t. Sorry if this is a stupid question, but how do I get it to work in PsychoPy?

Thanks!

---

<div class="post-metadata">

**Author:** ![alex116](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@alex116](https://discourse.psychopy.org/u/alex116)\
**Post date:** [February 20, 2019, 6:46pm UTC](https://discourse.psychopy.org/t/experiment-crashes-when-it-runs-for-too-long/4656/18 "2019-02-20T18:46:39Z")

</div>

I had this issue, and it turns out that the experiment was logging too much information about the data itself. If you go to “experiment settings”\>“data” and then uncheck “Save log file” this should help keep the experiment from crashing.
