Here is Colab Notebook

After 1500 episodes if batch_size=256, the RAM crashed. With Colab, I have the equivalent of 25.5 gigs of RAM. Is it normal? Or I don't have enough ram? I thought at a certain point, the RAM must free a bit of space. How can I fix that?


2 Answers 2


If RAM memory is a problem during training, and you have run for 1500 episodes, it would suggest to me that you are saving some information in each episodes (e.g. appending to a list). That will slowly add up over time and cause a crash.

After a quick look at your code, it looks like the scores and ep_history grow with every loop. You could consider writing that information to disk e.g. to a json, file or pickle object, with something like this

import pickle

if episode % 1000 == 0:
    with open(f"./results_dir/scores_{episode}.pkl", "wb") as file_handle:
        pickle.dump(scores, file_handle)

You should of course retain the amount of history required for your print() statements and computing average scores over time.

You rarely need to worry about freeing up some RAM yourself, as Python (and then afterwards, the operating system itself) will start to allow memory to be overwritten. The results is the same, they are essentially being deleted.


Batch size is one of the key factors in deciding RAM utilization because backpropagation happens only after the batch. till then all the temporary state has to be saved.

Check this answer

Also, we don't need a batch size = 256 in general.

  • $\begingroup$ If we're listing static parameters like batch wise, which people should consider regarding RAM limitations: model size, optimizer, activations,, loss function... All of these (and more) can manage different numbers of parameters and require different amounts of memory when computing gradients! $\endgroup$
    – n1k31t4
    Commented Apr 12, 2020 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.