René's URL Explorer Experiment


Title: Memory Leak Caused by the circular reference. · Issue #559 · python-graphblas/python-graphblas · GitHub

Open Graph Title: Memory Leak Caused by the circular reference. · Issue #559 · python-graphblas/python-graphblas

X Title: Memory Leak Caused by the circular reference. · Issue #559 · python-graphblas/python-graphblas

Description: First of all, thank you so much for developing such a powerful and interesting project. We are a group working on libraries such as LIBSVM, LIBLINEAR, LibMultiLabel, and so on. Recently, we have integrating python-graphblas into our code...

Open Graph Description: First of all, thank you so much for developing such a powerful and interesting project. We are a group working on libraries such as LIBSVM, LIBLINEAR, LibMultiLabel, and so on. Recently, we have in...

X Description: First of all, thank you so much for developing such a powerful and interesting project. We are a group working on libraries such as LIBSVM, LIBLINEAR, LibMultiLabel, and so on. Recently, we have in...

Opengraph URL: https://github.com/python-graphblas/python-graphblas/issues/559

X: @github

direct link

Domain: patch-diff.githubusercontent.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Memory Leak Caused by the circular reference.","articleBody":"First of all, thank you so much for developing such a powerful and interesting project.\nWe are a group working on libraries such as **LIBSVM**, **LIBLINEAR**, **LibMultiLabel**, and so on. \nRecently, we have integrating **python-graphblas** into our codebase to speed up sparse matrix operation.\nHowever, during the integrations, we encountered a memory issue when using sparse matrix multiplication. \nWe would like to share our observation and findings, and hope to get some feedback on potential improvements.\n\nThe information about our sparse matrices and predefined variables is shown below:\n```\nweights shape: (135909, 826017)  # CSR and NNZ: 1,818,756,815\ninstances shape: (153025, 135909)  # CSR and NNZ: 14,013,838\nbatch_size = 256\n```\nOur usage is simplified in the following examples: \n```python\ndef predict_values(A, B):\n    C = gb.Matrix(float, A.shape[0], B.shape[1])\n    C \u003c\u003c A.mxm(B, op=gb.semiring.min_plus)\n    return C.to_dense(fill_value=0)\n\ndef main():\n    for idx in range(0, instances.shape[0], batch_size):\n        batch_x = instances[idx : idx + batch_size, :]\n        results = predict_values(batch_x, weights)\n```\nWe initially assumed that once `predict_values` returns and the local variable `C` goes out of the `predict_values` function scope, its memory would be released (since the reference to `C` no longer exists). \nHowever, based on our observations, the memory occupied by `C` is not released. \nInstead, the memory usage grows linearly with each function call, until some conditions are met (i.e., the threshold of Python's garbage collector) and Python eventually release it.\nThis issue becomes more severe when `C` consumes a large amount of memory.\nIf `C` consumes a large amount of memory, this delayed release causes the system to consume all available memory (including swap), which in turn degrades the performance of matrix multiplication.\nUpon investigation, we found that the reason of the memory issue comes from the circular reference in GraphBLAS.\n\n```python\nclass Matrix(BaseType):\n    # SKIP\n    def __new__(cls, dtype=FP64, nrows=0, ncols=0, *, name=None):\n            # SKIP\n            if backend == \"suitesparse\":\n                self.ss = ss(self)\n``` \ncopied from [graphblas/core/matrix.py](https://github.com/python-graphblas/python-graphblas/blob/main/graphblas/core/matrix.py#L167-L203)\n```python\nclass ss:\n    __slots__ = \"_parent\", \"config\"\n\n    def __init__(self, parent):\n        self._parent = parent\n        self.config = MatrixConfig(parent)\n``` \ncopied from [graphblas/core/ss/matrix.py](https://github.com/python-graphblas/python-graphblas/blob/main/graphblas/core/ss/matrix.py#L183-L188). \nThe simplified reference structure is as follows:\n```pysql\nMatrix M\n  └── .ss (attribute of M)\n         └── (references back to) M\n```\nThis creates a circular reference between the `Matrix` object and its `.ss` attribute.\nSince Python uses reference counting to manage memory, the reference count of objects involved in a circular reference never becomes zero, even when local variables go out of scope (e.g., our simplified example).\nAs a result, these objects remain in memory until Python's garbage collector detects and frees them.\nHowever, the garbage collector does not detect them immediately. \nIn fact, it sets a threshold and uses it to decide when to detect and free those circular reference objects.\nTherefore, if the threshold is not reached, the unreleased memory may gradually consume all available memory, which is consistent with our observations. \n\nThere are several ways to address this memory issue:\n* Call `gc.collect()` frequently to force the garbage collector to clean up circular references. \n  However, this can introduce significant overhead and degrade performance.\n* Manually break the reference in the `.ss` attribute (e.g., `C.ss = None` in our example) after the matrix is no longer needed. However, this is not a general or scalable solution, as it requires explicit intervention.\n* Replace the strong reference with `weakref`, which avoids reference cycles entirely.\nWould replacing the strong reference with `weakref` be a safe and effective solution in this case?\n\nWe were curious about the use of circular references in `python-graphblas`, so we investigated it further.\nCurrently, we know that some functions in the `Matrix` class depend on the `.ss` attribute, and that the `ss` object also needs to access information from the `Matrix` itself (e.g., `nrows` and `ncols`). \nHowever, there might be alternative ways to support this interaction without the circular reference?!\nThat said, we may be missing some context. Is there a specific reason or design requirement for using this circular reference structure?\n\nIs it possible to refactor this and improve this memory behavior in `python-graphblas`?\n\nThanks","author":{"url":"https://github.com/zhi-bao","@type":"Person","name":"zhi-bao"},"datePublished":"2025-07-10T12:08:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/559/python-graphblas/issues/559"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:24fb5745-e8e0-45b9-fff2-21e9157beedd
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9306:9C235:2638065:3351E5D:698DACEF
html-safe-nonce2bf11393210406fd88d654b6c5b63260c58d2e9e098a9e0f94f55745d1067189
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MzA2OjlDMjM1OjI2MzgwNjU6MzM1MUU1RDo2OThEQUNFRiIsInZpc2l0b3JfaWQiOiIxNzYxOTIzNjI4MTU1MTIwODc5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacb0facb92a3ef01521c9a0239d4baaf87b1950413acd5826cbf9a8d4c9295073f
hovercard-subject-tagissue:3219095377
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-graphblas/python-graphblas/559/issue_layout
twitter:imagehttps://opengraph.githubassets.com/109103695d7bfbf9847d739ee582beb3e705149e483f68ef39823bbc604bac08/python-graphblas/python-graphblas/issues/559
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/109103695d7bfbf9847d739ee582beb3e705149e483f68ef39823bbc604bac08/python-graphblas/python-graphblas/issues/559
og:image:altFirst of all, thank you so much for developing such a powerful and interesting project. We are a group working on libraries such as LIBSVM, LIBLINEAR, LibMultiLabel, and so on. Recently, we have in...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamezhi-bao
hostnamegithub.com
expected-hostnamegithub.com
None8c7947c0c592efeab6162b9909ad11fa43bff8b0cb5ff43273dc25e41979d43e
turbo-cache-controlno-preview
go-importgithub.com/python-graphblas/python-graphblas git https://github.com/python-graphblas/python-graphblas.git
octolytics-dimension-user_id103965858
octolytics-dimension-user_loginpython-graphblas
octolytics-dimension-repository_id221014819
octolytics-dimension-repository_nwopython-graphblas/python-graphblas
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id221014819
octolytics-dimension-repository_network_root_nwopython-graphblas/python-graphblas
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseb22a9fbf4dea601ec149a9e5362e0558df79b505
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/issues/559#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-graphblas%2Fpython-graphblas%2Fissues%2F559
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-graphblas%2Fpython-graphblas%2Fissues%2F559
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python-graphblas%2Fpython-graphblas
Reloadhttps://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/issues/559
Reloadhttps://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/issues/559
Reloadhttps://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/issues/559
python-graphblas https://patch-diff.githubusercontent.com/python-graphblas
python-graphblashttps://patch-diff.githubusercontent.com/python-graphblas/python-graphblas
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2Fpython-graphblas%2Fpython-graphblas
Fork 16 https://patch-diff.githubusercontent.com/login?return_to=%2Fpython-graphblas%2Fpython-graphblas
Star 152 https://patch-diff.githubusercontent.com/login?return_to=%2Fpython-graphblas%2Fpython-graphblas
Code https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas
Issues 55 https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/issues
Pull requests 14 https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/pulls
Discussions https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/discussions
Actions https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/actions
Projects 0 https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/projects
Wiki https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/wiki
Security 0 https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/security
Insights https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/pulse
Code https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas
Issues https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/issues
Pull requests https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/pulls
Discussions https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/discussions
Actions https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/actions
Projects https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/projects
Wiki https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/wiki
Security https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/security
Insights https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/pulse
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/python-graphblas/python-graphblas/issues/559
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/python-graphblas/python-graphblas/issues/559
Memory Leak Caused by the circular reference.https://patch-diff.githubusercontent.com/python-graphblas/python-graphblas/issues/559#top
https://github.com/zhi-bao
https://github.com/zhi-bao
zhi-baohttps://github.com/zhi-bao
on Jul 10, 2025https://github.com/python-graphblas/python-graphblas/issues/559#issue-3219095377
graphblas/core/matrix.pyhttps://github.com/python-graphblas/python-graphblas/blob/main/graphblas/core/matrix.py#L167-L203
graphblas/core/ss/matrix.pyhttps://github.com/python-graphblas/python-graphblas/blob/main/graphblas/core/ss/matrix.py#L183-L188
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.