Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda