473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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
Jul 17 '05 #1
16 7411
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
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
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
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


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
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
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
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
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
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
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

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
What about using sessions stored in memory?
Anyone use them?

A.
Jul 17 '05 #14
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: G.D. | last post by:
Hi, I'm using a third-party library which writes some useful information into a file. In order to do so, I just have to pass a file name to a library function and the file gets written. The...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
19
by: pinkfloydhomer | last post by:
Please read the example below. I'm sorry I couldn't make it smaller, but it is pretty simple. When client code is calling newThingy(), it is similar to malloc: the client gets a pointer to a...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.