• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multithreading queries with memory management  RSS feed

 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

To the best of my knowledge, there are 2 types of memory in JAVA-
1)heap-->objects and static variables stored here
2)stack-->instance and local variables stored here

I have the following few queries:
1)Where are reference variables stored
2)Incase of multi threading, does threads share heap area and are they created in heap area?
I googled and found that each thread will in turn have PCs and stack frames to know which instruction to execute next
3)If too many threads are created, how and why will it affect the performance of the application
4)Will thread pool executor avoid problem mentioned in(3).If so how?
 
maxim markin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) all references stored in stack but objects stored in Heap (reference referencing to heap memory)
2) threads share heap
3) if you have too many threads you will observe performance degradation because JVM will switch context between many threads all the time (most of time)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:To the best of my knowledge, there are 2 types of memory in JAVA-
1)heap-->objects and static variables stored here
2)stack-->instance and local variables stored here

if you think about it, that can't be right. What if an instance variable is an object?

I have the following few queries:
1)Where are reference variables stored

What is a reference variable? It's any variable that is NOT a primitive - ie, it's an object. Perhaps that answers your question.

2)Incase of multi threading, does threads share heap area and are they created in heap area?

The heap is simply a space, so it's shared by ALL Threads (and in the context that you're referring to, it's a Thread; not a thread) - indeed, all Java code - that creates objects. As to the 2nd part: A Thread is an object.

I googled and found that each thread will in turn have PCs and stack frames to know which instruction to execute next

I fear you're headed down a long and winding road (cue: the Beatles).

Most of this stuff simply isn't important; and it will not make you a better programmer. In fact, I created the StackOrHeap page for precisely this sort of question.

And if you want to learn about multi-threading (although I suspect it may be a little soon): Learn about synchronization. It's not simple, and there's a fair bit of reading involved; but what you DON'T need to know about is where things are.

HIH

Winston
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
kushi kumar wrote:To the best of my knowledge, there are 2 types of memory in JAVA-
1)heap-->objects and static variables stored here
2)stack-->instance and local variables stored here

if you think about it, that can't be right. What if an instance variable is an object?

I think there is a terminology issue going on here. Variables aren't objects, they can hold a reference to an object, but they aren't objects themselves. But the initial statement is incorrect as well:
Under normal circumstances you should consider*
1) The heap holds class and instance data - that is it holds data associated with Classes (static variables) and data associated with Instances (instance variables).
2) The stack holds local variables.

kushi kumar wrote:I have the following few queries:
1)Where are reference variables stored

A variable which holds a reference follows the same rules as a variable that holds a primitive. See above for where it would be located.

Resource: Chapter 2 of the JVMS - http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html
* This is 'normal' but there are optimizations that can happen to change normal under specific circumstances.

kushi kumar wrote:3)If too many threads are created, how and why will it affect the performance of the application

maxim already answered this, but I will add to his answer. In addition to the possible cost of context switching, each thread will also have a native thread spawned from the OS, which can be a costly process. It will have its own stack, which consumes memory, and could lead to memory issues. If your threads are properly synchronized then contending for the same data could be blocking other threads from executing. If multiple threads access a resource which which is not multi threaded, the resource could block threads or perform much worse when accessed from multiple threads (a classic example of this is writing data to a disk drive. Writing from multiple threads is much worse than writing from a single thread because the write head has to jump from location to location - it can spend much more time spinning and seeking than writing.)
kushi kumar wrote:4)Will thread pool executor avoid problem mentioned in(3).If so how?

Only partially. You can use the executor to limit the number of active threads and allow you to re-use the same threads. This reduces the cost of spawning OS threads because it will be done less often. If you are careful in creating the thread pool you can reduce the amount of contention and number of context switches you need, but that isn't automatic, you have to tune the pool size and settings to find the correct balance.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:I think there is a terminology issue going on here. Variables aren't objects, they can hold a reference to an object, but they aren't objects themselves.

Yeah, I didn't really want to go into that one - where are the references to objects held?

Like I said, it's never stopped me as a programmer - and the fact is: I don't exactly know, or care, where my variables are stored. Indeed, I don't really care much where Java stores anything, as long as it gets it right.

There are many more important things to be worrying about than the mechanics of the language. And even if you find out all there is to know about it, all it makes you is a plumber. You (that is, kushi, I presume) wants to be an architect - ie, a programmer.

Winston
 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Steve-->Thanks... I really wished I was able to vote for your answer more than once Between I meant objects;static variables are stored in heap
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:@Steve-->Thanks... I really wished I was able to vote for your answer more than once Between I meant objects;static variables are stored in heap

So, kushi. Now you know that, what do you know? How do you think it might help you when you're programming?

Winston
 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Steve Luke wrote:I think there is a terminology issue going on here. Variables aren't objects, they can hold a reference to an object, but they aren't objects themselves.

Yeah, I didn't really want to go into that one - where are the references to objects held?

Like I said, it's never stopped me as a programmer - and the fact is: I don't exactly know, or care, where my variables are stored. Indeed, I don't really care much where Java stores anything, as long as it gets it right.

There are many more important things to be worrying about than the mechanics of the language. And even if you find out all there is to know about it, all it makes you is a plumber. You (that is, kushi, I presume) wants to be an architect - ie, a programmer.

Winston


@Winston- I know there lot of things to worry about but these things just crossed my mind... I think there is nothing wrong to do a bit of brainstorming rather than not thinking even once I suppose...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:
@Winston- I know there lot of things to worry about but these things just crossed my mind... I think there is nothing wrong to do a bit of brainstorming rather than not thinking even once I suppose...



I think what Winston is doing is "brainstorming". In my opinion, it may be good to just learn stuff, but you also need to know how can you use it. Otherwise, all you have is facts that you don't know what to do with. In other words, gathering "useless" facts is fine -- it may be useful in the future. However, gathering facts with no concern of its value is probably not .

Henry
 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston and @Henry-->2 of these questions were asked by an interviewer to me
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:@Winston and @Henry-->2 of these questions were asked by an interviewer to me

I thought that might be the case. Either that, or part of study for the SCJP exam.

Unfortunately, we could easily set up a "rogue's gallery" thread of bad interview questions that would run for months, and I'm afraid you're the victim of one.

It's quite possible that the interviewer (or the person who set the question) comes from a C/C++ background, where this sort of stuff is important, but in Java, there's no malloc(), no free(), and - most importantly - no 'destroy' (~). It's the JVM's business to decide where things go and how long they live for so, beyond knowing that there are areas called 'the Stack' and 'the Heap', there's really very little else to be said. You certainly can't change how the JVM uses them, and you shouldn't try.

Winston
 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston-chill... The interviewer was a senior Tech Lead in Java When it comes to interviews can't help it.. Its their choice to ask the kind of ques they want Anywayz thanks..
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:@Winston-chill... The interviewer was a senior Tech Lead in Java

Which just makes the question all the more astounding. However, everybody came from somewhere; and it's possible, like I said, that his background is C/C++. Mine was, and it took me a long time to understand that all this structural stuff to do with memory just isn't important in Java.

And believe me, once you do, it's very liberating.

Winston
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any tips on how to actually deal with such interview questions? Any smart answers?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:Any tips on how to actually deal with such interview questions? Any smart answers?

I honestly don't know.

Personally, for that particular question, I'd give some marks for knowing the basics about how Java uses the Stack and the Heap; and a gold star for knowing that it really isn't important. Because that indicates real knowledge.

You have to be very careful about that stuff though. You might well be marked down for seeming like a 'smart aleck'.

Winston
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I should actually quote the incident here.

So the question was how is memory managed in Java.
Then I said there is a garbage collector that frees the objects that are no longer referenced. We don't have to worry about it as the memory is automatically managed. Gc runs periodically and frees the memory that can be freed.

He's quiet for 2 minutes.
Then I continue - Ok, I know that there is a Stack and there is a Heap. Heap is where the objects live. Stack is where the local variables live. Method calls are maintained in stack. Is that what you were looking for?

Again the guy is quiet.
Then I continue - Ok, I know there are strong references, weak references, soft references, and one more type which I don't remember. Is that what you want me to speak on?

After 1 minute, I say - Ok, tell me what aspect of it you want my answer to address.
He finally says this - how does the garbage collector work.

At this point I just said - I have no idea as I have not had any opportunity to really work on such projects and I believe it must be an implementation detail. I don't know if this algorithm is specified anywhere. If it is, I am just not aware of it.

Then he asked, if you to write GC code, how would you write it.
I just said I would use a Tree.

Thank God, he changed the topic after that.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:He finally says this - how does the garbage collector work.
At this point I just said - I have no idea as I have not had any opportunity to really work on such projects and I believe it must be an implementation detail. I don't know if this algorithm is specified anywhere. If it is, I am just not aware of it.

Which sounds like a perfectly valid answer. In fact, all of them do. Sometimes, all they're trying to do is catch you out by stating something as fact which is actually not true. And saying you don't know something rather than giving a lot of woffle is often all they want to hear.

Then he asked, if you to write GC code, how would you write it.

My answer would be: I wouldn't; I'd give it to an expert. But, like I say, you have to be careful with answers like that.

About the only other thing I can advise is this: if you don't understand a question, say so. And when you do, make sure you state why.

Winston
 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston - Thanks for your insights.. Appreciated...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:@Winston - Thanks for your insights.. Appreciated...

You're most welcome.

Winston
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Winston. Yeah, it may be a better idea to ask the interviewer to elaborate more on the question sometimes. I'll keep this in mind.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!