Linux creates thread memory leaks, PHP json

Source: Internet
Author: User
Tags format array json php and php json thread domain domain name

This is still the encounter of a few problems to tidy up, I hope to meet the students can easily solve. Another reason why the recent blog feeds delayed updates is also explained together.

1.linux create thread causes memory leak
Today, after releasing a server in the extranet, the top found that Virt's usage has been rising, and it has risen 8m. So you can conclude that there is a memory leak, after the investigation, the final determination of the reasons for multithreading issues:
The code is as follows:

					1
2
3
4
5
6
					pthread_t thread_id;
int Ret=pthread_create (&thread_id, NULL, Flush_thread_work, (void*) &m_sql_client);
if (ret!=0) {
    appscore_error ("Thread creation failed:%d", ret);
    return ret;
}

Inside the Flush_thread_work function:

					1
2
3
4
5
					void* flush_thread_work (void* args)
{
    //....do something return
    NULL;
}

After a thread is started in the code, the main process continues, allowing the new thread to fend for itself (without invoking thread_join), and the main process pulls up such a thread every once in a while to do something about the landing of the data.

Such a writing would actually cause a memory leak.
The problem has been explained in the Linux man page:

When a joinable thread terminates, it memory resources (thread descriptor and stack) are not deallocated until th Read performs pthread_join on it. Therefore, Pthread_join must is called once for each joinable thread created to avoid memory.

It is said that if the thread does not join after execution, the thread's resources will not be released and lead to memory leaks!

There are two ways to fix this:
A. Manual release within a thread-executed function

					1
2
3
4
5
6
					void* flush_thread_work (void* args)
{//....do something Pthread_detach (
    ));
    return NULL;
}

B. Set the thread's Pthread_create_detached property when threads start

					1
2
3
4
5
6 7 8 9 (14)
					pthread_attr_t attr;
Pthread_attr_init (&attr);
Pthread_attr_setdetachstate (&attr, pthread_create_detached);
int ret=pthread_create (&thread_id, &attr, Flush_thread_work, (void*) &m_sql_client);
if (ret!=0) {
    //Remember attr also to be destructor, otherwise it is a memory
    leak Pthread_attr_destroy (&attr);
    Appscore_error ("Thread creation failed:%d", ret);
    return ret;
}
//Remember attr also want to destructor, otherwise it is a memory
leak Pthread_attr_destroy (&attr);

So the problem can be solved.
Resources:
Solved a hidden memory leak--pthread_create after no detach caused the memory to continue to grow

The problem of 2.php Json_encode function
In the company did an application, is the PHP and C + + network interaction, so chose JSON this more common serialization format, but encountered a relatively strange problem.
First look at the following code (PHP):

					1
2
3
4
5
					$OBJS = Array ();
$OBJS [1] = ' a ';
$OBJS [2] = ' B ';
$OBJS [4] = ' d ';
echo Json_encode ($OBJS). " \ n ";

The results of the output are as follows:

		{"0": "A", "1": "B", "3": "D"}

This is normal, with jsoncpp can also be resolved correctly, PHP automatically $objs as an associative array to generate JSON data.
However, when you replace the code with the following:

					1
2
3
4
5
6
					$OBJS = Array ();
$OBJS [0] = ' a ';
$OBJS [1] = ' B ';
$OBJS [2] = ' C ';
$OBJS [3] = ' d ';
echo Json_encode ($OBJS). " \ n ";

The output results are as follows:

		["A", "B", "C", "D"]

Jsoncpp in accordance with the previous analytical method is not out of the ~
In fact, this is also reasonable for PHP, the problem is that in PHP normal and associative arrays are array, but for C + +, there are vectors and map, so if you still want to json_encode the format of the associative array, you need to write this:

					1
2
3
4
5
6
					$OBJS = Array ();
$OBJS [0] = ' a ';
$OBJS [1] = ' B ';
$OBJS [2] = ' C ';
$OBJS [3] = ' d ';
Echo Json_encode ((object) $OBJS). \ n ";

The results are as follows:

		{"0": "A", "1": "B", "2": "C", "3": "D"}

3. This blog feed delay does not update the reason
Previously because of the Feedsky feed update is very dissatisfied, so refer to the practice of the Internet, set up a feed.vimer.cn, and Benbow domain name space will automatically create a subdirectory based on the domain name-feed, modify this directory. htaccess files are as follows:

					1
2
3
4
5
					<ifmodule mod_rewrite.c>
rewriteengine on
rewritebase/
rewriterule. http://feed.feedsky.com/ Vimer [L]
</IfModule>

When you access feed.vimer.cn, you will automatically jump to Http://feed.feedsky.com/vimer.
However, I overlooked a problem, that is the WordPress default feed link is http://vimer.cn/feed/rss2, and/feed/rss2 will be resolved into subdirectories, which automatically jump to http://feed.feedsky.com/ Vimer, so Feedsky will keep reading Http://feed.feedsky.com/vimer this link feed, of course, will not have any new data.

So in the end, this way is abandoned, it seems really wronged the Feedsky AH ~ ~ ~

OK, the recent summary is so much ~



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.