Skip to content
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

Reduce memory consumption #2725

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Reduce memory consumption #2725

wants to merge 1 commit into from

Conversation

r2k1
Copy link
Contributor

@r2k1 r2k1 commented May 1, 2024

Opencost stores Kubernetes objects (Pod, ReplicaSet, Deployment, DaemonSet, etc.) with all their fields in memory.

For every request to the /metrics endpoint, a copy of these objects is created, which can lead to significant memory usage, particularly for large clusters.

I suspect this is to prevent accidental modifications, but how likely is that? I think there should be no reason to modify the k8s objects.

I ran some tests on a single-node cluster, creating 300 deployments. Max memory usage dropped from 125MB to 105MB after avoiding the extra copies. Taking generic overhead into account it may produce greater benefits for the large clusters.

Related issue #2637

this create a copy of every kubernetes object for every /metrics call

Signed-off-by: r2k1 <yokree@gmail.com>
Copy link

vercel bot commented May 1, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
opencost ✅ Ready (Inspect) Visit Preview May 1, 2024 10:56pm

Copy link

sonarcloud bot commented May 1, 2024

@r2k1
Copy link
Contributor Author

r2k1 commented May 1, 2024

Additional Notes:

kube-state-metrics employs certain strategies to optimize memory usage:

  • Instead of Informers, it uses Reflector instead of Informer, allowing a direct transformation from a Kubernetes object to a Prometheus string format, retaining the object in memory only momentarily during unmarshalling.
  • It has a custom method to serve metrics since the Prometheus Gatherer can be inefficient.
    More details can be found here.

@AjayTripathy
Copy link
Contributor

Thanks @r2k1 . We'll get this reviewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants