Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Memory usage degradations after enabling thread in TCL

Closed / Invalid5ee1a650b7b537514162f3d9ce01317298ed45f5Created 6 years agoUpdated 42 days ago
Descriptionplain2021-04-07 17:27:47
Memory degradation is happing in few test cases after configuring the TCL with "--enable_threads". So I wanted to know why memory is degrading with "--enable-threads". Please help.
arvind29 Ticket [5ee1a650b7]: 3 changes [History diff]2021-04-07 17:29:06
arvind29 Ticket [5ee1a650b7]: 3 changes [History diff]2021-04-08 04:29:40
sebreswiki2021-04-08 10:50:25

What are you calling memory degradation?

1. If you rather mean that freed memory blocks are not released to system back, then it is indeed correct with threaded-alloc, but it is intended behavior - they will be put into common thread pool, so are reusable for further threads.

The "solution" is here only to build tcl with some custom memory manager (or redefine Tcl_Alloc, Tcl_Free etc as sys-calls alloc, free etc).

2. If you mean either certain memory fragmentation (e. g. like described here http://paste.tclers.tk/5260), then it is also known issue and work is in progress (I'll try to present new threaded-alloc memory manager soon).

Possible "solution" for that at the moment is the same as above.

3. And last but not least, some of your handling could be probably thread unsafe, so simply leaking on memory if something used multi-threaded. For instance, you cannot share tcl-objects across threads (it is only allowed to supply single reference of object from one thread to another, but not a common shared usage in both, see Tcl_DuplicateObj), let alone the tcl-interpreter or some other main Tcls instances, which must be also definitely owned by the single thread only.

arvind29plain2021-04-16 15:04:23
Hi sebres,

I have removed "#define USE_THREAD_ALLOC 1" from configure file and I have not seen any degradation. Is it safe to remove USE_THREAD_ALLOC from configure file. I am compiling TCL with --enable-threads option.

Thanks much!
arvind29 Ticket [5ee1a650b7]: 3 changes [History diff]2021-04-26 10:04:42
serhiy.storchakawiki2026-10-14 14:19:29

Yes, it is safe: without USE_THREAD_ALLOC, Tcl 8.6 uses the system malloc() and free(), which are thread-safe, but can be slower when many threads allocate at the same time. As sebres wrote, the threaded allocator keeps freed memory for reuse instead of returning it to the system; this is by design. The fragmentation problem is tracked in [3d1c409f1a].

serhiy.storchaka Ticket [5ee1a650b7]: 3 changes [History diff]2026-10-22 17:19:25
Log in (anonymous is fine) to add a comment.