softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

Application freezing #64

Closed Despair2000 closed 4 years ago

Despair2000 commented 4 years ago

Hi Abel,

I have a problem I have a hard time to debug.

I have an application that makes extensive use of IB4m. It subscribes to 60+ stocks and performs all kind of operations. Everything works fine but sometimes (maybe every other day) the program freezes. If this happens I can't close it. Hitting CTRL-C will give me strange java errors I don't understand but doesn't stop the program. I really have to close matlab and restart it. When I pause the running program at this point to come into matlabs debug-mode, I will usually end up in the processnotification.m and there I'm stuck. So when I try to trace forward from this point step by step or want to use the "step out" function to leave the process notification I can't. Looks little bit like calls to the process notification have accumulated on top of each other and leaving one just gives be back to another one.

I have no idea what is going on but it seems to be some part of IB4m that is causing this behavior. Do you have any idea?

abelatnvidia commented 4 years ago

The very first thing to try is increase this size of the java heap link

Debugging event based programs is very difficult. Moreover, just because you hit a break point in the Matlab debugger does not mean the events stop firing from TWS. Indeed, events will build up in the queue while sitting/paused at break point (which can cause its own problems).

Also, can you remind me which version of Matlab you are using?

Despair2000 commented 4 years ago

I already increased the heap size to 2 GB. This should be enough, shouldn't it?

I'm using the latest Matlab version (2019a) with all updates installed.

softwarespartan commented 4 years ago

it depends how much data you are streaming and for how long etc.

Can you describe more what you mean by "program freezes"?

Are you streaming market data or 5 sec bars, other?

Also, if you can post/send the java stack trace dump that you get, would be helpful.

Despair2000 commented 4 years ago

I usually run the app from 8:45 in the morning until 22:00 at night (it's trading both the UK and US market) and I'm streaming market data for say up to 60 stocks. (reqmarketdata) Or to be more exact, I'm streaming 20 UK stocks and 40 US stocks. In the morning it's only the UK stocks, then there are 2 hours when the UK and the US session overlap (so it's trading all 20 UK plus 40 US stocks) and after the UK market closed it continuous only with the 40 US stocks until the US market closes.

With "freeze" I mean the program becomes unresponsive. For example I have a GUI with a quit button. This won't work any longer. When I then try to close matlab (only way to stop the program) I get the response (at least under windows) "Matlab does not respond. Do you want to end now?". If I hit end now it takes a few seconds before I get a new windows with a slight different text (I can recall it exactly). When I then choose "End now" a second time matlab closes. This is how it goes when I start the application with the matlab desktop open. When I run the application in batch mode (no desktop), I usually have to use the task manager to stop matlab from running.

There is no possibility to return to matlab once the application has "frozen". Only closing matlab completely and restart it will work.

I will try to capture this java error dump for you. To see this I have to run it with desktop (usually I don't do so) and hit CTRL-C several times. When in batch mode hitting CTRL-C has no effect.

And I want to add that this "freezing" occurs at different times but most often during the time of the overlapping sessions when all stocks are traded. But also here not always. Yesterday for example the app ran all day without any problem. Today it froze once.

If I think little more, I think it is always freezing in this overlapping period.

Despair2000 commented 4 years ago

I now increased the java heap size to 3 GB (what is the maximum). Let's see if this helps. Would be very convenient if this could be resolved so easily.

Despair2000 commented 4 years ago

I want to add for clarification, it is not only my application that freezes, it seems to be whole Matlab. But let's wait and see how the days goes with 3GB heap space.

Despair2000 commented 4 years ago

I watched it now the whole week and the problem seems to be solved. Increasing the java heap space to 3GB was the solution. Still a little bit puzzled how my program can need so much memory but it works and this is the most important. Closing the issue.

softwarespartan commented 4 years ago

As much as you are streaming, it is no surprise.

Be sure to use the circular buffers where you can which will prevent too much accumulation when streaming.

Don’t hesitate to reopen issue if problem returns.

Cheers

Sent from my iPhone

On Sep 21, 2019, at 3:52 AM, Despair2000 notifications@github.com wrote:

I watched it now the whole week and the problem seems to be solved. Increasing the java heap space to 3GB was the solution. Still a little bit puzzled how my program can need so much memory but it works and this is the most important. Closing the issue.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

Despair2000 commented 4 years ago

As much as you are streaming, it is no surprise.

Be sure to use the circular buffers where you can which will prevent too much accumulation when streaming.

As much as I can see I only use circular buffers for the streaming. How can there be any accumulation? Shouldn’t the buffer simply throw out old observations when the specified capacity is reached and new data arrives? And on top I’m clearing the buffers all the time (after I retrieved the data that has arrived).

Have a great weekend!

Boris

softwarespartan commented 4 years ago

@Despair2000 I pushed updates today that should help address this issue. There were messages lingering too long in the internal event queue in the TWS message handler (under the hood in the jar file). The processNotification.m was updated to directly delete the event once retrieved by the MATLAB event listener thread. Note that I've not yet tested this on hardcore edge use-cases like multiple instances of MATLAB etc. so let me know if run into any issues.

Despair2000 commented 4 years ago

Awesome! All your improvements are highly appreciated as always! I will let you know if I experience any strange behaviour.

Med vänliga hälsningar,

Boris

Am 04.11.2019 um 10:22 schrieb Abel Brown notifications@github.com:

@Despair2000 https://github.com/Despair2000 I pushed updates today that should help address this issue. There were messages lingering too long in the internal event queue in the TWS message handler (under the hood in the jar file). The processNotification.m was updated to directly delete the event once retrieved by the MATLAB event listener thread. Note that I've not yet tested this on hardcore edge use-cases like multiple instances of MATLAB etc. so let me know if run into any issues.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/64?email_source=notifications&email_token=AJFOPM32NFWKQCW6HTUA743QR7ST5A5CNFSM4IUGNRQKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEC6T2WQ#issuecomment-549272922, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJFOPM5ILKMA3CYYJLJ4ETDQR7ST5ANCNFSM4IUGNRQA.