• Post Reply Bookmark Topic Watch Topic
  • New Topic

How many memory is allocated in heap to the same reference variable when instantiated more than one  RSS feed

 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If create an object like this then I know that s1 will be stored in heap, but If I create two objects with same name then will the memory be allocated to the s1 variable twice or only once it is allocated ? (Please don't ask me why you want to create in such way)
The code below show the scenario



-Thank you,
Rajeev
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You have not tried to compile and run this program, have you? And yes, we will ask you WHY do you want to do that?
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm Sorry, I didn't compile . Line number 6 should be: ProcessingThread1 pt1 = new ProcessingThread1(); and Line number 9 should be :Thread t2 = new Thread(pt1, "t2"); Actually I want to know weather memory is allocated twice or once at the sentence SampleClass s1 = new SampleClass().

--Thanks
-Rajeev
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the code won't compile, then it is very simple to answer: no memory will be allocated for anything.
I presume you are asking this out of simple curiosity, but why do you need two object, let along two objects with the same reference in the same class?
How much memory do you think would be allocated for sample object created, or per thread created.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'll give you some clues and you can put these together to get your answer:

1. s1 is a local variable in the run() method of the ProcessingThread class.
2. run() is an instance method of the ProcessingThread class.
3. In your main() method, you create two instances of ProcessingThread

The above should help you deduce how many instances of SampleClass you'll end up creating in your code.

My question to you is this: Why do you even care about the memory allocated for that line of code? IMO, you should first care more about WHAT you're trying to do, not how the JVM is doing it. Don't stick your nose into the JVM's business because managing objects and memory usage is NOT your responsibility; at least not until you can absolutely prove that the JVM needs some help. Your primary responsibility as a programmer is to write clean, well-factored, maintainable code.
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replying. As Mr.Campbell had said, I asked this question out of curiosity only. Mr. Junilu I keep these things(write clean, well-factored, maintainable code) in mind while writing code. Actually after those clues. I think I got the answer i.e each instance of ProcessingThread will have separate stack for storing the local variable s1. And also two objects one from each ProcessingThread are created in the Heap. Is my assumption correct?

--ThankYou
-Rajeev
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each thread has its own call stack, yes. If you create a local variable in two threads, you will get two instances each eligible for garbage collection as soon as the run() method terminates.
If you had two references to objects each with a method using Sample, you would also get two instancesOne cannot tell whether they would undergo garbage collection together or in turn, or maybe never.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a good idea to ask questions and to play around with code out of curiosity. You probably learn more when things go wrong than when they work correctly. But do tell us if code is simply out of curiosity; we see many people who write strange code and seem to think it is correct, and suitable for real life use.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically there is only one stack, which is why if you have an infinitely recursive loop you will eventually get a stack overflow. Each method call will get its own stack frame so that the computer can keep track of the list of parameters, the local variables, and the return address.

You really don't need to, and shouldn't at this point, worry about the inner workings of memory management and the use of the stack and heap. That's like trying to learn how to drive and worrying about whether you have rack and pinion steering or a recirculating ball. In either case, that stuff doesn't really matter much to you as a beginner who is just trying to learn how to do basic things.

Edit: I see that the comment about multiple call stacks was in the context of multiple threads. In that regard, I'll defer to Campbell but that's certainly what this document implies
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would appear the stacks are 1MB each:
The Document Junilu linked to wrote:The threads library allocates 1 Mbyte of virtual memory for each thread's stack with no swap space reserved.
That does look like multiple stacks, but as far as I know there is a single stack in a non‑threaded environment.
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Edit: I see that the comment about multiple call stacks was in the context of multiple threads. In that regard, I'll defer to Campbell but that's certainly what this document implies

...? but that's a Solaris / C / Pthreads document
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:
...? but that's a Solaris / C / Pthreads document

This article seems to imply that the same mechanics apply in Java. Note the Thread constructor: Thread(group, target, name, stackSize)

Edit: It's official, each JVM thread has its own stack: http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.5.2
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!