A double-edged sword for the frequent use of JSON

Source: Internet
Author: User
A double-edged sword for the frequent use of JSON

Recently, I got a chat room and used JSON as the intermediate storage medium to implement the chat room function on a single page. The advantage of JSON is that it is easy to parse and the JSON string can be placed in the memory, this reduces I/O operations and makes parsing JavaScript very convenient, reducing the process of language parsing. the JS framework is jquery.
Enter body

Code: [copy to clipboard] // obtain the JSON URL
VaR url_get_json = "Get. php ";
Function getchatlist ()
$. Getjson (''+ url_get_json + '? R = '+ math. Random (), function (JSON) {creatediv (JSON );});
If ($ ('#' + form_isgotoend). Val () = 1 & $ ('#' + form_isgotoend). ATTR ('checked') = true)
} Else {
Gotoend ();

This function is the core code

Code: [copy to clipboard] $ ("document"). Ready (
Function (){
// Create a selection
Makeselect (div_look_chat_select, form_look_chat_select, chat_type_array, 0 );
Makeselect (div_send_chat_select, form_send_chat_select, chat_type_array, 0 );
// Listen to the button event
$ ('#' + Form_send_button). Click (function () {checkform ();});
// Getchatlist ();
Setinterval ('getchatlist () ', 1000 );
$ ("#" + Div_chat_send). keyup (function (e) {keyup (e );});
// Setinterval ('getchatlist () ', 1000); frequently called

Get. PHP code

Code: [copy to clipboard] <? PHP
// Include_once ("JSON. php ");
Include_once ("classes/fileio. Class. php ");
// Set the cache file name
$ Cachefile = "cache/chat. Data ";
// Chat record Array
$ Chararr = array ();
If (fileio: isfileexists ($ cachefile ))
$ Chatstr = fileio: getfilecontent ($ cachefile );
$ Chararr = unserialize ($ chatstr );
// Echo fastjson: encode ($ chararr );
// $ JSON = new services_json ();
// Echo $ JSON-> encode ($ chararr );
Echo json_encode ($ chararr );

I have made a lot of comments on this file, and the functions of this file are not complex. Now, PHP itself supports JSON functions.

1. Convert arrays into JSON strings using non-PHP module Loading
IE memory usage, without significant changes to the memory usage of Apache processes, significantly increasing rapidly
2. Convert arrays into JSON strings using the PHP Module
IE memory usage. There is no significant change in the memory usage of Apache processes. The increase is slow, or the increase is acceptable.

Now let's explain why I have identified the bottleneck here.
I checked that there are two kinds of Voices: one is frequent Ajax requests, which causes js to use IE memory without increasing segments. The solution is to use JS memory collection mechanism.
I have observed that I have not significantly increased the memory usage of IE. That is to say, I didn't use ajax to generate a waste of requests, or I used jquery, this framework depends on it. But to be honest, I have read the code and I cannot solve the problem of excessive Ajax requests and release. Well, I'm doing the test.
As you can see, in the getchatlist () function, the JSON returned by my request is processed by creatediv (JSON). I wrote this function as function creatediv (JSON) {rerurn false}
If the Ajax request is too high, the amount of memory should come down. I tested it, but it didn't improve, and the memory usage continued to increase rapidly. Well, I commented it out. For JSON requests, the memory usage is gentle.
That's the problem here. Well, I modified it. In my get. php file, change it

Code: [copy to clipboard] <? PHP
$ JSON = new services_json ();
Echo $ JSON-> encode (Array ());

In this way, the memory usage is changed to usage, and the memory increases rapidly.
This indicates that the JS encoding is generated, resulting in a rapid increase in Apache memory usage, not due to excessive Ajax requests.
All of the above can be tested.

I cannot understand the cause. The JSON string is also generated. Even if the PHP module is loaded, the JSON format is generated, why can't I use the JSON format generated in PHP to keep the server memory stable? This should be explained by the PHP memory users.

The above solution is more extreme, because the get. php file is frequently called, so the memory usage looks more obvious.

Put the JSON string generation directly in post. php. It is a good solution. Get. php only provides file reading, which is better, but the above problem still exists.

Author: sanshi
Blog: http://blog.csdn.net/sanshi0815
Mail: sanshi0815@tom.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.