in reply to How to free memory used by previous program?

Sourav, I apologize on behalf of the group for some of the “answers” (sic ...) that you have lately received here.   It is true that this is not a particularly well-designed program.   However, it may also be true that you did not write it and/or that there is no business justification for a rewrite, given that, as you say, it is doing a complex time-consuming thing and it works.   So, here are two specific tricks that I use in this kind of situation:

  1. Spin off a subset of the work into a per-request child process, following more-or-less exactly the excellent example previously given.   The operating system will tear-down a process completely.   (Note that threads are not equivalent since all threads run in the context of a parent process.)   Each unit of work needs to be self-contained with basically no need to communicate or to maintain any kind of global-state betwixt them.
  2. Parcel the primary unit or units of work into subroutines, and move as many variables as possible into local scope within those subroutines, parameterizing them as need be.   Studiously avoid globals.   The interpreter will clean-up the local variables of a subroutine, with a few caveats regarding memory-references and circular-references that I hope we will not need to get into here.

Both of these things – especially the first – are changes that can usually be wrapped-around “logic that works” with a reasonably small risk of de-stabilization.   Nevertheless, results should be aggressively (re-)tested.

Replies are listed 'Best First'.
Re^2: How to free memory used by previous program?
by Laurent_R (Canon) on Apr 02, 2014 at 21:29 UTC

    Sourav, I apologize on behalf of the group for some of the “answers” (sic ...) that you have lately received here. It is true that this is not a particularly well-designed program. However, it may also be true that you did not write it and/or that there is no business justification for a rewrite, given that, as you say, it is doing a complex time-consuming thing and it works.

    I have to strongly disagree with that.

    First, who are YOU to apologize on behalf of others? Come on, who do you think you are? You have the right to disagree and please do so if you wish, but don't apologize on behalf of others, this is just ridiculously stupid.

    Second, the program presented is poorly written, to say the least. Saying that fact is an acceptable “answer” (sic ...), whether you agree or not.

    Third, judging from the OP's post, there is a problem, it does not work properly. So please don't say it works. It obviously doesn't.

    Fourth, when you have such a messy program, there will necessarily be one day where you have to refactor it, may be it is time. Or maybe you want to do it next time. But you know it will have to be done.

    Fifth, the OP stated that the program presented is not the real one, but some pseudo-code; nobody knows if this pseudo code exhibits the same problem as the real code. Should I care? I don't see why...

    I could add another half a dozen other objections to your post, but I think that I have made the point clearly.