Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Does a large amount of extraBufferCapacity in the SharedFlow cause memory leak?

I am working on a chat application with MVVM architecture and I use SharedFlow to transfer incoming messages from Repository to ViewModel. When the user is offline and another user sends a large number of messages to him, as soon as the user is online, the messages are received one after the other in the Repository and need to be transferred to the ViewModel for display in the user interface. I first set the extraBufferCapacity value to 1 and saw that in this case only the first two messages are received in the ViewModel. Then I increased the extraBufferCapacity value to 6 and saw that the first 7 messages were transmitted to the ViewModel. Now if I increase the extraBufferCapacity value to the Int.MAX_VALUE, will it cause memory leak?

Repository:

object ChatRepo {
    private val _receiveMessageFlow = MutableSharedFlow<Message>(extraBufferCapacity = Int.MAX_VALUE)
    val receiveMessageFlow = _receiveMessageFlow.asSharedFlow()

    fun listen(socket: Socket) {
        socket.on(Config.ON_PV_MESSAGE, onPvMessage)
    }

    private val onPvMessage = Emitter.Listener { args ->
        val message = Gson().fromJson(args[0].toString(), Message::class.java)

        _receiveMessageFlow.tryEmit(message)
    }
}

ViewModel:

class ChatViewModel(var receiver: User) : ViewModel() {
    private val _messagesLive = MutableLiveData<Resource<MessageList>>()
    val messagesLive = _messagesLive as LiveData<Resource<MessageList>>

    init {
        observeReceivedMessage()
    }

    private fun observeReceivedMessage() {
        viewModelScope.launch {
            ChatRepo.receiveMessageFlow.collect { message ->
                if (message.senderUid == receiver.uid) {
                    addMessage(message)
                }
            }
        }
    }

    private fun addMessage(message: Message) {
        val resource = _messagesLive.value ?: Resource.success(MessageList())
        val messageList = resource.data ?: MessageList()
        messageList.addMessageLast(message)
        _messagesLive.value = Resource.add(messageList, 0)
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

What is a memory leak from Wikipedia:

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

According to your code you have ChatRepo, which is object (Singleton), the lifetime of ChatRepo is lifetime of the application. It has a field receiveMessageFlow, which can buffer some data, not consumed by any subscriber. If the only subscriber is in ChatViewModel and it no longer consumes the data, emitted by receiveMessageFlow, but receiveMessageFlow is still buffers some data, then in this case I would say there is a memory leak, because memory which is no longer needed is not released.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!