Giter VIP home page Giter VIP logo

Comments (4)

joe-conigliaro avatar joe-conigliaro commented on September 25, 2024

I think I see what is happening here, I will report back soon.

from v.

joe-conigliaro avatar joe-conigliaro commented on September 25, 2024

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.

despiegk avatar despiegk commented on September 25, 2024

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

image

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.

joe-conigliaro avatar joe-conigliaro commented on September 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.