By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,348 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,348 IT Pros & Developers. It's quick & easy.

Keeping an Array in Memory

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
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:
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

Jul 17 '05 #2

P: n/a
Andrew wrote:
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


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
Jul 17 '05 #3

P: n/a
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.


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

Jul 17 '05 #4

P: n/a
Andrew wrote:

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


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
Jul 17 '05 #5

P: n/a


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.


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

$array = file('numbers.txt');

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


Jul 17 '05 #6

P: n/a
Andrew wrote:
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.


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.
Jul 17 '05 #7

P: n/a
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.


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

Jul 17 '05 #8

P: n/a
Zurab Davitiani <ag*@mindless.com> wrote in message
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();


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.txt" 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.txt" cached
Jul 17 '05 #9

P: n/a
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.txt" cached


Yes.

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

Is this dependent on the OS or the PHP version?

Andrew
Jul 17 '05 #10

P: n/a
Brad Kent wrote:
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.txt" 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.txt" cached


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

greetings, Christian
Jul 17 '05 #11

P: n/a
Andrew wrote:
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.txt" cached

Yes.

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

Is this dependent on the OS or the PHP version?

Andrew


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.
Jul 17 '05 #12

P: n/a

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.


Apache 1.3.29 on Linux

Andrew

Jul 17 '05 #13

P: n/a
What about using sessions stored in memory?
Anyone use them?

A.
Jul 17 '05 #14

P: n/a
> Currently, I'm loading the array from a flat file with:

$array = file('numbers.txt');

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.


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.


Jul 17 '05 #15

P: n/a
Andrea A wrote:
What about using sessions stored in memory?
Anyone use them?

A.


Sessions stored in memory are a good alternative. If you have some
control over your server and the traffic to this webapp (website) is
going to be high, you may want to investigate caching mechanisms like
APC or any of the Zend caching products. I have used the Zend products
and can safely say that it works. However, you may not want to invest in
commercial tools as your need may not require the investment.

Regards,
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
Jul 17 '05 #16

P: n/a
Brad Kent wrote:
Zurab Davitiani <ag*@mindless.com> wrote in message
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();

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.


That is not completely accurate. It may still increase the performance if he
*already uses sessions* since session data is already getting read and
parsed from a file anyway for each request.
now instead of having a single "numbers.txt" you've got it for every
session.
You would have to open and parse numbers.txt for every request too. Storing
the data in sessions will increase the disk space for sessions (depending
on number of concurrent sessions and length of data), but will cut down on
opening and closing one more file for each request. In either case,
performance increase, if any, would be marginal. And I'd like to know
myself which method is more efficient.
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.txt" cached

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.


How hard would it be to create something like a phpd that would keep
configuration, application and application-level data in memory to be
accessed by each PHP process?
Jul 17 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.