use getLoop()->defer to send data cause memory occupy increase greatly and no reduction after send #1713
Replies: 4 comments 12 replies
-
If you have issues with glibc default allocator you can look at libtcmalloc |
Beta Was this translation helpful? Give feedback.
-
I think you need some kind of flow control.
You need to somehow limit the number of queued calls, and then block the main thread as long as there are too many in the queue. This will stop new memory allocations until prior How to implement the limit efficiently, I can't just say from the top of my head, I'm also quite new to C++ concurrency. From old learned theory, it should be a counting semaphore. If you find a solution, please post it here, I might actually need it too 😎 _Mark |
Beta Was this translation helpful? Give feedback.
-
Yes, memory is freed on the heap, but that usually just means the blocks are added to a list of free blocks that can be reused. Adjacent blocks may be fused together. In a practical case of small and medium size blocks (which I guess we have here), memory cannot be truly "freed" in the eyes the operating system. A process' memory consumption is basically stuck at its maximum consumption. How do you measure memory consumption? Only for very large blocks (usually >= 64kB) there is an optimization using virtual memory mapping. Linux specific background on that: see |
Beta Was this translation helpful? Give feedback.
-
Yes glibc allocator works like that but libtcmalloc and similar uses mmap for everything so you get back to zero once everything is freed |
Beta Was this translation helpful? Give feedback.
-
[machine translation]
I created the uWS::App in a thread and assigned WebSocket to a global WebSocket pointer when the server was connected, then send a lot of json data via this pointer, and send function was added to the event loop using defer function. The memory occupy increases rapidly when the data is sent, and does not decrease at all after the data is sent. Why is that? Is there a solution?
I need to use multithreading so that the server can not only respond to client requests, but also change the data sent based on the console, so threading is necessary. In multithreading, using send function directly without adding it to the event loop cause to race condition, and now using the event loop leads to a large increase in memory usage, what should I do?)
Here is the my code
我在一个线程中创建了uWS::App,并且在服务器被连接时将WebSocket赋值给一个全局的WebSocket指针,而后通过这个指针去大量发送json数据,并且send使用defer被加入事件循环。在数据发送时,内存占用快速增加,并且在数据发送完成后也没有丝毫减少占用。这是为什么?有什么解决办法吗?
(我需要使用多线程使得服务器不只是响应客户端的请求,还可以根据控制台改变发送的数据,所以线程是必要的。而在多线程中,直接使用send而不将其加入事件循环则会导致竞态,而现在使用事件循环又导致内存占用大幅度增加,我到底该怎么办?)
Beta Was this translation helpful? Give feedback.
All reactions