1

I have a plugin that deletes a list of posts based on their post id.

The code is very simple

    foreach ( $post_ids as $post_id ) {
            wp_trash_post( $post_id, true );
        }
    }

However I found that after deleting about 200 posts WordPress was running out of memory. So I added a debug statement just before the function call.

    foreach ( $post_ids as $post_id ) {
            error_log(number_format(memory_get_usage()));
            wp_trash_post( $post_id, true );
        }
    }

After printing the memory usage I found that after each call to the function, the memory usage increases by 1MB. Here is the error_log after enabling.

[13-May-2015 07:00:22 UTC] 4,696,680
[13-May-2015 07:00:22 UTC] 5,546,264
[13-May-2015 07:00:22 UTC] 6,357,632
[13-May-2015 07:00:22 UTC] 7,318,472
[13-May-2015 07:00:22 UTC] 8,176,520
[13-May-2015 07:00:23 UTC] 9,230,352
[13-May-2015 07:00:23 UTC] 10,189,496
[13-May-2015 07:00:23 UTC] 11,340,784
[13-May-2015 07:00:23 UTC] 12,293,720
[13-May-2015 07:00:23 UTC] 13,306,000
[13-May-2015 07:00:23 UTC] 14,399,040
[13-May-2015 07:00:23 UTC] 15,200,872
[13-May-2015 07:00:23 UTC] 16,102,872
[13-May-2015 07:00:23 UTC] 17,147,096
[13-May-2015 07:00:23 UTC] 18,045,096
[13-May-2015 07:00:23 UTC] 19,040,560
[13-May-2015 07:00:23 UTC] 19,846,544
[13-May-2015 07:00:23 UTC] 20,707,888
[13-May-2015 07:00:23 UTC] 21,892,520
[13-May-2015 07:00:23 UTC] 22,845,200
[13-May-2015 07:00:23 UTC] 23,846,232
[13-May-2015 07:00:23 UTC] 24,798,752
[13-May-2015 07:00:23 UTC] 25,656,736
[13-May-2015 07:00:23 UTC] 26,426,752
[13-May-2015 07:00:23 UTC] 27,329,552
[13-May-2015 07:00:23 UTC] 28,161,096
[13-May-2015 07:00:23 UTC] 29,125,568
[13-May-2015 07:00:23 UTC] 30,230,144
[13-May-2015 07:00:23 UTC] 31,223,688
[13-May-2015 07:00:23 UTC] 32,339,904
[13-May-2015 07:00:23 UTC] 33,350,272
[13-May-2015 07:00:23 UTC] 34,253,088
[13-May-2015 07:00:23 UTC] 35,057,400
[13-May-2015 07:00:23 UTC] 35,960,944
[13-May-2015 07:00:23 UTC] 36,876,744
[13-May-2015 07:00:23 UTC] 37,921,256
[13-May-2015 07:00:23 UTC] 38,723,616
[13-May-2015 07:00:24 UTC] 39,833,088
[13-May-2015 07:00:24 UTC] 40,857,000
[13-May-2015 07:00:24 UTC] 41,757,760
[13-May-2015 07:00:24 UTC] 42,705,680
[13-May-2015 07:00:24 UTC] 43,607,560
[13-May-2015 07:00:24 UTC] 44,608,680
[13-May-2015 07:00:24 UTC] 45,515,608
[13-May-2015 07:00:24 UTC] 46,533,264
[13-May-2015 07:00:24 UTC] 47,643,224
[13-May-2015 07:00:24 UTC] 48,634,448
[13-May-2015 07:00:24 UTC] 49,852,744
[13-May-2015 07:00:24 UTC] 50,784,032
[13-May-2015 07:00:24 UTC] 51,588,168
[13-May-2015 07:00:24 UTC] 52,495,528
[13-May-2015 07:00:24 UTC] 53,350,008
[13-May-2015 07:00:24 UTC] 54,239,256
[13-May-2015 07:00:24 UTC] 55,054,616
[13-May-2015 07:00:24 UTC] 55,863,480
[13-May-2015 07:00:24 UTC] 56,949,080
[13-May-2015 07:00:24 UTC] 57,869,168
[13-May-2015 07:00:24 UTC] 58,888,280
[13-May-2015 07:00:24 UTC] 59,880,080
[13-May-2015 07:00:24 UTC] 60,627,624
[13-May-2015 07:00:24 UTC] 61,835,984
[13-May-2015 07:00:24 UTC] 62,970,504
[13-May-2015 07:00:24 UTC] 64,010,176
[13-May-2015 07:00:24 UTC] 65,336,904
[13-May-2015 07:00:24 UTC] 66,453,480
[13-May-2015 07:00:24 UTC] 67,569,816
[13-May-2015 07:00:25 UTC] 68,470,776
[13-May-2015 07:00:25 UTC] 69,372,360
[13-May-2015 07:00:25 UTC] 70,278,408
[13-May-2015 07:00:25 UTC] 71,276,536
[13-May-2015 07:00:25 UTC] 72,183,376
[13-May-2015 07:00:25 UTC] 73,018,936
[13-May-2015 07:00:25 UTC] 74,041,176
[13-May-2015 07:00:25 UTC] 74,942,720
[13-May-2015 07:00:25 UTC] 75,943,024
[13-May-2015 07:00:25 UTC] 77,053,696
[13-May-2015 07:00:25 UTC] 77,980,824
[13-May-2015 07:00:25 UTC] 79,391,384
[13-May-2015 07:00:25 UTC] 80,384,816
[13-May-2015 07:00:25 UTC] 81,282,688
[13-May-2015 07:00:25 UTC] 82,489,744
[13-May-2015 07:00:25 UTC] 83,497,296
[13-May-2015 07:00:25 UTC] 84,398,976
[13-May-2015 07:00:25 UTC] 85,804,440
[13-May-2015 07:00:25 UTC] 86,708,912
[13-May-2015 07:00:25 UTC] 87,615,952
[13-May-2015 07:00:25 UTC] 88,746,936
[13-May-2015 07:00:25 UTC] 89,747,000
[13-May-2015 07:00:25 UTC] 90,549,968
[13-May-2015 07:00:25 UTC] 91,726,896
[13-May-2015 07:00:25 UTC] 92,626,080
[13-May-2015 07:00:25 UTC] 94,111,616
[13-May-2015 07:00:25 UTC] 95,205,136
[13-May-2015 07:00:25 UTC] 96,063,704
[13-May-2015 07:00:26 UTC] 97,359,584
[13-May-2015 07:00:26 UTC] 98,461,808
[13-May-2015 07:00:26 UTC] 99,266,808
[13-May-2015 07:00:26 UTC] 100,091,304
[13-May-2015 07:00:26 UTC] 101,100,856
[13-May-2015 07:00:26 UTC] 102,292,768
[13-May-2015 07:00:26 UTC] 103,282,904
[13-May-2015 07:00:26 UTC] 104,216,760
[13-May-2015 07:00:26 UTC] 105,047,576
[13-May-2015 07:00:26 UTC] 105,901,504
[13-May-2015 07:00:26 UTC] 106,920,392
[13-May-2015 07:00:26 UTC] 107,632,128
[13-May-2015 07:00:26 UTC] 109,018,464
[13-May-2015 07:00:26 UTC] 110,057,984
[13-May-2015 07:00:26 UTC] 111,061,800
[13-May-2015 07:00:26 UTC] 111,964,488
[13-May-2015 07:00:26 UTC] 112,885,176
[13-May-2015 07:00:26 UTC] 113,688,680
[13-May-2015 07:00:26 UTC] 114,610,664
[13-May-2015 07:00:26 UTC] 115,509,600
[13-May-2015 07:00:26 UTC] 116,713,664
[13-May-2015 07:00:26 UTC] 117,743,432
[13-May-2015 07:00:26 UTC] 118,645,336
[13-May-2015 07:00:26 UTC] 119,667,128
[13-May-2015 07:00:26 UTC] 120,477,528
[13-May-2015 07:00:26 UTC] 121,516,296
[13-May-2015 07:00:26 UTC] 122,631,928
[13-May-2015 07:00:27 UTC] 123,623,880
[13-May-2015 07:00:27 UTC] 124,643,248
[13-May-2015 07:00:27 UTC] 125,545,872
[13-May-2015 07:00:27 UTC] 126,541,536
[13-May-2015 07:00:27 UTC] 127,443,128
[13-May-2015 07:00:27 UTC] 128,345,432
[13-May-2015 07:00:27 UTC] 129,343,600
[13-May-2015 07:00:27 UTC] 130,247,832
[13-May-2015 07:00:27 UTC] 131,344,736
[13-May-2015 07:00:27 UTC] 132,399,960
[13-May-2015 07:00:27 UTC] 133,296,800
[13-May-2015 07:00:27 UTC] 134,203,000
[13-May-2015 07:00:27 UTC] 135,194,848
[13-May-2015 07:00:27 UTC] 136,326,864
[13-May-2015 07:00:27 UTC] 137,324,992
[13-May-2015 07:00:27 UTC] 138,249,680
[13-May-2015 07:00:27 UTC] 139,185,984
[13-May-2015 07:00:27 UTC] 140,089,488
[13-May-2015 07:00:27 UTC] 141,013,040
[13-May-2015 07:00:27 UTC] 142,028,400
[13-May-2015 07:00:27 UTC] 142,999,944
[13-May-2015 07:00:27 UTC] 143,926,840
[13-May-2015 07:00:27 UTC] 144,829,368
[13-May-2015 07:00:27 UTC] 145,734,256
[13-May-2015 07:00:27 UTC] 146,732,232
[13-May-2015 07:00:28 UTC] 147,892,568
[13-May-2015 07:00:28 UTC] 148,832,824
[13-May-2015 07:00:28 UTC] 149,841,304
[13-May-2015 07:00:28 UTC] 150,756,736
[13-May-2015 07:00:28 UTC] 151,695,136
[13-May-2015 07:00:28 UTC] 152,633,664
[13-May-2015 07:00:28 UTC] 153,672,880
[13-May-2015 07:00:28 UTC] 154,615,160
[13-May-2015 07:00:28 UTC] 155,660,448
[13-May-2015 07:00:28 UTC] 156,657,704
[13-May-2015 07:00:28 UTC] 157,701,944
[13-May-2015 07:00:28 UTC] 158,505,608
[13-May-2015 07:00:28 UTC] 159,432,120
[13-May-2015 07:00:28 UTC] 160,233,904
[13-May-2015 07:00:28 UTC] 161,532,384
[13-May-2015 07:00:28 UTC] 162,429,056
[13-May-2015 07:00:28 UTC] 163,327,088
[13-May-2015 07:00:28 UTC] 164,867,880
[13-May-2015 07:00:28 UTC] 165,670,944
[13-May-2015 07:00:28 UTC] 166,482,768
[13-May-2015 07:00:28 UTC] 167,285,864
[13-May-2015 07:00:28 UTC] 168,395,040
[13-May-2015 07:00:28 UTC] 169,231,424
[13-May-2015 07:00:28 UTC] 170,223,056
[13-May-2015 07:00:28 UTC] 171,224,584
[13-May-2015 07:00:29 UTC] 172,226,816
[13-May-2015 07:00:29 UTC] 173,317,296
[13-May-2015 07:00:29 UTC] 174,043,584
[13-May-2015 07:00:29 UTC] 174,946,848
[13-May-2015 07:00:29 UTC] 176,062,520
[13-May-2015 07:00:29 UTC] 177,061,448
[13-May-2015 07:00:29 UTC] 178,087,840
[13-May-2015 07:00:29 UTC] 179,013,256
[13-May-2015 07:00:29 UTC] 179,918,472
[13-May-2015 07:00:29 UTC] 180,934,784
[13-May-2015 07:00:29 UTC] 182,135,416
[13-May-2015 07:00:29 UTC] 182,938,944
[13-May-2015 07:00:29 UTC] 183,840,320
[13-May-2015 07:00:29 UTC] 184,757,368
[13-May-2015 07:00:29 UTC] 185,757,176
[13-May-2015 07:00:29 UTC] 186,662,832
[13-May-2015 07:00:29 UTC] 187,465,072
[13-May-2015 07:00:29 UTC] 188,379,904
[13-May-2015 07:00:29 UTC] 189,286,456
[13-May-2015 07:00:29 UTC] 190,191,440
[13-May-2015 07:00:29 UTC] 191,096,784
[13-May-2015 07:00:29 UTC] 191,930,512
[13-May-2015 07:00:30 UTC] 192,931,688
[13-May-2015 07:00:30 UTC] 193,835,808
[13-May-2015 07:00:30 UTC] 194,738,520
[13-May-2015 07:00:30 UTC] 195,732,544
[13-May-2015 07:00:30 UTC] 196,534,032
[13-May-2015 07:00:30 UTC] 197,491,112
[13-May-2015 07:00:30 UTC] 198,444,304
[13-May-2015 07:00:30 UTC] 199,346,600
[13-May-2015 07:00:30 UTC] 200,441,920
[13-May-2015 07:00:30 UTC] 201,309,864
[13-May-2015 07:00:30 UTC] 202,501,384
[13-May-2015 07:00:30 UTC] 203,611,752
[13-May-2015 07:00:30 UTC] 204,609,992
[13-May-2015 07:00:30 UTC] 205,507,128
[13-May-2015 07:00:30 UTC] 206,407,672
[13-May-2015 07:00:30 UTC] 207,310,272
[13-May-2015 07:00:30 UTC] 208,383,456
[13-May-2015 07:00:30 UTC] 209,281,520
[13-May-2015 07:00:30 UTC] 210,227,264
[13-May-2015 07:00:30 UTC] 211,233,192
[13-May-2015 07:00:30 UTC] 212,079,232
[13-May-2015 07:00:30 UTC] 213,124,256
[13-May-2015 07:00:31 UTC] 214,022,656
[13-May-2015 07:00:31 UTC] 215,044,896
[13-May-2015 07:00:31 UTC] 216,046,232
[13-May-2015 07:00:31 UTC] 217,137,488
[13-May-2015 07:00:31 UTC] 217,940,952
[13-May-2015 07:00:31 UTC] 218,940,712
[13-May-2015 07:00:31 UTC] 219,748,856
[13-May-2015 07:00:31 UTC] 220,706,072
[13-May-2015 07:00:31 UTC] 221,563,304
[13-May-2015 07:00:31 UTC] 222,564,040
[13-May-2015 07:00:31 UTC] 223,461,952
[13-May-2015 07:00:31 UTC] 224,555,328
[13-May-2015 07:00:31 UTC] 225,545,896
[13-May-2015 07:00:31 UTC] 226,543,472
[13-May-2015 07:00:31 UTC] 227,637,136
[13-May-2015 07:00:31 UTC] 228,468,552
[13-May-2015 07:00:31 UTC] 229,570,920
[13-May-2015 07:00:31 UTC] 230,399,912
[13-May-2015 07:00:31 UTC] 231,066,992
[13-May-2015 07:00:31 UTC] 232,066,920
[13-May-2015 07:00:31 UTC] 232,968,176
[13-May-2015 07:00:31 UTC] 234,187,296
[13-May-2015 07:00:31 UTC] 235,089,256
[13-May-2015 07:00:32 UTC] 235,888,320
[13-May-2015 07:00:32 UTC] 236,796,312
[13-May-2015 07:00:32 UTC] 237,655,048
[13-May-2015 07:00:32 UTC] 238,752,888
[13-May-2015 07:00:32 UTC] 239,705,472
[13-May-2015 07:00:32 UTC] 240,702,624
[13-May-2015 07:00:32 UTC] 241,703,936
[13-May-2015 07:00:32 UTC] 242,607,008
[13-May-2015 07:00:32 UTC] 243,588,640
[13-May-2015 07:00:32 UTC] 244,489,640
[13-May-2015 07:00:32 UTC] 245,592,280
[13-May-2015 07:00:32 UTC] 246,585,120
[13-May-2015 07:00:32 UTC] 247,443,128
[13-May-2015 07:00:32 UTC] 248,434,496
[13-May-2015 07:00:32 UTC] 249,390,544
[13-May-2015 07:00:32 UTC] 250,481,296
[13-May-2015 07:00:32 UTC] 251,671,680
[13-May-2015 07:00:32 UTC] 252,579,680
[13-May-2015 07:00:32 UTC] 253,572,496
[13-May-2015 07:00:32 UTC] 254,675,232
[13-May-2015 07:00:32 UTC] 255,671,560
[13-May-2015 07:00:32 UTC] 256,434,616
[13-May-2015 07:00:32 UTC] 257,237,632
[13-May-2015 07:00:32 UTC] 258,419,168
[13-May-2015 07:00:33 UTC] 259,317,504
[13-May-2015 07:00:33 UTC] 260,241,424
[13-May-2015 07:00:33 UTC] 261,244,648
[13-May-2015 07:00:33 UTC] 262,243,616
[13-May-2015 07:00:33 UTC] 263,046,136
[13-May-2015 07:00:33 UTC] 263,903,784
[13-May-2015 07:00:33 UTC] 265,011,368
[13-May-2015 07:00:33 UTC] 265,951,328
[13-May-2015 07:00:33 UTC] 267,148,208
[13-May-2015 07:00:33 UTC] 268,149,048
[13-May-2015 07:00:33 UTC] PHP Fatal error:  Allowed memory size of 268435456 bytes exhausted (tried to allocate 72 bytes) in /srv/www/wordpress-default/wp-includes/formatting.php on line 1765

A quick glance at wp_trash_post didn't reveal anything.

Does anyone know why wp_trash_post is leaking memory or if there is any way to fix it?

1

Well, it seems SAVEQUERIES is the culprit.

After disabling SAVEQUERIES memory usage is down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.