1

I was calling a python script (2.7) from a console (Ubuntu 14.04) using a command: python script_name.py. At some point, I wanted to stop the running script by pressing Ctrl-C. However, when I checked Ubuntu System Monitor, the memory used by the python script was not freed up (I monitored Ubuntu System Monitor before I called the script, during the process, and after I pressed Ctrl-C to stop the script). I tried to free up the memory using a command explained on http://www.upubuntu.com/2013/01/how-to-free-up-unused-memory-in.html , but didn't work (I mean, the memory usage was not changed).

However, if I used pycharm to run and stop the script, the memory was freed up directly once I pressed the Stop button. For some reasons (such as from ssh or just to test from console), I want to run my script from the console (without using pycharm or any other IDEs).

My question is, what is the command, or how to stop running python script and free up directly the memory used by the script, if I run the script from the console?

Many thanks in advance.

Eduard
  • 13
  • 4
  • 1
    Python is written in C. If the interpreter is still running, then this may be relevant: [Will malloc implementations return free-ed memory back to the system?](https://stackoverflow.com/q/2215259/608639) PyCharm may be architected a little different, and may fork a copy of itself to run the script that eventually exits. – jww Jun 26 '17 at 03:22
  • Thanks, but I am looking for Unix/Linux command to terminate the process and free the memory directly. But your input is interesting too! Thank you! – Eduard Jun 26 '17 at 23:19
  • Which process? You have not detailed one. Maybe the answer is detailed in [How can I see what processes are running?](https://unix.stackexchange.com/q/3340/56041) or [How to kill a process running in specific script](https://unix.stackexchange.com/q/151099/56041) from [Unix & Linux Stack Exchange](http://unix.stackexchange.com/). – jww Jun 26 '17 at 23:41
  • Also see [What if 'kill -9' does not work?](https://unix.stackexchange.com/q/5642/56041) on [Unix & Linux Stack Exchange](http://unix.stackexchange.com/). There's a better post somewhere that talks about a potential bug in the kernel where the kernel delivers the signal but its not processed due to the process's \[kernel\] state, but I cannot find it at the moment. – jww Jun 26 '17 at 23:56
  • Yes I have read those and finally I found the answer (in the comment for the answer below). Thank you very much for your reply! – Eduard Jun 27 '17 at 04:30

1 Answers1

0

Those commands did not work since what you're trying to achieve is not what they do. How did you check the memory being used by your Python script. I use top to see memory and could used by each process (sorted in ascending order by default). You may have checked before the system had time to register that the python process was killed, I've used this a lot and I've never tun into with the OS not getting memory back after a process has been killed with ctrl + c.

Pycharm is probably doing some cleanup when you stop the program from it versus just having to wait for the OS to reclaim memory versys when you SIGTERM a process from a shell

  • I used Ubuntu System Monitor (under "Memory" in "Memory and Swap History"). I can see that an amount of memory is used (increased) when I run the script (which is normal), but after I used Ctrl-C in the terminal, "that amount of memory" is still there. If I used Pycharm, "that amount of memory" is freed up after I pressed Stop button. I had used 'top' and the process is killed, but System Monitor still showed "that amount of memory" was not fred up. – Eduard Jun 26 '17 at 02:54
  • Never used the system monitor. How many times did you try this? If you're just looking at total memory usage then it's possible another process just snapped that memory as soon as Python let it go ( big coincidence needed). If it's per process, then again it's probably just there for a while until the memory goes back into the OS's free memory pool. – arsh chauhan Jun 26 '17 at 03:02
  • Well, the OS seems won't let freed up the memory even though the script has been stopped for a while. I only run my script (no other applications were running), and my script needs a large memory so every time I have to restart my linux (which is something I don't like/can't do) to get the memory back before I could re-run my script again. Any commands from the terminal to freed up the memory used by my python script (apparently the command from the link in my post didn't work)? – Eduard Jun 26 '17 at 03:17
  • Ok, I found the solution based on @arsh chauhan 's answer. I use `top` and then `kill -15 pid` (SIGTERM). Now I can free the memory without restarting my Ubuntu :-) Thank you! – Eduard Jun 26 '17 at 03:44