New in MySQL 5.7: Performance Schema scalable memory allocation

Performance Schema is a mechanism to collect and report run time statistics for running MySQL server. These statistics are stored-in and fetched-from internal memory buffers. In MySQL 5.6 GA, memory for these buffers is allocated during MySQL server startup with either user specified configuration values or with default values that autosize. Once the server has started, the size of the buffers is fixed and performance_schema does not do any additional memory allocation or freeing during execution.

Using fixed memory allocation has a few limitations. To outline two specific situations that could occur whether a user specifies configuration values for performance_schema or uses the default values:

  • [Concern 1] significant amount of allocated buffer is left unused if less instances of instrument are encountered
  • [Concern 2] amount allocated is not sufficient and performance_schema starts loosing further statistics if more instances of instrument are encountered.

This then asks the question – is there a way, Performance Schema itself decides what should be the optimal size of different buffers and change it according to the server load? Is it at all possible?

Well, yes !!! In MySQL 5.7 GA Performance Schema has been provided with this intelligence.

Changes in MySQL 5.7

In MySQL 5.7 memory allocation for Performance Schema buffers doesn’t happen at server start-up but is instead based on the actual runtime requirement.

For the server variables (which control buffer size), you can now specify:

Value Description
0 to tell P_S not to collect stats thus no allocation for this buffer.
N to tell P_S to collect stats for maximum N instance only. Memory allocation happens as and when need arises. And this allocation continues until space for max (N here) instances is allocated.
-1 to tell P_S take your own decision for maximum limit. As above, memory is allocated as and when need arises. This allocation continues until space for max (decided by P_S here) instances is allocated.

Lets try to understand this behavior with an example:

[Concern 1] Significant amount of allocated buffer is left unused if less instances of instrument are encountered

Start MySQL server with intention to collect stats for maximum 2000 prepared statements:

Start a client and lets see the memory allocated for the buffer to store prepared statements statistics:

So till now NO memory is allocated for this buffer as there is no prepared statement on server yet.

Now lets prepare a statement:

We can see the size of buffer allocated:

As you can see, the numbers have changed, and memory was allocated as it was required.

Question: There is only 1 prepared statement on server and memory required for 1 prepared statement stat is 1664. Then why did P_S allocate 1703936?
Answer: P_S does not allocate memory for each any every instance but it allocates memory in chunk, (1703936 for 1024 prepared statements). Then this allocated chunk is used for further instances, until it could not accommodate any more. Then a new chunk (of 1703936 here) is allocated again provided it doesn’t exceed the maximum (N) specified by user (or decided by P_S in case -1 is given).

If we compare memory allocation of P_S buffer, before & after this feature, here is what it looks like :


[Concern 2] Amount allocated is not sufficient and it start loosing further statistics if more instances of instrument are encountered.

To resolve this, where one can’t anticipate the huge volume of incoming instruments, now (s)he can set value of variable to ‘-1‘. In this case, it would keep on allocating memory with the runtime requirements.
So for example, if one (wrongly) anticipates 2000 prepared statements to occur but at runtime 3000 prepared statements seen, then

BEFORE without auto-scaling:

As it can be seen, statistics for 1000 instances are lost.

Now with auto-scaling:

Statistics for all instances are collected and nothing lost.

One more important thing to mention here is that memory allocated by P_S for buffers at runtime, is NEVER deallocated.

For server variables which controls P_S buffers’ size and which could be auto-scaled (i.e. could be set with value ‘-1’), default value is already set to “-1”. Here is the list of these server variables:

Auto-sized vs. Auto-scaled performance schema variables.

Above (auto-scaled) variable should not be confused with auto-sized variables. Even for auto-sized variables, values could be set as ‘-1’ during server startup. But there is a difference between them.

  • auto-sized:
    Based on estimated server load/configuration, value of this variable would be determined by performance schema during startup.
  • auto-scaled:
    Based on actual runtime requirement, memory is allocated for buffers controlled by these variables.

6 thoughts on “New in MySQL 5.7: Performance Schema scalable memory allocation

Leave a Reply to Justin Swanhart Cancel reply
