Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

memory allocation

 
gylph knor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

could anyone please explain that if i make 1000s of instances of the same class then how will the memory be allocated to the instances of the class( i mean to diffent parts of the class i.e methods and attributes ) ...... please help !!!
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to say that will JVM be out of memory. If yes, then the JVM might go out of memory to allocate for the objects and throw an OutOfMemoryError...
 
gylph knor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no i mean to say that if my class takes 1 mb of memory in total then for suppose 10 objects will there be an allocation of 10*1mb=10mb of memory??
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mean to diffent parts of the class i.e methods and attributes


The size of an object of a class depends on the instance fields. Methods don't occupy memory in a class object (as far as I know). static fields also are shared among instances of a class. so they also don't occupy memory in individual objects of a class.

if my class takes 1 mb of memory in total


if an object of your class occupies 1mb, then 10 objects will occupy 10 mb memory.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's true. 10mb, according to your example, would be the memory used for the objects of your class because each object would be using its own space over the heap. But then taht 1mb should be the memory taken by the object, apart of the loaded class.
 
gylph knor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnaks !! got it ..
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gylph knor wrote:no i mean to say that if my class takes 1 mb of memory in total then for suppose 10 objects will there be an allocation of 10*1mb=10mb of memory??

Hmmm... quite a spacious class
But on the other hand you can create one big array and it will be allocated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic