#244 Memory leaks and execution time increases

v1.0 (example)
closed-fixed
Holger Vogt
frontend (66)
5
2013-06-21
2013-06-06
mk
No

I encountered some when repeatedly executing a spice code for transient analysis.

I saw it in two situation:

1. when using ngspice as shared library (here from rlab), repeated execution of the same circuit through
command sequence

ret = ((int * (*)(char**)) ngSpice_Circ_handle)(circarray);
ret = ((int * (*)(char*)) ngSpice_Command_handle)("bg_run");

increases memory footprint of rlab, and increases in execution time (in my case for the file "r1.ckt" below, by 0.4 second for each call)

2. increase in memory footprint comes from ngspice, and I verified it by running ngspice from command line, looking at the output of the "top" command, and then repeatedly typing the same file name, here "r1.ckt".

Am I doing something wrong?

Discussion

  • mk
    mk
    2013-06-06

    transient analysis circuit that causes problems

     
    Attachments
  • Holger Vogt
    Holger Vogt
    2013-06-07

    • assigned_to: nobody --> h_vogt
    • status: open --> open-invalid
     
  • Holger Vogt
    Holger Vogt
    2013-06-07

    Do not use a .control section, which itself starts a simulation (by the 'tran' command), but load a file like:

    * rc circuit: 2 rc circuits connected by a switch
    * file r1_new.ckt
    v1 1 0 dc 5
    r1 1 2 1e+08
    c1 2 0 1.25e-10
    r2 4 5 1200
    c2 5 0 2e-10
    vc 3 0 0 pulse(0 1 0 1n 1n 5u 1s)
    s1 2 4 3 0 switch
    .model switch sw(vt=0.09 ron=0.01)
    .ic v(2)=5 v(4)=0 v(5)=0
    .tran 0.05ns 2000ns
    .end

    and then issue the commands 'bg_run' starting the simulation, and 'write' ... via the interface. This is the preferred way.

    This is similar to the procedure loading the above circuit into ngspice by typing 'r1_new.ckt' on the command line, then type 'run', later type 'write'.

    If you do your experiment 2, and then type 'setplot' and 'setcirc', you will see that there is no memory leak, but all the old simulation results (shown with setplot) and all the circuits (several times the same, as shown by setcirc) are all kept in memory.

    Holger

     
  • mk
    mk
    2013-06-07

    • status: open-invalid --> open-wont-fix
     
  • mk
    mk
    2013-06-07

    H,

    thank you for clarification of some commands.

    I rewrote the script used in testing the ngspice shared library. During the first execution of the script the entire circuit is passed to the ngspice. The circuit is given by what you suggested, that is,

    BEGIN SCRIPT
    * rc circuit: 2 rc circuits connected by a switch
    v1 1 0 dc 5
    r1 1 2 1e+08
    c1 2 0 1.25e-10
    r2 4 5 1200
    c2 5 0 1e-13
    vc 3 0 0 pulse(0 1 0 1n 1n 5u 1s)
    s1 2 4 3 0 switch
    .model switch sw(vt=0.09 ron=0.01)
    .ic v(2)=5 v(4)=0 v(5)=0
    .tran 0.05ns 200ns
    .end
    END SCRIPT

    in consecutive calls to perform the calculation, I just alter the value of single parameter:

    BEGIN COMMAND
    alter c2= ... some new value ...
    bg_resume
    END COMMAND

    These are the timing I obtain, where the first column is a duration of execution of (first time)

    ngspice_ret = ((int * (*)(char**)) ngSpice_Circ_handle)(circarray);
    ngspice_ret = ((int * (*)(char*)) ngSpice_Command_handle)("bg_run");

    or just (all the other times),

    ngspice_ret = ((int * (*)(char*)) ngSpice_Command_handle)(s1->s[i]);

    second column is the duration of retrieval of the variables, while the third is just timing of the fitting procedure (irrelevant here),

    1. Iteration
    0.063069 0.000678 0.009038
    0.06205 0.000695 0.005026
    0.06172 0.000653 0.006924
    0.061588 0.000225 0.009
    0.06156 0.000183 0.013301
    0.061579 8.1e-05 0.025743
    0.061677 0.000565 0.051008
    0.061587 7.8e-05 0.122126
    0.061634 8.2e-05 0.185781
    0.071828 0.000305 0.263582
    0.071702 7.9e-05 0.309739
    _________________________________________________
    0.699994 0.003624 1.001268
    Calculation using files shared object library lasted 1.73924 sec!

    2. Iteration
    0.071694 8.6e-05 0.004312
    0.071654 9.4e-05 0.005266
    0.07181 0.000248 0.007389
    0.071655 9.8e-05 0.009816
    0.071635 7.4e-05 0.014249
    0.07182 0.000282 0.028949
    0.071626 9.4e-05 0.048449
    0.071666 8.3e-05 0.112334
    0.061656 0.000268 0.162655
    0.071713 7.3e-05 0.261676
    0.081829 0.000245 0.307051
    _________________________________________________
    0.788758 0.001645 0.962146
    Calculation using files shared object library lasted 1.77852 sec!

    3. Iteration
    0.071768 0.00028 0.004646
    0.081821 0.000251 0.004687
    0.08192 0.000261 0.007025
    0.081856 0.000255 0.008459
    0.081839 0.000239 0.019955
    0.071673 0.0001 0.031172
    0.081766 0.000263 0.04791
    0.081738 7.9e-05 0.106718
    0.081986 0.000245 0.170942
    0.081975 0.000248 0.267266
    0.092073 0.000266 0.305462
    _________________________________________________
    0.890415 0.002487 0.974242
    Calculation using files shared object library lasted 1.88601 sec!

    4. Iteration
    0.092095 0.000272 0.007449
    0.091802 0.000104 0.005548
    0.092011 0.000249 0.007026
    0.091814 9.8e-05 0.009436
    0.091793 8.1e-05 0.014182
    0.092028 0.000251 0.034779
    0.091819 7.8e-05 0.050433
    0.091816 7.9e-05 0.107292
    0.091823 8.5e-05 0.176706
    0.101913 8.3e-05 0.261893
    0.091875 0.000149 0.316832
    _________________________________________________
    1.020789 0.001529 0.991576
    Calculation using files shared object library lasted 2.02971 sec!

    5. Iteration
    0.102175 0.000265 0.007535
    0.101965 0.000289 0.005311
    0.102098 0.000243 0.008133
    0.10189 0.000103 0.008523
    0.10186 0.000228 0.01228
    0.101861 7.6e-05 0.035741
    0.091835 7.9e-05 0.051435
    0.10191 8.9e-05 0.10739
    0.112131 0.000253 0.171426
    0.101945 8.2e-05 0.266696
    0.101944 8.6e-05 0.313995
    _________________________________________________
    1.121614 0.001793 0.988465
    Calculation using files shared object library lasted 2.12745 sec!

    6. Iteration
    0.102069 0.000347 0.002463
    0.102067 0.000303 0.004588
    0.112223 0.00025 0.00617
    0.111963 0.000104 0.00843
    0.101871 8e-05 0.014244
    0.111971 0.000103 0.034509
    0.11197 8.3e-05 0.051243
    0.111958 8.1e-05 0.107423
    0.112015 7.9e-05 0.171836
    0.112001 8e-05 0.267527
    0.122397 0.000249 0.313881
    _________________________________________________
    1.212505 0.001759 0.982314
    Calculation using files shared object library lasted 2.21138 sec!

    7. Iteration
    0.112015 9e-05 0.009443
    0.122303 0.00025 0.005707
    0.12238 0.000252 0.006979
    0.11198 0.000116 0.009769
    0.111934 7.9e-05 0.013499
    0.122194 0.000244 0.034639
    0.122217 0.000245 0.049991
    0.122057 7.9e-05 0.105808
    0.122079 0.000133 0.173922
    0.132167 8.2e-05 0.266726
    0.132482 0.000245 0.309919
    _________________________________________________
    1.333808 0.001815 0.986402
    Calculation using files shared object library lasted 2.33654 sec!

    8. Iteration
    0.132143 8.8e-05 0.003663
    0.132226 0.000243 0.004215
    0.132076 8.3e-05 0.007443
    0.122123 0.000247 0.009553
    0.121996 7.7e-05 0.013615
    0.122046 0.000106 0.028632
    0.122054 9.9e-05 0.054885
    0.132222 0.000245 0.108758
    0.132246 0.000246 0.171865
    0.142593 0.000245 0.265916
    0.142301 9.8e-05 0.306711
    _________________________________________________
    1.434026 0.001777 0.975256
    Calculation using files shared object library lasted 2.42748 sec!

    9. Iteration
    0.142457 0.000255 0.005033
    0.142205 0.000285 0.004445
    0.142562 0.000245 0.006597
    0.132128 0.000101 0.009408
    0.132071 7.7e-05 0.013584
    0.13213 0.000105 0.027842
    0.132275 0.000255 0.054818
    0.132327 0.000321 0.107073
    0.142393 0.000245 0.172024
    0.15236 8e-05 0.269663
    0.152662 0.00024 0.307328
    _________________________________________________
    1.53557 0.002209 0.977815
    Calculation using files shared object library lasted 2.53287 sec!

    10. Iteration
    0.152331 9e-05 0.004083
    0.15236 0.000249 0.004106
    0.152692 0.000243 0.006428
    0.14236 0.000252 0.008257
    0.14242 0.000315 0.011799
    0.142207 0.000103 0.028116
    0.14222 0.000107 0.055351
    0.142226 8e-05 0.11024
    0.162434 0.000296 0.169977
    0.152359 8.2e-05 0.269547
    0.162478 0.000103 0.309243
    _________________________________________________
    1.646087 0.00192 0.977147

    As you can see, the execution times drift up.

    Do you know what is happening?
    With the script reorganized, the existing circuit parameter is modified, so there should be no increase in execution time.

    Regards,
    mk

     
  • Holger Vogt
    Holger Vogt
    2013-06-08

    There is some "memory" of previous simulations, if you do repeated simulation on a circuit loaded once at the beginning.

    Try adding the reset command in between (be careful to run alter after reset), like

    BEGIN COMMAND
    reset
    alter c2= ... some new value ...
    bg_run
    END COMMAND

    Holger

     
  • mk
    mk
    2013-06-08

    H,
    following your advice I modified second and subsequent calls to ngspice shared library as
    in consecutive calls to perform the repeated calculation,

    BEGIN COMMAND
    reset
    alter c2= ... some new value ...
    bg_resume
    END COMMAND

    As you can see the overall timing problem remains. The execution time in ngspice slowly creep upwards, just as was the case without 'reset' command.

    Any other suggestions?

    mk

    1. Iteration
    0.061831 0.000256 0.005681
    0.061709 0.000673 0.004457
    0.061813 0.000705 0.006658
    0.061624 0.000232 0.008836
    0.061665 0.000663 0.011051
    0.061793 0.000259 0.030716
    0.061689 0.000211 0.054673
    0.061769 0.000271 0.121009
    0.062018 7.8e-05 0.185364
    0.071829 8.4e-05 0.2657
    0.071964 0.00025 0.307463
    _________________________________________________
    0.699704 0.003682 1.001608
    Calculation using files shared object library lasted 1.74101 sec!
    2. Iteration
    0.071827 8.2e-05 0.005672
    0.071815 0.000255 0.004965
    0.071927 0.000247 0.008125
    0.071754 9.8e-05 0.008741
    0.071732 8e-05 0.014228
    0.071746 8.1e-05 0.028668
    0.071776 0.000318 0.047372
    0.07178 9.3e-05 0.113442
    0.071975 0.000269 0.162362
    0.071847 0.000179 0.259559
    0.082127 0.000242 0.306351
    _________________________________________________
    0.800306 0.001944 0.959485
    Calculation using files shared object library lasted 1.78809 sec!
    3. Iteration
    0.081943 0.000178 0.00645
    0.081872 0.000242 0.005127
    0.08259 0.000313 0.006668
    0.082037 0.000243 0.008902
    0.081805 7.4e-05 0.021286
    0.071794 8.5e-05 0.031182
    0.08197 0.000317 0.046711
    0.08188 9.2e-05 0.105227
    0.082014 0.000295 0.169575
    0.092118 0.000245 0.258293
    0.081918 0.000114 0.309714
    _________________________________________________
    0.901941 0.002198 0.969135
    Calculation using files shared object library lasted 1.88879 sec!
    4. Iteration
    0.092256 0.000263 0.007104
    0.092077 0.000253 0.005108
    0.092179 0.000243 0.006908
    0.092067 0.000237 0.009543
    0.092107 0.000257 0.011505
    0.091958 0.000106 0.032892
    0.092046 0.000267 0.051644
    0.102279 0.00024 0.104746
    0.102229 0.000247 0.170706
    0.092146 9.7e-05 0.257744
    0.102067 8.8e-05 0.304498
    _________________________________________________
    1.043411 0.002298 0.962398
    Calculation using files shared object library lasted 2.02473 sec!
    5. Iteration
    0.102102 0.000262 0.003566
    0.102032 0.00024 0.004353
    0.102245 0.00024 0.00687
    0.10201 9.8e-05 0.008589
    0.091934 9.6e-05 0.013483
    0.102032 0.000182 0.033784
    0.102054 9e-05 0.049432
    0.112261 0.00029 0.107881
    0.112358 0.000255 0.179902
    0.112374 0.000308 0.264347
    0.102091 9.2e-05 0.308075
    _________________________________________________
    1.143493 0.002153 0.980282
    Calculation using files shared object library lasted 2.14473 sec!
    6. Iteration
    0.11216 8.5e-05 0.008647
    0.112117 0.00012 0.005733
    0.112984 0.000156 0.002933
    0.102291 0.000254 0.011938
    0.102308 0.000273 0.014415
    0.11222 0.00011 0.034801
    0.112481 0.000257 0.048453
    0.12284 0.000105 0.111932
    0.122484 0.000297 0.176609
    0.122566 0.000276 0.261485
    0.12237 0.000238 0.302196
    _________________________________________________
    1.256821 0.002171 0.979142
    Calculation using files shared object library lasted 2.25337 sec!
    7. Iteration
    0.122231 0.000158 0.004832
    0.121144 0.000105 0.002163
    0.11251 0.000252 0.002821
    0.112368 0.000272 0.007929
    0.112191 0.000106 0.019136
    0.112398 0.000304 0.036512
    0.122532 0.000313 0.04839
    0.132571 0.000278 0.106197
    0.132597 0.000281 0.169171
    0.132731 0.00024 0.265055
    0.132716 0.00028 0.304197
    _________________________________________________
    1.345989 0.002589 0.966403
    Calculation using files shared object library lasted 2.3334 sec!
    8. Iteration
    0.13233 8e-05 0.003185
    0.132393 0.000242 0.003973
    0.122203 9.8e-05 0.007894
    0.122459 0.000292 0.007459
    0.122168 7.8e-05 0.01423
    0.122401 0.000247 0.027362
    0.132272 8.1e-05 0.054341
    0.142375 8.7e-05 0.106405
    0.142357 0.00025 0.170361
    0.132506 0.000276 0.261472
    0.142459 0.000118 0.3028
    _________________________________________________
    1.445923 0.001849 0.959482
    Calculation using files shared object library lasted 2.42191 sec!
    9. Iteration
    0.14268 0.000257 0.008017
    0.132561 0.000265 0.005783
    0.142751 0.000242 0.005784
    0.132326 0.000126 0.006735
    0.132516 0.00025 0.014111
    0.132574 0.000267 0.026952
    0.142825 0.000291 0.052838
    0.152821 0.000332 0.10751
    0.142752 0.000252 0.171681
    0.152669 9.8e-05 0.262137
    0.152863 0.000242 0.309387
    _________________________________________________
    1.559338 0.002622 0.970935
    Calculation using files shared object library lasted 2.55165 sec!
    10. Iteration
    0.153468 0.0001 0.002415
    0.152776 0.00011 0.001411
    0.142511 0.000106 0.002747
    0.142763 0.000252 0.011636
    0.142737 0.000239 0.013221
    0.142705 0.000285 0.026751
    0.152852 0.000259 0.053959
    0.16313 9.1e-05 0.104063
    0.152882 0.000103 0.168179
    0.153056 0.000247 0.26353
    0.162876 0.000122 0.30007
    _________________________________________________
    1.661756 0.001914 0.947982
    Calculation using files shared object library lasted 2.62432 sec!

     
  • mk
    mk
    2013-06-08

    • status: open-wont-fix --> open
     
  • Holger Vogt
    Holger Vogt
    2013-06-08

    Remove the output if no longer needed with
    destroy all
    like

    BEGIN COMMAND
    reset
    alter c2= ... some new value ...
    bg_run
    write file vec
    destroy all
    END COMMAND

    Holger

     
  • mk
    mk
    2013-06-09

    • status: open --> open-fixed
     
  • mk
    mk
    2013-06-09

    H,

    this is to confirm that your suggestion fixes the problem.

    A script that is going to be executed repeatedly should start with a command,

    "destroy all"

    which cleans the memory of previous circuits, et c.

    If the command is not issued then the ngspice collates the circuits in the memory, its memory footprint increases, and so does the execution time.

    Thank you for your time and effort, I appreciate it.

    mk

     
  • Holger Vogt
    Holger Vogt
    2013-06-21

    • status: open-fixed --> closed-fixed
    • Group: --> v1.0 (example)