-
Notifications
You must be signed in to change notification settings - Fork 562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
High memory consumption INSERTing #1384
Comments
@vkazmirchuk thanks for reporting this. Do I understand correctly memory consumption grows over time?
Do you have any runtime statistics on GC attempting to free memory? Is memory going to the OS/container limit? |
Yes, it happens after a lot of baches insertions
We tried to run GC manually after each insertion, but it doesn't release memory completely and for a long time the client accumulates memory which is not cleared. |
@vkazmirchuk Could you also check for number of goroutines? |
@vkazmirchuk, just to clarify - because GC in Go and memory management in general is tricky. Let me ask again:
do you encounter out-of-memory? I want to double-check if it's GC not releasing or we have problem with a stuff not releasing a memory. See: https://pkg.go.dev/runtime/debug#FreeOSMemory (https://stackoverflow.com/questions/37382600/cannot-free-memory-once-occupied-by-bytes-buffer/37383604#37383604) If you are not hitting out-of-memory, you could play with |
We have one gorutine that performs insertions of 1 bach one after another. The total number of gorutines in the appendix is 23. But I don't think that plays any role.
Yeah, we end up catching the OOM.
This is what helped us not to catch OOM, we limit memory to 40 gigabytes and it saves us, but our application after a while eats all 40 gigabytes and stays at that level. It would be ideal to reuse memory within the client to clickhouse. We have implemented |
When you set GOMEMLIMIT to 40 GiB, it stays at this level, and it does not go over the limit, right? It sounds like GC is not freeing memory to the OS. This is something that can happen. What if you lower GOMEMLIMIT? This can influence CPU cycles as GC will be executed more often. Besides that, of course, we should invest a bit into figuring out how we can save us from unnecessary memory allocations. Current driver architecture assumes everything is buffered per each block. Thus, high memory consumption can happen. For now what I can recommend is to lower batch size. |
Yes over 40 GiB is not out, but for us even that value is great. Because 1 batch takes about 600 MiB maximum.
That's right, that's what happens. But the point is that we are trying to achieve good performance with low CPU and RAM consumption. We overuse memory almost everywhere in our application so as not to strain GC. And the only weak point in the app is the clickhouse client
We have a fairly large clichhouse cluster and the batch size was chosen so that our cluster could handle the load. By reducing the batch size we will need to invest a lot of money in horizontal scaling of the cluster. In general we can do this, but it is a last resort, first we want to solve the problem with the clickhouse client we believe that it can work more efficiently with memory |
Thanks for your comment @vkazmirchuk . We will need to prioritize it on our side. If you don't need a |
Yes but as you can see in the description of this issue the problem is in this package ch-go =) |
@vkazmirchuk are you interested in taking a look if solution from #1181 helps in your case? |
For ch-go there is a ongoing draft PR that aims to reduce memory overhead: ClickHouse/ch-go#413 |
Observed
We insert 1 million records at a time using the function
batch.AppendStruct(item)
After some number of iterations we had high memory consumption by the clickhouse client.
pprof memory report: pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.028.pb.gz
Our golang structure that we put into the database:
Expected behaviour
The client should reuse memory whenever possible, rather than allocating new memory at each iteration of batch insertion
Code example
Details
Environment
clickhouse-go
version:v2.25.0
database/sql
compatible driver:ClickHouse API
1.22.1
Linux
23.8.8.20
No
CREATE TABLE
statements for tables involved:The text was updated successfully, but these errors were encountered: