473,671 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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.c om> 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.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
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.tx t" 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10123
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 code... TCHAR myArray; DoStuff(myArray);
2
1715
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 problem is that I need the information that has been written to the file immediately. I other words, the library writes the info to a file (on disk) and my code reads this
7
7403
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 example, the manner in which I update element 1 depends on several other (randomly numbered) elements in the array. So, I can't change an element until I've figured out how every element changes.
19
3144
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 than in .Net environment, under VS 2005 Beta 2. Does anyone have any idea whether this will be addressed in the final release? Thanks, Tomasz
19
2358
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 chunk of memory that it can use for it's own purposes. But on the "library" side, I want to be able to do some book-keeping for each memory block that I hand out. I want some "hidden" meta-data (that I keep in struct Container) to be associated...
7
6428
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 now thown out of the array released properly by the CLI?
17
7239
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 to show the array data to the end user. Can I do that? How?
5
3788
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);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8911
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2808
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.