gc.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. -- $Id: testes/gc.lua $
  2. -- See Copyright Notice in file lua.h
  3. print('testing incremental garbage collection')
  4. local debug = require"debug"
  5. assert(collectgarbage("isrunning"))
  6. collectgarbage()
  7. local oldmode = collectgarbage("incremental")
  8. -- changing modes should return previous mode
  9. assert(collectgarbage("generational") == "incremental")
  10. assert(collectgarbage("generational") == "generational")
  11. assert(collectgarbage("incremental") == "generational")
  12. assert(collectgarbage("incremental") == "incremental")
  13. local function nop () end
  14. local function gcinfo ()
  15. return collectgarbage"count" * 1024
  16. end
  17. -- test weird parameters to 'collectgarbage'
  18. do
  19. collectgarbage("incremental")
  20. local opause = collectgarbage("param", "pause", 100)
  21. local ostepmul = collectgarbage("param", "stepmul", 100)
  22. assert(collectgarbage("param", "pause") == 100)
  23. assert(collectgarbage("param", "stepmul") == 100)
  24. local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe}
  25. for i = 1, #t do
  26. collectgarbage("param", "pause", t[i])
  27. for j = 1, #t do
  28. collectgarbage("param", "stepmul", t[j])
  29. collectgarbage("step", t[j])
  30. end
  31. end
  32. -- restore original parameters
  33. collectgarbage("param", "pause", opause)
  34. collectgarbage("param", "stepmul", ostepmul)
  35. collectgarbage()
  36. end
  37. --
  38. -- test the "size" of basic GC steps (whatever they mean...)
  39. --
  40. do print("steps")
  41. local function dosteps (siz)
  42. collectgarbage()
  43. local a = {}
  44. for i=1,100 do a[i] = {{}}; local b = {} end
  45. local x = gcinfo()
  46. local i = 0
  47. repeat -- do steps until it completes a collection cycle
  48. i = i+1
  49. until collectgarbage("step", siz)
  50. assert(gcinfo() < x)
  51. return i -- number of steps
  52. end
  53. if not _port then
  54. collectgarbage"stop"
  55. assert(dosteps(10) < dosteps(2))
  56. collectgarbage"restart"
  57. end
  58. end
  59. _G["while"] = 234
  60. --
  61. -- tests for GC activation when creating different kinds of objects
  62. --
  63. local function GC1 ()
  64. local u
  65. local b -- (above 'u' it in the stack)
  66. local finish = false
  67. u = setmetatable({}, {__gc = function () finish = true end})
  68. b = {34}
  69. repeat u = {} until finish
  70. assert(b[1] == 34) -- 'u' was collected, but 'b' was not
  71. finish = false; local i = 1
  72. u = setmetatable({}, {__gc = function () finish = true end})
  73. repeat i = i + 1; u = tostring(i) .. tostring(i) until finish
  74. assert(b[1] == 34) -- 'u' was collected, but 'b' was not
  75. finish = false
  76. u = setmetatable({}, {__gc = function () finish = true end})
  77. repeat local i; u = function () return i end until finish
  78. assert(b[1] == 34) -- 'u' was collected, but 'b' was not
  79. end
  80. local function GC2 ()
  81. local u
  82. local finish = false
  83. u = {setmetatable({}, {__gc = function () finish = true end})}
  84. local b = {34}
  85. repeat u = {{}} until finish
  86. assert(b[1] == 34) -- 'u' was collected, but 'b' was not
  87. finish = false; local i = 1
  88. u = {setmetatable({}, {__gc = function () finish = true end})}
  89. repeat i = i + 1; u = {tostring(i) .. tostring(i)} until finish
  90. assert(b[1] == 34) -- 'u' was collected, but 'b' was not
  91. finish = false
  92. u = {setmetatable({}, {__gc = function () finish = true end})}
  93. repeat local i; u = {function () return i end} until finish
  94. assert(b[1] == 34) -- 'u' was collected, but 'b' was not
  95. end
  96. local function GC() GC1(); GC2() end
  97. do
  98. print("creating many objects")
  99. local limit = 5000
  100. for i = 1, limit do
  101. local a = {}; a = nil
  102. end
  103. local a = "a"
  104. for i = 1, limit do
  105. a = i .. "b";
  106. a = string.gsub(a, '(%d%d*)', "%1 %1")
  107. a = "a"
  108. end
  109. a = {}
  110. function a:test ()
  111. for i = 1, limit do
  112. load(string.format("function temp(a) return 'a%d' end", i), "")()
  113. assert(temp() == string.format('a%d', i))
  114. end
  115. end
  116. a:test()
  117. _G.temp = nil
  118. end
  119. -- collection of functions without locals, globals, etc.
  120. do local f = function () end end
  121. print("functions with errors")
  122. local prog = [[
  123. do
  124. a = 10;
  125. function foo(x,y)
  126. a = sin(a+0.456-0.23e-12);
  127. return function (z) return sin(%x+z) end
  128. end
  129. local x = function (w) a=a+w; end
  130. end
  131. ]]
  132. do
  133. local step = 1
  134. if _soft then step = 13 end
  135. for i=1, string.len(prog), step do
  136. for j=i, string.len(prog), step do
  137. pcall(load(string.sub(prog, i, j), ""))
  138. end
  139. end
  140. end
  141. rawset(_G, "a", nil)
  142. _G.x = nil
  143. do
  144. foo = nil
  145. print('long strings')
  146. local x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
  147. assert(string.len(x)==80)
  148. local s = ''
  149. local k = math.min(300, (math.maxinteger // 80) // 2)
  150. for n = 1, k do s = s..x; local j=tostring(n) end
  151. assert(string.len(s) == k*80)
  152. s = string.sub(s, 1, 10000)
  153. local s, i = string.gsub(s, '(%d%d%d%d)', '')
  154. assert(i==10000 // 4)
  155. assert(_G["while"] == 234)
  156. _G["while"] = nil
  157. end
  158. if not _port then
  159. -- test the pace of the collector
  160. collectgarbage(); collectgarbage()
  161. local x = gcinfo()
  162. collectgarbage"stop"
  163. repeat
  164. local a = {}
  165. until gcinfo() > 3 * x
  166. collectgarbage"restart"
  167. assert(collectgarbage("isrunning"))
  168. repeat
  169. local a = {}
  170. until gcinfo() <= x * 2
  171. end
  172. print("clearing tables")
  173. local lim = 15
  174. local a = {}
  175. -- fill a with `collectable' indices
  176. for i=1,lim do a[{}] = i end
  177. b = {}
  178. for k,v in pairs(a) do b[k]=v end
  179. -- remove all indices and collect them
  180. for n in pairs(b) do
  181. a[n] = undef
  182. assert(type(n) == 'table' and next(n) == nil)
  183. collectgarbage()
  184. end
  185. b = nil
  186. collectgarbage()
  187. for n in pairs(a) do error'cannot be here' end
  188. for i=1,lim do a[i] = i end
  189. for i=1,lim do assert(a[i] == i) end
  190. print('weak tables')
  191. a = {}; setmetatable(a, {__mode = 'k'});
  192. -- fill a with some `collectable' indices
  193. for i=1,lim do a[{}] = i end
  194. -- and some non-collectable ones
  195. for i=1,lim do a[i] = i end
  196. for i=1,lim do local s=string.rep('@', i); a[s] = s..'#' end
  197. collectgarbage()
  198. local i = 0
  199. for k,v in pairs(a) do assert(k==v or k..'#'==v); i=i+1 end
  200. assert(i == 2*lim)
  201. a = {}; setmetatable(a, {__mode = 'v'});
  202. a[1] = string.rep('b', 21)
  203. collectgarbage()
  204. assert(a[1]) -- strings are *values*
  205. a[1] = undef
  206. -- fill a with some `collectable' values (in both parts of the table)
  207. for i=1,lim do a[i] = {} end
  208. for i=1,lim do a[i..'x'] = {} end
  209. -- and some non-collectable ones
  210. for i=1,lim do local t={}; a[t]=t end
  211. for i=1,lim do a[i+lim]=i..'x' end
  212. collectgarbage()
  213. local i = 0
  214. for k,v in pairs(a) do assert(k==v or k-lim..'x' == v); i=i+1 end
  215. assert(i == 2*lim)
  216. a = {}; setmetatable(a, {__mode = 'kv'});
  217. local x, y, z = {}, {}, {}
  218. -- keep only some items
  219. a[1], a[2], a[3] = x, y, z
  220. a[string.rep('$', 11)] = string.rep('$', 11)
  221. -- fill a with some `collectable' values
  222. for i=4,lim do a[i] = {} end
  223. for i=1,lim do a[{}] = i end
  224. for i=1,lim do local t={}; a[t]=t end
  225. collectgarbage()
  226. assert(next(a) ~= nil)
  227. local i = 0
  228. for k,v in pairs(a) do
  229. assert((k == 1 and v == x) or
  230. (k == 2 and v == y) or
  231. (k == 3 and v == z) or k==v);
  232. i = i+1
  233. end
  234. assert(i == 4)
  235. x,y,z=nil
  236. collectgarbage()
  237. assert(next(a) == string.rep('$', 11))
  238. do -- invalid mode
  239. local a = setmetatable({}, {__mode = 34})
  240. collectgarbage()
  241. end
  242. if T then -- bug since 5.3: all-weak tables are not being revisited
  243. T.gcstate("propagate")
  244. local t = setmetatable({}, {__mode = "kv"})
  245. T.gcstate("enteratomic") -- 't' was visited
  246. setmetatable(t, {__mode = "kv"})
  247. T.gcstate("pause") -- its new metatable is not being visited
  248. assert(getmetatable(t).__mode == "kv")
  249. end
  250. -- 'bug' in 5.1
  251. a = {}
  252. local t = {x = 10}
  253. local C = setmetatable({key = t}, {__mode = 'v'})
  254. local C1 = setmetatable({[t] = 1}, {__mode = 'k'})
  255. a.x = t -- this should not prevent 't' from being removed from
  256. -- weak table 'C' by the time 'a' is finalized
  257. setmetatable(a, {__gc = function (u)
  258. assert(C.key == nil)
  259. assert(type(next(C1)) == 'table')
  260. end})
  261. a, t = nil
  262. collectgarbage()
  263. collectgarbage()
  264. assert(next(C) == nil and next(C1) == nil)
  265. C, C1 = nil
  266. -- ephemerons
  267. local mt = {__mode = 'k'}
  268. a = {{10},{20},{30},{40}}; setmetatable(a, mt)
  269. x = nil
  270. for i = 1, 100 do local n = {}; a[n] = {k = {x}}; x = n end
  271. GC()
  272. local n = x
  273. local i = 0
  274. while n do n = a[n].k[1]; i = i + 1 end
  275. assert(i == 100)
  276. x = nil
  277. GC()
  278. for i = 1, 4 do assert(a[i][1] == i * 10); a[i] = undef end
  279. assert(next(a) == nil)
  280. local K = {}
  281. a[K] = {}
  282. for i=1,10 do a[K][i] = {}; a[a[K][i]] = setmetatable({}, mt) end
  283. x = nil
  284. local k = 1
  285. for j = 1,100 do
  286. local n = {}; local nk = k%10 + 1
  287. a[a[K][nk]][n] = {x, k = k}; x = n; k = nk
  288. end
  289. GC()
  290. local n = x
  291. local i = 0
  292. while n do local t = a[a[K][k]][n]; n = t[1]; k = t.k; i = i + 1 end
  293. assert(i == 100)
  294. K = nil
  295. GC()
  296. -- assert(next(a) == nil)
  297. -- testing errors during GC
  298. if T then
  299. collectgarbage("stop") -- stop collection
  300. local u = {}
  301. local s = {}; setmetatable(s, {__mode = 'k'})
  302. setmetatable(u, {__gc = function (o)
  303. local i = s[o]
  304. s[i] = true
  305. assert(not s[i - 1]) -- check proper finalization order
  306. if i == 8 then error("@expected@") end -- error during GC
  307. end})
  308. for i = 6, 10 do
  309. local n = setmetatable({}, getmetatable(u))
  310. s[n] = i
  311. end
  312. warn("@on"); warn("@store")
  313. collectgarbage()
  314. assert(string.find(_WARN, "error in __gc"))
  315. assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = false
  316. for i = 8, 10 do assert(s[i]) end
  317. for i = 1, 5 do
  318. local n = setmetatable({}, getmetatable(u))
  319. s[n] = i
  320. end
  321. collectgarbage()
  322. for i = 1, 10 do assert(s[i]) end
  323. getmetatable(u).__gc = nil
  324. warn("@normal")
  325. end
  326. print '+'
  327. -- testing userdata
  328. if T==nil then
  329. (Message or print)('\n >>> testC not active: skipping userdata GC tests <<<\n')
  330. else
  331. local function newproxy(u)
  332. return debug.setmetatable(T.newuserdata(0), debug.getmetatable(u))
  333. end
  334. collectgarbage("stop") -- stop collection
  335. local u = newproxy(nil)
  336. debug.setmetatable(u, {__gc = true})
  337. local s = 0
  338. local a = {[u] = 0}; setmetatable(a, {__mode = 'vk'})
  339. for i=1,10 do a[newproxy(u)] = i end
  340. for k in pairs(a) do assert(getmetatable(k) == getmetatable(u)) end
  341. local a1 = {}; for k,v in pairs(a) do a1[k] = v end
  342. for k,v in pairs(a1) do a[v] = k end
  343. for i =1,10 do assert(a[i]) end
  344. getmetatable(u).a = a1
  345. getmetatable(u).u = u
  346. do
  347. local u = u
  348. getmetatable(u).__gc = function (o)
  349. assert(a[o] == 10-s)
  350. assert(a[10-s] == undef) -- udata already removed from weak table
  351. assert(getmetatable(o) == getmetatable(u))
  352. assert(getmetatable(o).a[o] == 10-s)
  353. s=s+1
  354. end
  355. end
  356. a1, u = nil
  357. assert(next(a) ~= nil)
  358. collectgarbage()
  359. assert(s==11)
  360. collectgarbage()
  361. assert(next(a) == nil) -- finalized keys are removed in two cycles
  362. end
  363. -- __gc x weak tables
  364. local u = setmetatable({}, {__gc = true})
  365. -- __gc metamethod should be collected before running
  366. setmetatable(getmetatable(u), {__mode = "v"})
  367. getmetatable(u).__gc = function (o) os.exit(1) end -- cannot happen
  368. u = nil
  369. collectgarbage()
  370. local u = setmetatable({}, {__gc = true})
  371. local m = getmetatable(u)
  372. m.x = {[{0}] = 1; [0] = {1}}; setmetatable(m.x, {__mode = "kv"});
  373. m.__gc = function (o)
  374. assert(next(getmetatable(o).x) == nil)
  375. m = 10
  376. end
  377. u, m = nil
  378. collectgarbage()
  379. assert(m==10)
  380. do -- tests for string keys in weak tables
  381. collectgarbage(); collectgarbage()
  382. local m = collectgarbage("count") -- current memory
  383. local a = setmetatable({}, {__mode = "kv"})
  384. a[string.rep("a", 2^22)] = 25 -- long string key -> number value
  385. a[string.rep("b", 2^22)] = {} -- long string key -> collectable value
  386. a[{}] = 14 -- collectable key
  387. collectgarbage()
  388. local k, v = next(a) -- string key with number value preserved
  389. assert(k == string.rep("a", 2^22) and v == 25)
  390. assert(next(a, k) == nil) -- everything else cleared
  391. assert(a[string.rep("b", 2^22)] == undef)
  392. a[k] = undef -- erase this last entry
  393. k = nil
  394. collectgarbage()
  395. assert(next(a) == nil)
  396. -- make sure will not try to compare with dead key
  397. assert(a[string.rep("b", 100)] == undef)
  398. assert(collectgarbage("count") <= m + 1) -- everything collected
  399. end
  400. -- errors during collection
  401. if T then
  402. warn("@store")
  403. u = setmetatable({}, {__gc = function () error "@expected error" end})
  404. u = nil
  405. collectgarbage()
  406. assert(string.find(_WARN, "@expected error")); _WARN = false
  407. warn("@normal")
  408. end
  409. if not _soft then
  410. print("long list")
  411. local a = {}
  412. for i = 1,200000 do
  413. a = {next = a}
  414. end
  415. a = nil
  416. collectgarbage()
  417. end
  418. -- create many threads with self-references and open upvalues
  419. print("self-referenced threads")
  420. local thread_id = 0
  421. local threads = {}
  422. local function fn (thread)
  423. local x = {}
  424. threads[thread_id] = function()
  425. thread = x
  426. end
  427. coroutine.yield()
  428. end
  429. while thread_id < 1000 do
  430. local thread = coroutine.create(fn)
  431. coroutine.resume(thread, thread)
  432. thread_id = thread_id + 1
  433. end
  434. -- Create a closure (function inside 'f') with an upvalue ('param') that
  435. -- points (through a table) to the closure itself and to the thread
  436. -- ('co' and the initial value of 'param') where closure is running.
  437. -- Then, assert that table (and therefore everything else) will be
  438. -- collected.
  439. do
  440. local collected = false -- to detect collection
  441. collectgarbage(); collectgarbage("stop")
  442. do
  443. local function f (param)
  444. ;(function ()
  445. assert(type(f) == 'function' and type(param) == 'thread')
  446. param = {param, f}
  447. setmetatable(param, {__gc = function () collected = true end})
  448. coroutine.yield(100)
  449. end)()
  450. end
  451. local co = coroutine.create(f)
  452. assert(coroutine.resume(co, co))
  453. end
  454. -- Now, thread and closure are not reachable any more.
  455. collectgarbage()
  456. assert(collected)
  457. collectgarbage("restart")
  458. end
  459. do
  460. collectgarbage()
  461. collectgarbage"stop"
  462. collectgarbage("step") -- steps should not unblock the collector
  463. local x = gcinfo()
  464. repeat
  465. for i=1,1000 do _ENV.a = {} end -- no collection during the loop
  466. until gcinfo() > 2 * x
  467. collectgarbage"restart"
  468. _ENV.a = nil
  469. end
  470. if T then -- tests for weird cases collecting upvalues
  471. local function foo ()
  472. local a = {x = 20}
  473. coroutine.yield(function () return a.x end) -- will run collector
  474. assert(a.x == 20) -- 'a' is 'ok'
  475. a = {x = 30} -- create a new object
  476. assert(T.gccolor(a) == "white") -- of course it is new...
  477. coroutine.yield(100) -- 'a' is still local to this thread
  478. end
  479. local t = setmetatable({}, {__mode = "kv"})
  480. collectgarbage(); collectgarbage('stop')
  481. -- create coroutine in a weak table, so it will never be marked
  482. t.co = coroutine.wrap(foo)
  483. local f = t.co() -- create function to access local 'a'
  484. T.gcstate("enteratomic") -- ensure all objects are traversed
  485. assert(T.gcstate() == "enteratomic")
  486. assert(t.co() == 100) -- resume coroutine, creating new table for 'a'
  487. assert(T.gccolor(t.co) == "white") -- thread was not traversed
  488. T.gcstate("pause") -- collect thread, but should mark 'a' before that
  489. assert(t.co == nil and f() == 30) -- ensure correct access to 'a'
  490. collectgarbage("restart")
  491. -- test barrier in sweep phase (backing userdata to gray)
  492. local u = T.newuserdata(0, 1) -- create a userdata
  493. collectgarbage()
  494. collectgarbage"stop"
  495. local a = {} -- avoid 'u' as first element in 'allgc'
  496. T.gcstate"enteratomic"
  497. T.gcstate"sweepallgc"
  498. local x = {}
  499. assert(T.gccolor(u) == "black") -- userdata is "old" (black)
  500. assert(T.gccolor(x) == "white") -- table is "new" (white)
  501. debug.setuservalue(u, x) -- trigger barrier
  502. assert(T.gccolor(u) == "gray") -- userdata changed back to gray
  503. collectgarbage"restart"
  504. print"+"
  505. end
  506. if T then
  507. local debug = require "debug"
  508. collectgarbage("stop")
  509. local x = T.newuserdata(0)
  510. local y = T.newuserdata(0)
  511. debug.setmetatable(y, {__gc = nop}) -- bless the new udata before...
  512. debug.setmetatable(x, {__gc = nop}) -- ...the old one
  513. assert(T.gccolor(y) == "white")
  514. T.checkmemory()
  515. collectgarbage("restart")
  516. end
  517. if T then
  518. collectgarbage("stop")
  519. T.gcstate("pause")
  520. local sup = {x = 0}
  521. local a = setmetatable({}, {__newindex = sup})
  522. T.gcstate("enteratomic")
  523. assert(T.gccolor(sup) == "black")
  524. a.x = {} -- should not break the invariant
  525. assert(not (T.gccolor(sup) == "black" and T.gccolor(sup.x) == "white"))
  526. T.gcstate("pause") -- complete the GC cycle
  527. sup.x.y = 10
  528. collectgarbage("restart")
  529. end
  530. if T then
  531. print("emergency collections")
  532. collectgarbage()
  533. collectgarbage()
  534. T.totalmem(T.totalmem() + 200)
  535. for i=1,200 do local a = {} end
  536. T.totalmem(0)
  537. collectgarbage()
  538. local t = T.totalmem("table")
  539. local a = {{}, {}, {}} -- create 4 new tables
  540. assert(T.totalmem("table") == t + 4)
  541. t = T.totalmem("function")
  542. a = function () end -- create 1 new closure
  543. assert(T.totalmem("function") == t + 1)
  544. t = T.totalmem("thread")
  545. a = coroutine.create(function () end) -- create 1 new coroutine
  546. assert(T.totalmem("thread") == t + 1)
  547. end
  548. -- create an object to be collected when state is closed
  549. do
  550. local setmetatable,assert,type,print,getmetatable =
  551. setmetatable,assert,type,print,getmetatable
  552. local tt = {}
  553. tt.__gc = function (o)
  554. assert(getmetatable(o) == tt)
  555. -- create new objects during GC
  556. local a = 'xuxu'..(10+3)..'joao', {}
  557. ___Glob = o -- resurrect object!
  558. setmetatable({}, tt) -- creates a new one with same metatable
  559. print(">>> closing state " .. "<<<\n")
  560. end
  561. local u = setmetatable({}, tt)
  562. ___Glob = {u} -- avoid object being collected before program end
  563. end
  564. -- create several objects to raise errors when collected while closing state
  565. if T then
  566. local error, assert, find, warn = error, assert, string.find, warn
  567. local n = 0
  568. local lastmsg
  569. local mt = {__gc = function (o)
  570. n = n + 1
  571. assert(n == o[1])
  572. if n == 1 then
  573. _WARN = false
  574. elseif n == 2 then
  575. assert(find(_WARN, "@expected warning"))
  576. lastmsg = _WARN -- get message from previous error (first 'o')
  577. else
  578. assert(lastmsg == _WARN) -- subsequent error messages are equal
  579. end
  580. warn("@store"); _WARN = false
  581. error"@expected warning"
  582. end}
  583. for i = 10, 1, -1 do
  584. -- create object and preserve it until the end
  585. table.insert(___Glob, setmetatable({i}, mt))
  586. end
  587. end
  588. -- just to make sure
  589. assert(collectgarbage'isrunning')
  590. do -- check that the collector is not reentrant in incremental mode
  591. local res = true
  592. setmetatable({}, {__gc = function ()
  593. res = collectgarbage()
  594. end})
  595. collectgarbage()
  596. assert(not res)
  597. end
  598. collectgarbage(oldmode)
  599. print('OK')