Re: XMLHttpRequest and memory leak

103 views
Skip to first unread message

Chris McDonough

unread,
Jul 6, 2012, 9:28:09 AM7/6/12
to pylons-...@googlegroups.com
On 07/06/2012 06:44 AM, R�mi Chaintreuil wrote:
> Hi,
>
> I'm developping a monitoring application, which use a classic
> XMLHttpRequest object to get data from the server every second.
>
> The object is calling a route ('/test', for instance), and the
> view_callable is simply returning a Response object built from a string
> (which is generated by the application).
>
> I've realized that my application's memory use was increasing over time,
> and after several tests, I can tell that this memory leak does not come
> from the generation of the string itself, but from the view_callable
> processing.
>
> To confirm it, I've created a simple project who looks like this :
>
> *__init__.py*
>
> from pyramid.config import Configurator
>
> def main(global_config, **settings):
> """ This function returns a Pyramid WSGI application.
> """
> config = Configurator(settings=settings)
> config.add_static_view('static', 'static', cache_max_age=3600)
> config.add_route('home', '/')
> config.add_route('test','/test')
> config.scan()
> return config.make_wsgi_app()
>
>
> *views.py*
>
> from pyramid.view import view_config
> from pyramid.response import Response
> import random
>
> @view_config(route_name='home', renderer='templates/mytemplate.pt')
> def my_view(request):
> return {}
>
> @view_config(route_name='test')
> def test_view(request):
> return Response(str(random.random()))
>
>
> *mytemplate.pt*
>
> <p id="test"></p>
> <script type="text/javascript">
>
> function test() {
> var xhr = new XMLHttpRequest();
>
> xhr.onreadystatechange = function() {
> if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
> document.getElementById('test').innerHTML = xhr.responseText;
> window.setTimeout(test, 500);
> }
> };
> xhr.open("GET", '/test', true);
> xhr.send(null);
> }
>
> test();
>
> </script>
>
>
> The memory use of this application is indeed increasing over time,
> especially when a lot of clients are connected.
>
> Is Pyramid caching the response of my test_view in memory, and not
> releasing it properly when the response changes ?
> What would be the best way to achieve the same behaviour without having
> such an increase of memory use ?

Be sure you're not using the Pyramid debugtoolbar in this configuration.

- C

Rémi Chaintreuil

unread,
Jul 9, 2012, 5:14:11 AM7/9/12
to pylons-...@googlegroups.com
Le vendredi 6 juillet 2012 15:28:09 UTC+2, Chris McDonough a écrit :
Be sure you're not using the Pyramid debugtoolbar in this configuration.

- C

Thanks for your response.

I already removed the debugtoolbar. I've tried other templates engines (Jinja2 and Mako), but the increase of memory use remains. I also forgot to add xhr.close in my Javascript, but it doesn't solve the problem either.

Is Pyramid caching the responses the same way the compiled templates are cached in memory, and clearing this cache only on deployment ? In this case, how can I prevent this behaviour ?

Chris McDonough

unread,
Jul 9, 2012, 7:53:18 AM7/9/12
to pylons-...@googlegroups.com
On 07/09/2012 05:14 AM, R�mi Chaintreuil wrote:
> Le vendredi 6 juillet 2012 15:28:09 UTC+2, Chris McDonough a �crit :
>
> Be sure you're not using the Pyramid debugtoolbar in this
> configuration.
>
> - C
>
>
> Thanks for your response.
>
> I already removed the debugtoolbar. I've tried other templates engines
> (Jinja2 and Mako), but the increase of memory use remains. I also forgot
> to add xhr.close in my Javascript, but it doesn't solve the problem either.
>
> Is Pyramid caching the responses the same way the compiled templates are
> cached in memory, and clearing this cache only on deployment ?

No.

In this
> case, how can I prevent this behaviour ?

It doesn't do that.

- C


>
> --
> You received this message because you are subscribed to the Google
> Groups "pylons-discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pylons-discuss/-/fRQkg6hUnTAJ.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-discus...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.


Reply all
Reply to author
Forward
0 new messages