473,387 Members | 1,863 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,387 software developers and data experts.

mysql_real_escape_string memory leak?

Could someone please help me figure out why the memory usage
fluctuates when I use mysql_real_escape_string? I'm finding (what I
think are) memory leaks with a few mysql functions in php and I'm
trying to figure them all out. This one is pretty vexing. Thanks in
advance.

Here is example code:
class memTest {
function __construct() {
$con = mysql_connect("***************","*******","****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
mysql_query("SET NAMES 'utf8'");
for ($i = 1; $i<=10; $i++) {
$temp = 'mysql';
echo "mem: ", memory_get_usage(), "\n";
echo mysql_real_escape_string($temp), "\n";
echo "mem: ", memory_get_usage(), "\n\n";
}
mysql_close($con);
}
}
$a = new memTest();

Here is what is output:
mem: 43216
mysql
mem: 43208

mem: 43208
mysql
mem: 43216

mem: 43216
mysql
mem: 43224

mem: 43224
mysql
mem: 43232

mem: 43232
mysql
mem: 43240

mem: 43240
mysql
mem: 43248

mem: 43248
mysql
mem: 43256

mem: 43256
mysql
mem: 43264

mem: 43264
mysql
mem: 43272

mem: 43272
mysql
mem: 43280

Jul 4 '07 #1
5 2369
vi***@thoughtconvergence.com wrote:
Could someone please help me figure out why the memory usage
fluctuates when I use mysql_real_escape_string? I'm finding (what I
think are) memory leaks with a few mysql functions in php and I'm
trying to figure them all out. This one is pretty vexing. Thanks in
advance.
Sorry, not able to reproduce here:

mem: 99552
mysql
mem: 99552

mem: 99552
mysql
mem: 99552

mem: 99552
mysql
mem: 99552

mem: 99552
mysql
mem: 99552

(Keep in mind that I use a amd64 processor, so the 64-bit addresses waste a
lot of memory).
Question: does the memory usage go up and up, or does the garbage collector
come after some time (hint: go wild with the loop count)?? You may just be
creating temporal variables to hold the result of mysql_real_escape_string,
that aren't collected thereafter.
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

http://acm.asoc.fi.upm.es/~mr/
Proudly running Debian Linux with 2.6.20-1-amd64 kernel, KDE 3.5.7, and PHP
5.2.3-1 generating this signature.
Uptime: 02:43:00 up 18 days, 10:13, 5 users, load average: 1.72, 1.78,
1.95

Jul 4 '07 #2
vi***@thoughtconvergence.com wrote:
Could someone please help me figure out why the memory usage
fluctuates when I use mysql_real_escape_string? I'm finding (what I
think are) memory leaks with a few mysql functions in php and I'm
trying to figure them all out. This one is pretty vexing. Thanks in
advance.

Here is example code:
class memTest {
function __construct() {
$con = mysql_connect("***************","*******","****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
mysql_query("SET NAMES 'utf8'");
for ($i = 1; $i<=10; $i++) {
$temp = 'mysql';
echo "mem: ", memory_get_usage(), "\n";
echo mysql_real_escape_string($temp), "\n";
echo "mem: ", memory_get_usage(), "\n\n";
}
mysql_close($con);
}
}
$a = new memTest();

Here is what is output:
mem: 43216
mysql
mem: 43208

mem: 43208
mysql
mem: 43216

mem: 43216
mysql
mem: 43224

mem: 43224
mysql
mem: 43232

mem: 43232
mysql
mem: 43240

mem: 43240
mysql
mem: 43248

mem: 43248
mysql
mem: 43256

mem: 43256
mysql
mem: 43264

mem: 43264
mysql
mem: 43272

mem: 43272
mysql
mem: 43280
And you're worried about *164 bytes* of memory?

Do you actually have a problem? Or are you doing "premature optimization"?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 4 '07 #3
On Jul 3, 6:42 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
v...@thoughtconvergence.com wrote:
Could someone please help me figure out why the memory usage
fluctuates when I use mysql_real_escape_string? I'm finding (what I
think are) memory leaks with a few mysql functions in php and I'm
trying to figure them all out. This one is pretty vexing. Thanks in
advance.
Here is example code:
class memTest {
function __construct() {
$con = mysql_connect("***************","*******","****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
mysql_query("SET NAMES 'utf8'");
for ($i = 1; $i<=10; $i++) {
$temp = 'mysql';
echo "mem: ", memory_get_usage(), "\n";
echo mysql_real_escape_string($temp), "\n";
echo "mem: ", memory_get_usage(), "\n\n";
}
mysql_close($con);
}
}
$a = new memTest();
Here is what is output:
mem: 43216
mysql
mem: 43208
mem: 43208
mysql
mem: 43216
mem: 43216
mysql
mem: 43224
mem: 43224
mysql
mem: 43232
mem: 43232
mysql
mem: 43240
mem: 43240
mysql
mem: 43248
mem: 43248
mysql
mem: 43256
mem: 43256
mysql
mem: 43264
mem: 43264
mysql
mem: 43272
mem: 43272
mysql
mem: 43280

And you're worried about *164 bytes* of memory?

Do you actually have a problem? Or are you doing "premature optimization"?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
I am doing premature optimization. Eventually I'm going to be looping
~3.5 million times and inserting data into a table, so any memory leak
will be a problem.

Interestingly, when I increase the loop count to 1000, the memory
seems to stop increasing and stabilizes at iteration 256 (2^8). Prior
to that, the memory usage increases by 8 bytes each iteration. I'd
like to know why it increases in the first place, and how to prevent
it from increasing. Is there a way to force garbage collection in php?

Jul 5 '07 #4
vi***@thoughtconvergence.com wrote:
On Jul 3, 6:42 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>v...@thoughtconvergence.com wrote:
>>Could someone please help me figure out why the memory usage
fluctuates when I use mysql_real_escape_string? I'm finding (what I
think are) memory leaks with a few mysql functions in php and I'm
trying to figure them all out. This one is pretty vexing. Thanks in
advance.
Here is example code:
class memTest {
function __construct() {
$con = mysql_connect("***************","*******","****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
mysql_query("SET NAMES 'utf8'");
for ($i = 1; $i<=10; $i++) {
$temp = 'mysql';
echo "mem: ", memory_get_usage(), "\n";
echo mysql_real_escape_string($temp), "\n";
echo "mem: ", memory_get_usage(), "\n\n";
}
mysql_close($con);
}
}
$a = new memTest();
Here is what is output:
mem: 43216
mysql
mem: 43208
mem: 43208
mysql
mem: 43216
mem: 43216
mysql
mem: 43224
mem: 43224
mysql
mem: 43232
mem: 43232
mysql
mem: 43240
mem: 43240
mysql
mem: 43248
mem: 43248
mysql
mem: 43256
mem: 43256
mysql
mem: 43264
mem: 43264
mysql
mem: 43272
mem: 43272
mysql
mem: 43280
And you're worried about *164 bytes* of memory?

Do you actually have a problem? Or are you doing "premature optimization"?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

I am doing premature optimization. Eventually I'm going to be looping
~3.5 million times and inserting data into a table, so any memory leak
will be a problem.

Interestingly, when I increase the loop count to 1000, the memory
seems to stop increasing and stabilizes at iteration 256 (2^8). Prior
to that, the memory usage increases by 8 bytes each iteration. I'd
like to know why it increases in the first place, and how to prevent
it from increasing. Is there a way to force garbage collection in php?
Nope, no way to force garbage collection. It's not a separate thread
like in Java; the interpreter will clean things up when it gets around
to it.

But again, I wouldn't worry about it until you have a problem. With
3.5M iterations, you'll have other problems than memory leaks - for
instance, it's probably going to take a lot of time to do that many
inserts into a database.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 5 '07 #5
Jerry Stuckle wrote:
>Is there a way to force garbage collection in php?

Nope, no way to force garbage collection. It's not a separate thread
like in Java; the interpreter will clean things up when it gets around
to it.

But again, I wouldn't worry about it until you have a problem. With
3.5M iterations, you'll have other problems than memory leaks - for
instance, it's probably going to take a lot of time to do that many
inserts into a database.
And it would take even more time if you forced the garbage collector to run
more frequently than needed...

Instead of that, try experimenting with prepared statements, COPY
statements and different DB interfaces; those decisions can lead to very
different times when inserting that volume of information in the DB.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

http://acm.asoc.fi.upm.es/~mr/
Proudly running Debian Linux with 2.6.20-1-amd64 kernel, KDE 3.5.7, and PHP
5.2.3-1+b1 generating this signature.
Uptime: 21:37:58 up 20 days, 5:08, 5 users, load average: 3.24, 3.37,
1.52

Jul 5 '07 #6

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

Similar topics

8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
13
by: ndlarsen | last post by:
Hello. It's been a while since I used php. Since then magic quotes has been deprecated and will be removed when php 6.0 hits. My question is, what should I be using when submitting data to a...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.