Keeping an Array in Memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew

    #1

    Keeping an Array in Memory

    I'm afraid I don't know PHP well enough
    to figure this out.

    What I would like is to keep an array in
    memory so that it doesn't have to be
    reloaded each time a .php script is run.

    Is this possible?

    In Java, I could load the array from a file
    in the init() method of a servlet and it would
    stay in memory until the server is shutdown
    or restarted, etc.

    Thanks,

    Andrew


  • Christopher Vogt

    #2
    Re: Keeping an Array in Memory

    Hi Andrew,

    what do you mean "reloaded"?

    Anyways... you can use sessions or simple cookies. read some tutorials
    (google) what sessions in php are and how they work if you don't know
    yet (as i would guess) and look into the php manual for the session
    functions.

    Good Luck ;) ,

    Christopher

    Andrew schrieb am 18.06.2004 02:36:
    [color=blue]
    > I'm afraid I don't know PHP well enough
    > to figure this out.
    >
    > What I would like is to keep an array in
    > memory so that it doesn't have to be
    > reloaded each time a .php script is run.
    >
    > Is this possible?
    >
    > In Java, I could load the array from a file
    > in the init() method of a servlet and it would
    > stay in memory until the server is shutdown
    > or restarted, etc.
    >
    > Thanks,
    >
    > Andrew
    >
    >[/color]

    Comment

    • Amir Khawaja

      #3
      Re: Keeping an Array in Memory

      Andrew wrote:[color=blue]
      > I'm afraid I don't know PHP well enough
      > to figure this out.
      >
      > What I would like is to keep an array in
      > memory so that it doesn't have to be
      > reloaded each time a .php script is run.
      >
      > Is this possible?
      >
      > In Java, I could load the array from a file
      > in the init() method of a servlet and it would
      > stay in memory until the server is shutdown
      > or restarted, etc.
      >
      > Thanks,
      >
      > Andrew
      >
      >[/color]

      Andrew,

      Unlike in a Java servlet environment, arrays in PHP are not kept in
      memory. All objects created by your script are destroyed at the end of
      execution. So, once your script is done loading the page, all data
      stored in memory is marked for garbage collection. You will have to
      store your data structures (I am assuming you want to save persistent
      data) in a database, flat file, or any other location that is to your
      liking.

      Amir.

      --
      Rules are written for those who lack the ability to truly reason, But for
      those who can, the rules become nothing more than guidelines, And live
      their lives governed not by rules but by reason.
      - James McGuigan

      Comment

      • Andrew

        #4
        Re: Keeping an Array in Memory

        [color=blue]
        > Andrew,
        >
        > Unlike in a Java servlet environment, arrays in PHP are not kept in
        > memory. All objects created by your script are destroyed at the end of
        > execution. So, once your script is done loading the page, all data
        > stored in memory is marked for garbage collection. You will have to
        > store your data structures (I am assuming you want to save persistent
        > data) in a database, flat file, or any other location that is to your
        > liking.
        >
        > Amir.
        >[/color]

        Yes, I'm using a flat file.

        I am loading this everytime the script it run.

        This does not seem good for performance since it's the
        exact same array that gets loaded.

        Is there no way around this?

        Andrew

        Comment

        • Amir Khawaja

          #5
          Re: Keeping an Array in Memory

          Andrew wrote:
          [color=blue]
          >
          > Yes, I'm using a flat file.
          >
          > I am loading this everytime the script it run.
          >
          > This does not seem good for performance since it's the
          > exact same array that gets loaded.
          >
          > Is there no way around this?
          >
          > Andrew
          >[/color]

          You are absolutely right. You will get a performance hit by doing so. A
          few ways to mitigate the hit the server takes is by serializing the
          data. Try the serialize() and unserialize() functions. I would suggest
          you perform some simple benchmarks to see how much of a hit you will
          have to contend with. For this purpose, apache ab may come in handy. If
          you have a decent server (a server with technology no more than a year
          old) and at least 1 GB of RAM, the performance hit should almost be
          negligible. Just keep in mind the the major bottleneck will be the I/O
          subsystem since the file holding the array structure will have to be
          loaded at every request.

          Amir.

          --
          Rules are written for those who lack the ability to truly reason, But for
          those who can, the rules become nothing more than guidelines, And live
          their lives governed not by rules but by reason.
          - James McGuigan

          Comment

          • Andrew

            #6
            Re: Keeping an Array in Memory


            [color=blue]
            >
            > You are absolutely right. You will get a performance hit by doing so. A
            > few ways to mitigate the hit the server takes is by serializing the
            > data. Try the serialize() and unserialize() functions. I would suggest
            > you perform some simple benchmarks to see how much of a hit you will
            > have to contend with. For this purpose, apache ab may come in handy. If
            > you have a decent server (a server with technology no more than a year
            > old) and at least 1 GB of RAM, the performance hit should almost be
            > negligible. Just keep in mind the the major bottleneck will be the I/O
            > subsystem since the file holding the array structure will have to be
            > loaded at every request.
            >
            > Amir.
            >[/color]

            Currently, I'm loading the array from a flat file with:

            $array = file('numbers.t xt');

            Does serialize() and unserialize()
            convert the array to binary data?

            I could possibly create the array in the .php
            file itself but it's got 1000 numbers and I expect this
            to grow, so it's easier to manage in a seperate file.

            Andrew




            Comment

            • Zurab Davitiani

              #7
              Re: Keeping an Array in Memory

              Andrew wrote:
              [color=blue]
              > I'm afraid I don't know PHP well enough
              > to figure this out.
              >
              > What I would like is to keep an array in
              > memory so that it doesn't have to be
              > reloaded each time a .php script is run.[/color]

              If you already use sessions, Christopher's suggestion to store an array in a
              session variable is a valid one:

              if(!$logged_in) // whatever your check
              $_SESSION["arr_stuff"] = get_array_from_ disk();

              This way, array is only loaded when a session starts and can be accessed
              from memory as long as the session is maintained. At the end, I'm not sure
              how much performance you will gain from this - I am assuming PHP will do
              some kind of caching on a file on its own anyway.

              I'm not sure if this is your situation, but one thing that's not present in
              PHP is application-level variables since there is no concept of
              application. It would be nice to be able to keep certain application-level
              data in memory without having to read it from disk and/or keep the
              duplicates with every session; especially since this has been possible in
              ASP and with Java servlets for awhile.

              Comment

              • Andrew

                #8
                Re: Keeping an Array in Memory

                [color=blue]
                > I'm not sure if this is your situation, but one thing that's not present in
                > PHP is application-level variables since there is no concept of
                > application. It would be nice to be able to keep certain application-level
                > data in memory without having to read it from disk and/or keep the
                > duplicates with every session; especially since this has been possible in
                > ASP and with Java servlets for awhile.[/color]

                Yes, that's it. I want application level variables. ;)

                I will look into storing the array in a session,
                but I don't think it's ideal for my situation.
                (not using a conventional web browser as the client)

                Also, I just read something that said the session
                variables are stored on the hard disk. So, I'm not
                sure if I gain any performance if it's just going
                read the data back from the hard disk anyway.

                Andrew

                Comment

                • Brad Kent

                  #9
                  Re: Keeping an Array in Memory

                  Zurab Davitiani <agt@mindless.c om> wrote in message[color=blue]
                  > If you already use sessions, Christopher's suggestion to store an array in a
                  > session variable is a valid one:
                  >
                  > if(!$logged_in) // whatever your check
                  > $_SESSION["arr_stuff"] = get_array_from_ disk();
                  >[/color]

                  That accomplishes nothing other than duplicating the array for every
                  user that accesses the page. The session data still hast to be loaded
                  each time the page is accessed. You _might_ see a performance
                  increase if you're storing session info in a DB, but the default is a
                  flat file. But I doubt it.. you're just creating more overhead.
                  now instead of having a single "numbers.tx t" you've got it for every
                  session.

                  What the original guy is wanting is some sort of persistant memory..
                  doesn't exist. Just hope that your File I/O is intelligent and keeps
                  the frequently accessed "numbers.tx t" cached

                  Comment

                  • Andrew

                    #10
                    Re: Keeping an Array in Memory

                    [color=blue]
                    > What the original guy is wanting is some sort of persistant memory..
                    > doesn't exist. Just hope that your File I/O is intelligent and keeps
                    > the frequently accessed "numbers.tx t" cached[/color]

                    Yes.

                    How can I tell if the numbers.txt is being cached?

                    Is this dependent on the OS or the PHP version?

                    Andrew


                    Comment

                    • Christian Fersch

                      #11
                      Re: Keeping an Array in Memory

                      Brad Kent wrote:[color=blue]
                      > You _might_ see a performance increase if you're storing session info
                      > in a DB, but the default is a flat file. But I doubt it.. you're
                      > just creating more overhead. now instead of having a single
                      > "numbers.tx t" you've got it for every session.
                      >
                      > What the original guy is wanting is some sort of persistant memory..
                      > doesn't exist. Just hope that your File I/O is intelligent and
                      > keeps the frequently accessed "numbers.tx t" cached[/color]

                      Simple task - use mysql with a HEAP table. This should be quite close to
                      keeping the array in the memory.

                      greetings, Christian

                      Comment

                      • Michael Austin

                        #12
                        Re: Keeping an Array in Memory

                        Andrew wrote:[color=blue]
                        >[color=green]
                        >> What the original guy is wanting is some sort of persistant memory..
                        >> doesn't exist. Just hope that your File I/O is intelligent and keeps
                        >> the frequently accessed "numbers.tx t" cached[/color]
                        >
                        >
                        > Yes.
                        >
                        > How can I tell if the numbers.txt is being cached?
                        >
                        > Is this dependent on the OS or the PHP version?
                        >
                        > Andrew
                        >
                        >[/color]

                        Actually, your web server may be able to help in keeping things in
                        memory. If this page is loaded often, it may be cached in memory
                        already. Which web server?

                        Michael.

                        Comment

                        • Andrew

                          #13
                          Re: Keeping an Array in Memory

                          [color=blue]
                          >
                          > Actually, your web server may be able to help in keeping things in
                          > memory. If this page is loaded often, it may be cached in memory
                          > already. Which web server?
                          >
                          > Michael.[/color]

                          Apache 1.3.29 on Linux

                          Andrew

                          Comment

                          • Andrea A

                            #14
                            Re: Keeping an Array in Memory

                            What about using sessions stored in memory?
                            Anyone use them?

                            A.


                            Comment

                            • Chung Leong

                              #15
                              Re: Keeping an Array in Memory

                              > Currently, I'm loading the array from a flat file with:[color=blue]
                              >
                              > $array = file('numbers.t xt');
                              >
                              > Does serialize() and unserialize()
                              > convert the array to binary data?
                              >
                              > I could possibly create the array in the .php
                              > file itself but it's got 1000 numbers and I expect this
                              > to grow, so it's easier to manage in a seperate file.
                              >[/color]

                              Amir is right about using serialize/and unserialize. It's faster than either
                              file() or keeping the array in a php file.

                              I wouldn't worry too much about overhead. Since all modern OSes cache disk
                              content one way or another, the array would be in memory already if it's
                              accessed often.




                              Comment

                              Working...