Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Live: Source cleanup to free memory for multiple streams. #413

Closed
szhuangzan opened this issue May 29, 2015 · 9 comments
Closed

Live: Source cleanup to free memory for multiple streams. #413

szhuangzan opened this issue May 29, 2015 · 9 comments
Assignees
Labels
Bug It might be a bug. Enhancement Improvement or enhancement. TransByAI Translated by AI/GPT.
Milestone

Comments

@szhuangzan
Copy link

szhuangzan commented May 29, 2015

SrsSource* source = SrsSource::fetch(req);
if (!source) {
    if ((ret = SrsSource::create(req, server, server, &source)) != ERROR_SUCCESS) {
        return ret;
    }
}

If a server is continuously requested with rtmp://xxx/live/(1-100000), the number of objects keeps increasing, and no release logic is found.

@winlinvip
Copy link
Member

winlinvip commented May 29, 2015

That's right, this overhead can be calculated.

Note: There is a workaround, Gracefully Quit, which can be considered as a more mature solution.

@winlinvip winlinvip added this to the srs 1.0 release milestone May 29, 2015
@willkk
Copy link

willkk commented Apr 25, 2016

Same question, if SRS keeps running, the number of sources will keep increasing, when will they be released?

@winlinvip
Copy link
Member

winlinvip commented Apr 28, 2016

9900 streams only occupy 65MB of memory.

Note: There is a workaround, Gracefully Quit, which can be considered as a more mature solution.

@winlinvip winlinvip changed the title SrsSource这类对象好像一直都不会释放? SrsSource清理,SrsSource这类对象好像一直都不会释放? Jan 11, 2021
@winlinvip winlinvip reopened this Jan 11, 2021
@winlinvip
Copy link
Member

winlinvip commented Jan 11, 2021

The situation of Source cleanup is as follows:

Therefore, I still need to restart this Issue to support a more comprehensive Source cleanup capability.

@winlinvip winlinvip added Bug It might be a bug. and removed question labels Jan 11, 2021
@winlinvip winlinvip changed the title SrsSource清理,SrsSource这类对象好像一直都不会释放? SrsSource清理,解决流较多场景时,内存不断增长的问题 Mar 4, 2021
@winlinvip winlinvip self-assigned this Aug 24, 2021
@winlinvip winlinvip added the Enhancement Improvement or enhancement. label Sep 13, 2021
@winlinvip
Copy link
Member

winlinvip commented Sep 13, 2021

Source cleanup will be resolved in SRS 5.0, mainly in monitoring and RTC scenarios, where there will be a large number of streams (Sources).

Since the Source pointer is widely referenced, after a long period of consideration, the best solution is to refer to C++11's smart pointers to avoid crashes after cleanup.

Note: There is a workaround, Gracefully Quit, which can be considered as a more mature solution.

@winlinvip winlinvip changed the title SrsSource清理,解决流较多场景时,内存不断增长的问题 SrsSource清理:解决流较多场景时,内存不断增长的问题 Sep 13, 2021
@winlinvip
Copy link
Member

winlinvip commented Aug 26, 2022

The problem of Source cleanup is essentially due to the direct reference to Source, which makes it impossible to count and accurately clean up. A better approach is to make Source an internal type and expose a wrapped type externally. Each time it is used, a new wrapped type is created, similar to a smart pointer.

void* fetch_source(url) // The fetched void* avoids being converted to Source.

Add a wrapped type:

WrapSource WrapSource::wrap(void*) // Provides Source functions.

Also avoid creating pointers, always use instances, so you can accurately know how many references there are.

Then, combined with a simple GC for delayed release, this can most simply solve the Source cleanup problem.

It's a bit like a smart pointer, but much simpler, relying on specifications and type hiding to implement. It will be a bit more complicated, but overall it's quite simple.

This can also be used to solve other objects that need to be cleaned up but are widely referenced.

@winlinvip winlinvip changed the title SrsSource清理:解决流较多场景时,内存不断增长的问题 Live: Source cleanup to free memory for multiple streams. SrsSource清理:解决流较多场景时,内存不断增长的问题 Jan 2, 2023
@winlinvip
Copy link
Member

winlinvip commented Jan 2, 2023

Pushing a stream every 5 seconds, the memory will increase to 4.2GB in about three days. In scenarios with a large number of push-pull streams, such as stress testing, monitoring, and calling, this problem is quite serious.

Some optimizations are planned for 5.0, without deleting Source, to do some cleanup and slow down the memory growth. The plan is to delete Source in 6.0 and completely solve this problem.

@winlinvip
Copy link
Member

winlinvip commented Feb 28, 2023

Synchronize the progress: It is confirmed that WebRTC's own implementation of ShraredPtr and WeakPtr smart pointers will be referenced, but a simpler version will be implemented and used only in Source objects.

It will be done in two steps to avoid affecting stability: it will be implemented and partially cleaned up in 5.0, and it is expected to be completely resolved in 6.0.

@jinleileiking
Copy link

jinleileiking commented Jul 11, 2023

Another approach is to refactor with Rust, but the implementation might be challenging.

@winlinvip winlinvip changed the title Live: Source cleanup to free memory for multiple streams. SrsSource清理:解决流较多场景时,内存不断增长的问题 Live: Source cleanup to free memory for multiple streams. Jul 18, 2023
@ossrs ossrs locked and limited conversation to collaborators Jul 18, 2023
@winlinvip winlinvip converted this issue into discussion #3667 Jul 18, 2023
@winlinvip winlinvip added the TransByAI Translated by AI/GPT. label Jul 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Bug It might be a bug. Enhancement Improvement or enhancement. TransByAI Translated by AI/GPT.
Projects
Status: No status
Development

No branches or pull requests

5 participants