# Memory and Performance Issues with starting\_solution in PyBaMM for RL Environment

**URL:** <https://pybamm.discourse.group/t/memory-and-performance-issues-with-starting-solution-in-pybamm-for-rl-environment/270>\
**Category:** General\
**Created:** [25 March 2025 08:40 UTC](https://pybamm.discourse.group/t/memory-and-performance-issues-with-starting-solution-in-pybamm-for-rl-environment/270 "2025-03-25T08:40:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://pybamm.discourse.group/u/squirrel)\
**Post date:** [25 March 2025 08:40 UTC](https://pybamm.discourse.group/t/memory-and-performance-issues-with-starting-solution-in-pybamm-for-rl-environment/270/1 "2025-03-25T08:40:35Z")

</div>

Hello,

I’m trying to use PyBaMM to build a reinforcement learning environment, but I’ve encountered an issue. To allow the current to change in real-time based on the values provided by the agent, I adopted the following approach: continuously creating experiments and simulations, and using starting\_solution to ensure the next simulation continues from the previous result.

I want to account for aging in the loop, so this cycle will continue until the SOH reaches 0.8. However, possibly due to the nature of the starting\_solution feature, a large amount of data from the first cycle is retained, causing the program to eventually run out of memory and slow down significantly. How can I resolve this issue?

The code is like this:  
def step(self, action):  
self.current = action[0]  
experiment = pybamm.Experiment([f"Charge at {self.current} C for {self.dt} seconds"])  
self.simulation = pybamm.Simulation(  
self.model,  
parameter\_values=self.param,  
experiment=experiment,  
solver=self.solver  
)  
self.start = self.simulation.solve(starting\_solution=self.start, calc\_esoh=False)

---

<div class="post-metadata">

**Author:** ![DrSOKane](https://avatars.discourse-cdn.com/v4/letter/d/b5a626/32.png) [@DrSOKane](https://pybamm.discourse.group/u/DrSOKane)\
**Post date:** [25 March 2025 15:30 UTC](https://pybamm.discourse.group/t/memory-and-performance-issues-with-starting-solution-in-pybamm-for-rl-environment/270/2 "2025-03-25T15:30:17Z")

</div>

I noticed self.start contains a reference to self.start, which will trigger a reference cycle in Python’s memory management process. Is that intentional?

---

<div class="post-metadata">

**Author:** ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://pybamm.discourse.group/u/squirrel)\
**Post date:** [26 March 2025 02:09 UTC](https://pybamm.discourse.group/t/memory-and-performance-issues-with-starting-solution-in-pybamm-for-rl-environment/270/3 "2025-03-26T02:09:37Z")

</div>

Thanks for your response!  
Yes, this was intentional—I intended for each simulation cycle to continue from the last result to adjust the current in real-time. But with increasing cycles, memory usage grows until it crashes, and the slowdown becomes severe.  
What I wanted was real-time current adjustment (via the agent) on the same battery, but this implementation isn’t viable.

---

<div class="post-metadata">

**Author:** ![Marc](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Marc](https://pybamm.discourse.group/u/Marc)\
**Post date:** [26 March 2025 20:25 UTC](https://pybamm.discourse.group/t/memory-and-performance-issues-with-starting-solution-in-pybamm-for-rl-environment/270/4 "2025-03-26T20:25:08Z")

</div>

Hi @squirrel, can you try replacing

```python
 self.start = self.simulation.solve(starting_solution=self.start, calc_esoh=False)

```

with

```python
sol = self.simulation.solve(starting_solution=self.start, calc_esoh=False)
self.start = sol.sub_solutions[-1]

```

---

<div class="post-metadata">

**Author:** ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://pybamm.discourse.group/u/squirrel)\
**Post date:** [27 March 2025 02:34 UTC](https://pybamm.discourse.group/t/memory-and-performance-issues-with-starting-solution-in-pybamm-for-rl-environment/270/5 "2025-03-27T02:34:41Z")

</div>

Thank you for your reply! This solution works perfectly - I really appreciate your help!
