• Post Reply Bookmark Topic Watch Topic
  • New Topic

How methods are stored in java?  RSS feed

 
Mamata Gelanee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am wondering that java allocates memory for objects in heap..
As I belive that it reserves space that every instance variables required..
like if objects has two integer variable then it will occupy 4*2 byte..
now question is that how java allocates memory for instance methods..???

Thanks for your help
 
fred rosenberger
lowercase baba
Bartender
Posts: 12559
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mamata Gelanee wrote:if objects has two integer variable then it will occupy 4*2 byte..

I don't think you can say this is definitively true. I believe anyone who implements a JVM is free to do it however they like, as long as the required BEHAVIOR is modeled. It may make sense on some hardware platforms to reserve 32 byte blocks at a time. So if you have one int, it reserves 32 bytes. If you have four ints, it reserves....32 blocks.

For the most part, you don't really need to know such things. If this is an academic exercise, then ok...but otherwise, it really doesn't matter.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16047
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a big difference between objects and variables on one hand, and methods on the other hand.

Objects and variables are allocated dynamically - when the program runs, memory is allocated and freed for objects and variables as needed. When you use the new operator, memory is allocated for an object, and later on, when the object is not used anymore, the garbage collector frees the memory used by the object.

Methods do not consist of dynamic data. They just consist of a block of bytecode, which is loaded from the *.class file when a class is loaded. The bytecode is stored in memory somewhere as long as the class is loaded. Note that the bytecode is not copied for every instance that you make of a class. That would be totally unnecessary, because the bytecode is fixed - there's no point keeping the same fixed bytecode in memory multiple times.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interpreting the question a little bit: I don't think the question is about memory for the method code itself, but how does memory get allocated when you CALL a method.

There are two main memory stores in java. The "Stack" and the "Heap"
As you have pointed out, memory for Objects created by "new" gets allocated from the heap.
Memory for method calls and the local variables in a method are allocated from the Stack.

When you call a method it allocates memory onto the "top" of the stack for that methods variables.
If you call another method, then more memory gets added to the "top" of the stack.
Every time you call another method, allocates more memory.
You can think of it as "stacking" a pile of boxes up, only ever dealing with the one on top.

When a method returns, the memory it used for local variables can be freed immediately - you can think of this as removing a box from the top of your stack.
The box on top now contains the memory for the method you have just returned to, and so it can keep executing happily.

Of course you can only "stack" the boxes so high before you run out of room. Which is when you encounter a stack overflow error.

 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a beginning question. Moving discussion.
 
Mamata Gelanee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Interpreting the question a little bit: I don't think the question is about memory for the method code itself, but how does memory get allocated when you CALL a method.

There are two main memory stores in java. The "Stack" and the "Heap"
As you have pointed out, memory for Objects created by "new" gets allocated from the heap.
Memory for method calls and the local variables in a method are allocated from the Stack.

When you call a method it allocates memory onto the "top" of the stack for that methods variables.
If you call another method, then more memory gets added to the "top" of the stack.
Every time you call another method, allocates more memory.
You can think of it as "stacking" a pile of boxes up, only ever dealing with the one on top.

When a method returns, the memory it used for local variables can be freed immediately - you can think of this as removing a box from the top of your stack.
The box on top now contains the memory for the method you have just returned to, and so it can keep executing happily.

Of course you can only "stack" the boxes so high before you run out of room. Which is when you encounter a stack overflow error.




That is it..

Great answer..
I want this answer
Thank you very much

Do you wanna party for this..
:-p
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be out of topic. Generally an object takes space as following:

Total memory usage for a data type value:
1)Primitive type: 4 bytes for int, 8 bytes for double, …
2)Object reference: 8 bytes.
3)Array: 24 bytes + memory for each array entry.
4)Object: 16 bytes + memory for each instance variable + 8 bytes if inner class (for pointer to enclosing class).
5)Padding: round up to multiple of 8 bytes.

So say if you have class:

public class String
{
private char[] value;
private int offset;
private int count;
private int hash;
...
}

So total memory taken by object is calculated as :

Object overhead = 16 bytes
int variables = 4 * 3 = 12 bytes
char array = 2 * N + 24 + 8 byte for reference to array

Then total memory is = 16 + 4 * 3 + 24 + n * 2 + 8 = 64 + 2 * n and result should be multiple of 8. So say if n = 1 so total becomes = 64 + 2 = 66
so for this we add 6 as padding so total size of an object becomes = 66 + 6 = 72 bytes
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure? You would have to look through the JVM specification, but I think you will find the memory footprint is only defined for the primitive numeric types and for everything else it is implementation‑dependent. Those 8 bytes for a reference may only apply to a 64‑bit machine.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was mentioned in my algorithm class. Robert Sedgwick was the instructor so i suppose it is authenticated.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16047
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe those numbers are correct for a specific JVM brand and version (for example, Oracle's 64-bit JVM version 8 for Windows).

In general, how much memory is used by particular data types, arrays and objects is not specified in the Java Language Specification or the Java Virtual Machine Specification - it's deliberately left up to the implementation to use whatever is best for whatever reason (for example, to use whatever size is most efficient on a particular target platform).
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with you Jasper and Campbell. I thought to share with all so i did it, may be it is specific to particular JVM.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!