Extreme memory usage when `synchronized( this )` is used

Daniel Kozák via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 11 03:24:43 PDT 2015


On Mon, 11 May 2015 09:40:28 +0000
tcak via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

> On Monday, 11 May 2015 at 09:20:50 UTC, Daniel Kozák wrote:
> >
> > On Mon, 11 May 2015 09:09:07 +0000
> > tcak via Digitalmars-d-learn 
> > <digitalmars-d-learn at puremagic.com> wrote:
> >
> >
> > I think synchronize(this) prevents GC from collect memory
> 
> I am not sure whether this is expected behaviour from 
> `synchronization` keyword.
> 
> Similar code in Java. It has settled on about 62M and didn't 
> increase at all.
> 
> [code]
> public class Main{
> 	private void other(){}
> 
> 	public void close(){
> 		synchronized( this ){
> 			other();
> 		}
> 	}
> 
> 	public void hasData(){ System.out.println("Has Data"); }
> 
> 	public static void main( String[] args ){
> 		for(long i=0; i < 250000000; ++i){
> 			Main conn = new Main();
> 
> 			conn.hasData();
> 
> 			conn.close();
> 
> 			conn = null;
> 		}
> 	}
> }
> [/code]

But yes I would say, it is not intentional behaviour. It should use
weak reference, so It would be possible for GC to collect it. Please
fill issue



More information about the Digitalmars-d-learn mailing list