Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda