Home > Backend Development > Golang > Sync Map may cause an increase in ram and goroutine

Sync Map may cause an increase in ram and goroutine

PHPz
Release: 2024-02-09 18:15:08
forward
856 people have browsed it

Sync Map 可能会导致 ram 和 goroutine 的增加

php editor Youzi reminds you that using Sync Map may lead to an increase in RAM and goroutine. Sync Map is a library in Go language used to implement concurrency-safe mapping data structures. Although it provides efficient performance when handling concurrent reads and writes, using Sync Map may lead to an increase in memory usage and the number of goroutines when processing large-scale data. Therefore, when using Sync Map, you need to weigh the balance between usage and performance based on specific business scenarios and needs to ensure the stability and efficiency of the system.

Question content

Hello, this is the code where I call util a collector

import (
    "context"
    "errors"
    "sync"
    "time"
)

type Collector struct {
    keyValMap *sync.Map
}

func (c *Collector) LoadOrWait(key any) (retValue any, availability int, err error) {
    value, status := c.getStatusAndValue(key)

    switch status {
    case 0:
        return nil, 0, nil
    case 1:
        return value, 1, nil
    case 2:
        ctxWithTimeout, _ := context.WithTimeout(context.Background(), 5 * time.Second)
        for {
            select {
            case <-ctxWithTimeout.Done():
                return nil, 0, errRequestTimeout
            default:
                value, resourceStatus := c.getStatusAndValue(key)
                if resourceStatus == 1 {
                    return value, 1, nil
                }
                time.Sleep(50 * time.Millisecond)
            }
        }
    }

    return nil, 0, errRequestTimeout
}

// Store ...
func (c *Collector) Store(key any, value any) {
    c.keyValMap.Store(key, value)
}

func (c *Collector) getStatusAndValue(key any) (retValue any, availability int) {
    var empty any
    result, loaded := c.keyValMap.LoadOrStore(key, empty)

    if loaded && result != empty {
        return result, 1
    }

    if loaded && result == empty {
        return empty, 2
    }

    return nil, 0
}
Copy after login

So the purpose of this utility is to act as a cache where similar values ​​are loaded only once but read multiple times. However, when the collector's object is passed to multiple goroutines, I face increased gorotine and ram usage whenever multiple goroutines try to use the collector cache. Can someone explain if this usage of synchronized maps is correct. If yes, then what may be the reason for the high number of goroutines/high memory usage

Solution

Of course, since the cancellation function of the newly created ctxwithtimeout context is not called , you may face memory leaks. To resolve this issue, change the line to:

ctxWithTimeout, cancelFunc := context.WithTimeout(context.Background(), requestTimeout)
defer cancelFunc()
Copy after login

So you can always clear all allocated resources once the context expires. This should resolve the leak.
Regarding the usage of sync.map it seems fine to me.
If this solves your problem or there are other issues that need to be solved, please let me know, thank you!

The above is the detailed content of Sync Map may cause an increase in ram and goroutine. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template