Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

how java reserves memory

Thread view: 
devnew@gmail.com - 09 Nov 2007 09:43 GMT
i am a bit confused about the way java allocates memory..
is 'reserving memory' and 'allocating memory' two diff things?
can anyone tell me how it is done in these situations?

int i;

int i=12;

MyClass my;
MyClass my=new MyClass();

also is any memory allocated on stack when primitive types are
defined?

thanx in advance
dn
Lew - 09 Nov 2007 14:00 GMT
> i am a bit confused about the way java allocates memory..
> is 'reserving memory' and 'allocating memory' two diff things?
[quoted text clipped - 9 lines]
> also is any memory allocated on stack when primitive types are
> defined?

Memory allocation is a bit complex in Java, and not always what you expect.
For one thing, it changes during the program run, because the optimizer
dynamically rewrites code as it happens.  It is possible for a memory
allocation to disappear altogether if the optimizer sees a way to do that.

The short version is that the JVM (Java Virtual Machine) reserves (allocates
from the operating system (OS)) a block of memory equal to at least the -Xms
value when it starts.  As memory needs in the programs grow, the JVM takes
more memory from the operating system, up to the -Xmx amount.  At that point
it needs to manage its own use more aggressively.

Inside the JVM, programs get their memory from the amount allocated by it from
the OS.  Typically a call to, say, "new MyFoo()" will allocate enough memory
from the Java heap to hold a MyFoo instance.  When no part of the program
refers to that object any longer, the garbage collector (GC) is allowed to
release the memory allocated to that object.

It's allowed to release it.  It's not required to release it.

Memory allocation is fast in Java, typically about ten machine instructions
(about 5 ns on modern processors).  De-allocation for about 95% of objects
costs nothing; garbage collection for most objects is only influenced by how
many are still alive, not how many are unreachable.

Whether primitive values go on the stack or not is up to the optimizer.  Many
might live in registers their whole lives.  It's conceivable the JVM could
decide to use the heap (if, say, autoboxing is involved).  As Java programmers
we do not get to say; it's up to the JVM.

Likewise with object allocation.  While we think of objects as living on the
heap, only, it's possible for the JVM to optimize that into register values,
too, under the right circumstances.

Unlike C# or other languages, the stack / heap dichotomy is not part of the
programming model in Java.

Signature

Lew

Richard Reynolds - 09 Nov 2007 20:10 GMT
>> i am a bit confused about the way java allocates memory..
>> is 'reserving memory' and 'allocating memory' two diff things?
[quoted text clipped - 46 lines]
> Unlike C# or other languages, the stack / heap dichotomy is not part of
> the programming model in Java.

thanks Lew, excellent description, I found that very useful
Christian - 13 Nov 2007 01:55 GMT
Lew schrieb:

> Unlike C# or other languages, the stack / heap dichotomy is not part of
> the programming model in Java.

Do you know if java really doesn`t have a stack under the hood for
faster garbage collecting ?

Wouldn`t it be rather easy to determine what objects in a function call
could be put on a stack instead of a heap... (no Global reference to
them directly or indirectly)

I mean wouldn`t it be helpful for the garbage collector, if it would
know Objects that would usually be put on the stack, so they could be
cleared from the heap when the method that allocated them returned?

So not a real stack but it sounds like an easy to do optimization.
Lew - 13 Nov 2007 02:26 GMT
Lew schrieb:
>> Unlike C# or other languages, the stack / heap dichotomy is not part of
>> the programming model in Java.

> Do you know if java really doesn`t have a stack under the hood for
> faster garbage collecting ?

Of course Java has a stack, but it's got nothing to do with speeding up GC.

> Wouldn`t it be rather easy to determine what objects in a function call
> could be put on a stack instead of a heap... (no Global reference to
> them directly or indirectly)

The JVM does all sorts of optimizations, some of which involve putting objects
on the heap, or even in registers.  Escape analysis is part of how it does that.

> I mean wouldn`t it be helpful for the garbage collector, if it would
> know Objects that would usually be put on the stack, so they could be
> cleared from the heap when the method that allocated them returned?

If they're on the stack, there's nothing to "be cleared from the heap" in the
first place.

> So not a real stack but it sounds like an easy to do optimization.

There is a stack in Java, it's just not part of the programming model (much).
 It holds the method-call frame, variables and the like.  It's also a
convenient tool for the optimizer, as you discerned.

As programmers, we allocate objects on the heap, conceptually.  They may wind
up on the stack, in registers or even not allocated whatsoever, at run time,
according to the optimizer's wisdom, but that is of no moment to the programmer.

Signature

Lew

Roedy Green - 13 Nov 2007 08:12 GMT
>> Unlike C# or other languages, the stack / heap dichotomy is not part of
>> the programming model in Java.
>>
>Do you know if java really doesn`t have a stack under the hood for
>faster garbage collecting ?

What happens under the hood is not part of the programming model. Java
presents a higher level programming interface than C++.  You can't get
as close to the silicon.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 09 Nov 2007 21:45 GMT
On Fri, 09 Nov 2007 01:43:19 -0800, "devnew@gmail.com"
<devnew@gmail.com> wrote, quoted or indirectly quoted someone who said

>also is any memory allocated on stack when primitive types are
>defined?

when a method is called, the stack pointer grows to allow room for all
the temporary variables in the stack frame.  Primitives typically take
1 32-bit slot. A reference to a object takes 32 bits in the usual JVM.
The object itself once allocated, lives on the heap.  In Jet,
optimisation sometimes puts objects themselves on the stack for speed.
This is transparent to your program.  It works to the program just as
if they were heap allocated.

You might find digging under the covers at how the JVM works as
fascinating as I do.  See http://mindprod.com/jgloss/jvm.html
http://mindprod.com/jgloss/jasm.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 09 Nov 2007 21:46 GMT
On Fri, 09 Nov 2007 01:43:19 -0800, "devnew@gmail.com"
<devnew@gmail.com> wrote, quoted or indirectly quoted someone who said

>is 'reserving memory' and 'allocating memory' two diff things?

Usually the term "reserving" is about the size of the entire Java app,
controlled by java.exe command line options.
see http://mindprod.com/jgloss/javaexe.html

The term "allocating" is about finding space for a single object.
http://mindprod.com/jgloss/garbagecollection.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.