Skip to main content
January 11, 2011
Answered

Storing object refernces in memory, good idea or bad idea?

  • January 11, 2011
  • 2 replies
  • 888 views

Hey all,

These is probably a somehwhat basic question, but it's something i've wondered about for a while. Is it alright to store object references in a persistant scope, like application or server?

Basically just doing this once in application.cfm

<cfset application.siteMethods = createObject("component","components.siteMethods")>

then calling stuff like

<cfset serverInit = application.siteMethods.init(application.dsn)>

instead of having to reinstatiated it on every page it is required? Any input is appreciated. Thanks!

    This topic has been closed for replies.
    Correct answer JMF3

    Yes, in most cases that is not only all right, it is preferable. So long

    as you're not swamping server memory with persistent objects, you are

    potentially reducing load by quite a bit. In fact, a number of the

    application frameworks, like Model-Glue, are designed to do exactly this:

    non-instanced object classes are loaded into application scope for use

    across all requests.

    2 replies

    12Robots
    Participating Frequently
    January 11, 2011

    Yes, it is a good idea and a perfectly acceptable thing to do.

    There are of course, things to watch out for, but for the most part, you should be fine doing that. Just note that if you do that, EVERY user will be using the exact same instance of that object. The application scope is a shared scope. So if you store data in that object it should not relate to a specific user.

    January 11, 2011

    Thanks for the input guys. I kinda thought it was a decent idea, but since I just sorta... came up with it (I didn't see anyone else doing it) I wasn't sure. I was aware the application scope was shared, but thanks for the heads up. Shared stuff in application, individual stuff in session.

    BKBK
    Community Expert
    Community Expert
    January 11, 2011

    kenji776 wrote:

    Shared stuff in application, individual stuff in session.

    It doesn't need to be that strict. You could still get some more juice out of it.

    It is the object instance that you store in application scope. You could design the class or component in such a way that variables in session, request and variables scope go in as method arguments! The object would then behave differently each time, depending on the caller. That gives you more abstraction and greater scalability.

    JMF3Correct answer
    Participating Frequently
    January 11, 2011

    Yes, in most cases that is not only all right, it is preferable. So long

    as you're not swamping server memory with persistent objects, you are

    potentially reducing load by quite a bit. In fact, a number of the

    application frameworks, like Model-Glue, are designed to do exactly this:

    non-instanced object classes are loaded into application scope for use

    across all requests.