How does Scala's Lift manage state?
Asked Answered
C

2

6

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.

Caress answered 22/8, 2010 at 7:37 Comment(0)
E
12

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.

Environment answered 24/8, 2010 at 12:33 Comment(1)
Answer from the author, I'm honored. Thanks for the details and for a wonderful project!Caress
Z
1

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.

Zoogeography answered 22/8, 2010 at 12:46 Comment(1)
Don't forget VisualGC inside the jvmstat tool.Dartboard

© 2022 - 2024 — McMap. All rights reserved.