Comments (4)
I think I see what is happening here, I will report back soon.
from v.
Hi @despiegk, I have had a look and I see what is happening.
Here you are creating an endless amount of coroutines as the loop never breaks:
mut t:=Test{}
mut c:=0
for {
c++
go monitor(ch2,c,mut &t)
coroutines.sleep(1000 * time.millisecond)
}
And if we look at the monitor function, it also has a loop which never breaks. This means that the coroutine never finishes, and therefore its stack is never freed.
fn monitor(ch chan string, counter int, mut t &Test) {
for {
//println('2 ${m}')
// coroutines.sleep(1 * time.second)
t.mycounter += 1
println('hello from monitor ${counter}')
coroutines.sleep(100 * time.millisecond)
println(t)
}
}
Each time a coroutine is created space is allocated on the heap for it's stack. This stack memory is freed when the coroutine finishes. In your code there is an infinite loop, so the coroutine will never finish and it's stack memory will never get freed. Therefore an endless amount of stacks are being allocated and never freed.
I hope this clears things up.
from v.
the code I tested with was limited till certain nr of co-routines, and the co-routines are just sleeping and printing something every 5 sec
its normal it takes some mem but for only 1000 co-routines 3GB might be much
module main
import coroutines
import time
fn monitor(ch chan string, counter int, mut t &Test) {
for {
//println('2 ${m}')
// coroutines.sleep(1 * time.second)
t.mycounter += 1
println('hello from monitor ${counter}')
coroutines.sleep(5000 * time.millisecond)
println(t)
}
}
pub struct Test{
pub mut:
mycounter int
}
fn main() {
ch1 := chan string{}
ch2 := chan string{}
mut t:=Test{}
mut c:=0
for i in 0..1000 {
c++
go monitor(ch2,c,mut &t)
}
coroutines.sleep(4000 * time.second)
}
from v.
hi @despiegk, I will have a look with your new example. 3Gb sounds too much for that
EDIT:
Actually 3GB is sounding correct. The current stack size being allocated is 8MB (I think, I am checking this) for every coroutine (I'm seeing if this can be decreased).
However I'm sure you can refactor the code to do what you want with less memory. For example by using less coroutines and re-using each one, something like a worker pool. Or by not having so many coroutines running at once, starting 1000 coroutines is not an issue, its the fact that those 1000 coroutines are never ending, and their memory is never cleaned up. if it was a constant flow of coroutines starting and finishing then the memory footprint would be significantly different.
from v.
Related Issues (20)
- compile of tvintris.v failed HOT 11
- Test failed on OpenBSD for vlib/os/os_stat_test.v HOT 1
- Test failed on OpenBSD for vlib/net/http/server_test.v
- Build fails on OpenBSD with clang and cstrict compilation HOT 3
- Map of fixed array initialisations HOT 4
- veb: parallel event loop HOT 5
- struct embedding must be declared at the beginning of the struct body HOT 2
- `coroutines` stuck whole program
- Can't add same element to cleared map HOT 2
- veb: memory leak HOT 1
- After clear(), a map allows double entries
- After calling clear() on a map several times, duplicate keys are allowed
- calling map.clear() fails to change the receiver map, in such a way that it has the exact same observable behaviour as doing `m = {}` for some maps HOT 4
- flag.FlagParser bug with certain combination of flags
- Binary crashes when compiled with tcc on OpenBSD HOT 5
- C compilation error when compiling simple Generic Interface test case
- cgen error for using a `default := p or { T{} }` construct in a generic function HOT 6
- type OneD = []Thing causes unexpected behaviour HOT 1
- FlagParser single letter options dependent on order of flags.
- C error when trying to use a &Struct value where Struct is required instead
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from v.