Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doesn't create 10000 instances with forloop.Why?  RSS feed

 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here i can't able to create 10000 instance with forloop.i do know why? Can anyone help me with this.

OUTPUT:
Total memory : 16252928
Free memory : 15800136
Free memory after creating 10000 instances : 15800136
Free memory after garbage collector is called : 15981408

So far,i have created 10000 instances but for me it shows that i've nothing created. SORRY FOR MY ENGLISH... :'(

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new class object isn't used anywhere so it is available for garbage collection immediately. If you want to see the memory go down, try creating an ArrayList (outside the loop) and adding the instances to it each loop.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinoth vino wrote:
Here i can't able to create 10000 instance with forloop.i do know why? Can anyone help me with this.

OUTPUT:
Total memory : 16252928
Free memory : 15800136
Free memory after creating 10000 instances : 15800136
Free memory after garbage collector is called : 15981408

So far,i have created 10000 instances but for me it shows that i've nothing created. SORRY FOR MY ENGLISH... :'(




Every instance you created is just getting garbage collected and the memory is freed. Your instances become eligible right after creation at each loop iteration.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is problably what your are trying to do; just put a reference of your created instances in a data structure so they don't get garbage collected:

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!