Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Scala's Lift manage state?

I'm quite impressed by what Lift 2.0 brings to the table with Actors and StatefulSnippets, etc, but I'm a little worried about the memory overhead of these things. My question is twofold:

  1. How does Lift determine when to garbage collect state objects?
  2. What does the memory footprint of a page request look like?

If a web crawler dances across the footprint of the site, are they going to be opening up enough state objects to drown out a modest VPS (512M)? The question is very obviously application dependent, but I'm curious if anyone has any real world figures they can throw out at me.

like image 546
Stefan Mai Avatar asked Aug 22 '10 07:08

Stefan Mai


2 Answers

Lift stores state information in a session, so once the session is destroyed the state associated with that session goes away.

Within the session, Lift tracks each page that state is allocated for (e.g., mapping between an ajax button in the browser and a function on the server) and have a heart-beat from the browser. Functions for pages that have not seen the heartbeat in 10 minutes are unreferenced so the JVM can garbage collection them. All of this is tunable, so you can change heart-beat frequency, function lifespan, etc., but in practice the defaults work quite well.

In terms of session explosion, yeah... that's a minor issue. Popular sites (including http://demo.liftweb.net/ ) experience it. The example code (see http://github.com/lift/lift/tree/master/examples/example/ ) detects sessions that were created by a single request and then abandoned and expires those early. I'm running demo.liftweb.net with 256MB of heap size (that'd fit in a 512MB VPS) and occasionally, the session count rises over 1,000, but that's quickly tamped down for search engine traffic.

like image 61
David Pollak Avatar answered Sep 19 '22 06:09

David Pollak


I think the question about memory footprint was once answered somewhere on the mailing list, but I can’t find it at the moment.

Garbage collection is done after some idle time. There is, however, an example on the wiki which uses some better heuristics to kill off sessions spawned by web crawlers.

Of course, for your own project it makes sense to check memory consumption with something like VisualVM while spawning a couple of sessions yourself.

like image 43
Debilski Avatar answered Sep 19 '22 06:09

Debilski