试图减少脚本的内存使用量

时间:2013-11-14 23:20:15

标签: php memory

我是一名数学老师,他为我的学校建立了一个在线测试网站(也就是说,不是专业人士)。该网站运作良好但随着我学校的使用量增加,我开始遇到内存问题(我认为)。在我有大约50到60个用户同时使用该网站后,整个网站开始崩溃,它将在几分钟后恢复。低使用率我从来没有遇到过这个问题。学生参加测验的页面在页面上加载10个问题,每个选项有4个无线电选项。 (不是很多jquery继续)。每次用户点击答案时,我都会使用ajax将答案存储在数据库中。下面是jquery代码,它们在进行测验时发送他们的点击。

$('input:radio').click(function(){
var questionId = $(this).parent().parent().find('.qid').val();
var answer = $(this).val();
$.ajax({
        type: "POST",
        url: "insertqanswerajax.php",
        data: {questionId: questionId, answer: answer},
    });

});

当我在我的cpanel中加载系统进程时,我看到有5个不同的进程在运行,每个进程大约80兆字节。我的php.ini中的最大值设置为540MB。如果我使用memory_get_peak_usage()检查页面,它从不会读取大约半兆字节,但是在控制台时间线中我可以看到一个用户的内存使用量几乎达到10兆字节(下图)。我需要检查什么,或者解决差异的最佳方法是什么?可能导致问题的原因是什么?如果需要,我可以提供更多信息,我只是不确定所有相关内容。

提前感谢您的帮助。

这是通过ajax

访问的php文件的代码
<?php session_start();
include('../includes/startup.php');
$questionId = $_POST['questionId'];
$answer = $_POST['answer'];



insertQuizAnswer($questionId, $userId, $answer, 1);


?> 

该文件中调用的函数:

function insertQuizAnswer($questionId, $userId, $answer, $testId){
global $DB;                                          
$standardsHandle = $DB->prepare("INSERT INTO quizanswers (questionid, userid,answer,testid)    
VALUES (:questionId,:userId, :answer, :testId)
                                ");   
$standardsHandle->bindParam(':questionId', $questionId);
$standardsHandle->bindParam(':userId', $userId);
$standardsHandle->bindParam(':answer', $answer);
$standardsHandle->bindParam(':testId', $testId);
$standardsHandle->execute();    
}

启动文件加载到:

<?php
if(preg_match('/(?i)msie [2-7]/',$_SERVER['HTTP_USER_AGENT']))
{
// if IE < 8
echo "My Class Progress does not Work with this version of Internet Explorer</br>
<a href='https://www.google.com/intl/en/chrome/browser/'>Click Here to Download a more modern browser</a>";
exit;
}
else
{

}
if(isset($_POST['getGrade'])){
$_SESSION['gradeLevel'] = $_POST['getGrade'];
}
if(isset($_POST['getSubject'])){
$_SESSION['subject'] = $_POST['getSubject'];
}
include_once('../functions/userfunctions.php');   //all functions
include_once('../functions/goalfunctions.php');   //all functions
include_once('../functions/modulefunctions.php');   //all functions
include_once('../functions/globalfunctions.php');   //all functions
include_once('../functions/skillfunctions.php');   //all functions
include_once('../functions/quizfunctions.php');   //all functions
include_once('../functions/practicefunctions.php');   //all functions
include_once('../functions/benchmarkfunctions.php');   //all functions
include_once('../functions/dockfunctions.php');   //all functions
include_once('../functions/dashboardfunctions.php');   //all functions
include_once('../functions/notificationfunctions.php');   //all functions
include_once('../includes/connect.php');     //connect to database
$userSubject = $_SESSION['subject'];
$userGradeLevel = $_SESSION['gradeLevel'];
$userId = $_SESSION['userId'];
if ($_SESSION['loggedIn'] == 'true'){
}   
    else{
    header('location: ../../index.php');
    die();
    }

&GT;

以下是访问的connect.php文件:

try {                                                                                 
$DB = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);   
}

catch(PDOException $e) {      
 echo $e->getMessage();
}

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

使用的内存量取决于ini_set('memory_limit');这个数量是由Apache保留的,在内存耗尽之前,脚本实际使用了多少并不重要。