Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > question on memory allocation and class instance

Reply
Thread Tools

question on memory allocation and class instance

 
 
Jimmy Zhang
Guest
Posts: n/a
 
      02-17-2004
hi, I have a couple of questions that I would like someone to take a look,
1. java.lang.Object is the super class for all, does that mean a simple
instantiation of any class
will automatically create a java.lang.Object instance, so any simple object
allocation will create
at least two classes.

2. In Java, if I want to allocate a memory block of 1M, does the JVM have to
ensure the existence
of such memory block, even it has to join and recombine smaller blocks. Is
it more expensive with
a GC enabled language than C/C++

Thanks,
Jimmy


 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      02-17-2004
"Jimmy Zhang" <(E-Mail Removed)> wrote in message
news:KGdYb.44723$jk2.108539@attbi_s53...
> hi, I have a couple of questions that I would like someone to take a look,
> 1. java.lang.Object is the super class for all, does that mean a simple
> instantiation of any class
> will automatically create a java.lang.Object instance, so any simple

object
> allocation will create
> at least two classes.



You have some reading to do.
The answer is no, but not because there is the possibility that the answer
could be yes.
The reason the answer is no is because the question has no context.
The term "object allocation will create at least two classes." makes
absolutely no sense.

Do you "typewriters eat cauliflower spaghetti car window divided under forty
dinosaurs" ?
The answer is no, because the question makes no sense.

> 2. In Java, if I want to allocate a memory block of 1M, does the JVM have

to
> ensure the existence
> of such memory block, even it has to join and recombine smaller blocks. Is
> it more expensive with
> a GC enabled language than C/C++


Again, impossible to answer.
Have you tried to "allocate a memory block of 1M" ?
You can't have, since it makes no sense in the context of the Java 2
Programming Language.

http://java.sun.com/tutorial
Good luck !

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform


 
Reply With Quote
 
 
 
 
Jimmy Zhang
Guest
Posts: n/a
 
      02-17-2004
Don't know what you are trying to get across, in particular, I have tried to
allocate a block of 1MB, didn't have
any problem in Java1, or 2.
What is the context are you trying to have?
I am just asking if a single allocation of one object will in fact return
two object, one of them linked to java.lang.Object?
Your comment is nevertheless weird and interesting.

"Tony Morris" <(E-Mail Removed)> wrote in message
news:c0rqln$bka$(E-Mail Removed)...
> "Jimmy Zhang" <(E-Mail Removed)> wrote in message
> news:KGdYb.44723$jk2.108539@attbi_s53...
> > hi, I have a couple of questions that I would like someone to take a

look,
> > 1. java.lang.Object is the super class for all, does that mean a simple
> > instantiation of any class
> > will automatically create a java.lang.Object instance, so any simple

> object
> > allocation will create
> > at least two classes.

>
>
> You have some reading to do.
> The answer is no, but not because there is the possibility that the answer
> could be yes.
> The reason the answer is no is because the question has no context.
> The term "object allocation will create at least two classes." makes
> absolutely no sense.
>
> Do you "typewriters eat cauliflower spaghetti car window divided under

forty
> dinosaurs" ?
> The answer is no, because the question makes no sense.
>
> > 2. In Java, if I want to allocate a memory block of 1M, does the JVM

have
> to
> > ensure the existence
> > of such memory block, even it has to join and recombine smaller blocks.

Is
> > it more expensive with
> > a GC enabled language than C/C++

>
> Again, impossible to answer.
> Have you tried to "allocate a memory block of 1M" ?
> You can't have, since it makes no sense in the context of the Java 2
> Programming Language.
>
> http://java.sun.com/tutorial
> Good luck !
>
> --
> Tony Morris
> (BInfTech, Cert 3 I.T.)
> Software Engineer
> IBM Australia - Tivoli Security Software
> (2003 VTR1000F)
> Sun Certified Programmer for the Java 2 Platform (1.4)
> Sun Certified Developer for the Java 2 Platform
>
>



 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      02-17-2004
> I am just asking if a single allocation of one object will in fact return
> two object, one of them linked to java.lang.Object?

This is completely out of context - it makes no sense at all.


Post the source code where you believe you have allocated a memory block of
1MB.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform


 
Reply With Quote
 
Woebegone
Guest
Posts: n/a
 
      02-17-2004

"Jimmy Zhang" <(E-Mail Removed)> wrote in message
news:QWeYb.46888$uV3.95703@attbi_s51...
> Don't know what you are trying to get across, in particular, I have tried

to
> allocate a block of 1MB, didn't have
> any problem in Java1, or 2.
> What is the context are you trying to have?
> I am just asking if a single allocation of one object will in fact return
> two object, one of them linked to java.lang.Object?
> Your comment is nevertheless weird and interesting.
>
> "Tony Morris" <(E-Mail Removed)> wrote in message
> news:c0rqln$bka$(E-Mail Removed)...
> > "Jimmy Zhang" <(E-Mail Removed)> wrote in message
> > news:KGdYb.44723$jk2.108539@attbi_s53...
> > > hi, I have a couple of questions that I would like someone to take a

> look,
> > > 1. java.lang.Object is the super class for all, does that mean a

simple
> > > instantiation of any class
> > > will automatically create a java.lang.Object instance, so any simple

> > object
> > > allocation will create
> > > at least two classes.

> >
> >
> > You have some reading to do.
> > The answer is no, but not because there is the possibility that the

answer
> > could be yes.
> > The reason the answer is no is because the question has no context.
> > The term "object allocation will create at least two classes." makes
> > absolutely no sense.
> >
> > Do you "typewriters eat cauliflower spaghetti car window divided under

> forty
> > dinosaurs" ?
> > The answer is no, because the question makes no sense.
> >
> > > 2. In Java, if I want to allocate a memory block of 1M, does the JVM

> have
> > to
> > > ensure the existence
> > > of such memory block, even it has to join and recombine smaller

blocks.
> Is
> > > it more expensive with
> > > a GC enabled language than C/C++

> >
> > Again, impossible to answer.
> > Have you tried to "allocate a memory block of 1M" ?
> > You can't have, since it makes no sense in the context of the Java 2
> > Programming Language.
> >
> > http://java.sun.com/tutorial
> > Good luck !
> >
> > --
> > Tony Morris
> > (BInfTech, Cert 3 I.T.)
> > Software Engineer
> > IBM Australia - Tivoli Security Software
> > (2003 VTR1000F)
> > Sun Certified Programmer for the Java 2 Platform (1.4)
> > Sun Certified Developer for the Java 2 Platform
> >
> >

>
>


Hi Jimmy,

Every object in Java is ... an Object. Allocation (instantiation) via new
creates and returns a reference to one object. Java's intrinsic polymorphism
means that the object may be referenced as an instance of any class it
extends (including Object) or of any interface it implements, as well as its
own static type. One object, potentially many types.

As for your question about memory allocation, Java doesn't allow such an
operation. You can allocate objects, whose size might approximate 1MB, but
that (the size) is not up to you as a programmer to determine. Of course,
you could I suppose guess that "new byte[1000000]" would allocate about 1MB,
but that's not the same as e.g. a "malloc(1000000)" since Java has no
equivalent of a void*. Is that what you're driving at? Once you have that
MB, you can't share it around if things get tight for your program. Also,
although polymorphic, Java is very strongly typed, so there's no way to cast
your byte[] into e.g. a Widget. This is part of the reason your original
question lacked context.

If you're thinking of simply having your program claim an arbitrary hunk of
heap memory "just in case," you might look at the -Xms flag for the JVM.

HTH, Regards,
Sean.


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-17-2004
Jimmy Zhang wrote:

> hi, I have a couple of questions that I would like someone to take a look,
> 1. java.lang.Object is the super class for all, does that mean a simple
> instantiation of any class
> will automatically create a java.lang.Object instance, so any simple
> object allocation will create
> at least two classes.


As Tony Moris said, you are having difficulty with the terminology which makes
it difficult to understand your question.

If what you are asking is "does creating an instance of a subclass of Object
automatically create an instance of Object too ?" then the answer is no. Each
instance of a subclass contains slots to hold the fields it inherits from its
superclasses, as well as the slots needed for the fields defined in that class,
so -- in a very loose sense -- it contains the superclass "object" nested
within it. But there is only one object.

If that isn't what you meant then I suggest you ask again.


> 2. In Java, if I want to allocate a memory block of 1M, does the JVM have
> to ensure the existence
> of such memory block, even it has to join and recombine smaller blocks. Is
> it more expensive with
> a GC enabled language than C/C++


If you ask for -- say -- an array of a million bytes, then its up to the Java
runtime to try to find the memory to satsify the request. *How* it does that
is implementation-dependent, but common implementation techniques would involve
doing a GC if the memory isn't immediately available. That is implementation
dependent too, but common GC techniques have the effect of compacting memory so
that available smaller blocks would be swept up into a larger single block.

There is really no way to compare the "expense" with C/C++ here, since C has
explicit pointers there is no (normal) way for the memory subsystem to move
allocated blocks around at runtime. Hence C malloc() cannot shuffle the heap
to remove fragmentation. (I'm sure it'd be possible to create a C
implementation that used an extra level of indirection in it's "pointers" to
allow such reshuffling, but that largely defeats the point of using C in the
first place, and I'm not aware of any such implementations).

BTW, there is nothing to say that the byte[1000000] has to be allocated in a
contiguous chunk by the Java runtime. It could be -- say -- allocated as a
number of unrelated 16K chunks. There is no way to tell using Java (or even
JNI) whether the runtime does that. I don't know of any implementations that
*do* do it, but there *might* be some -- it might make sense on
resource-restricted devices for instance, especially if they don't use a
compacting GC.

-- chris


 
Reply With Quote
 
Albert Deinbeck
Guest
Posts: n/a
 
      02-17-2004
"Jimmy Zhang" <(E-Mail Removed)> schrieb im Newsbeitrag
news:KGdYb.44723$jk2.108539@attbi_s53...
> hi, I have a couple of questions that I would like someone to take a look,
> 1. java.lang.Object is the super class for all, does that mean a simple
> instantiation of any class
> will automatically create a java.lang.Object instance, so any simple

object
> allocation will create
> at least two classes.
>
> 2. In Java, if I want to allocate a memory block of 1M, does the JVM have

to
> ensure the existence
> of such memory block, even it has to join and recombine smaller blocks. Is
> it more expensive with
> a GC enabled language than C/C++
>
> Thanks,
> Jimmy


Hi Jimmy,
I would like to try and clear up some misconceptions you seem to have.
1. One class has multiple instances. The class holds the methods, the
instance the variables.
2. java.lang.Object only defines methods, so nothing is added to child class
instances. (as far as I know ?)
3. java.lang.Object class exists only once (per classloader). All other
classes refer to it. Again, this reference is in the class, not in every
instance.
4. If java.lang.Object would define variables, they would be part of every
class instance.
5. The parent classes of a class do not produce seperate instances. If you
have A extends B extends C and you create new A() you get 1 (one) object,
not 3 refering to each other.

6. You cannot allocate a memory block of 1M. You can make an object which
can hold the same data as a 1M block can.
byte[1000000] is not guaranteed to be a block of 1M. It is guaranteed to
refer to 1000000 primitives of type 'byte'.
7.The VM is free to allocate an array in one block or in several pieces. You
cannot predict what the VM will do.
8. There is no such thing as a pointer in java. myArray[123] is NOT
*(myArray+123*sizeof(Element))
9. Java GC is more expensive than C/C++ in terms of runtime. However, it is
much more secure in terms of memory leaks.

Albert


 
Reply With Quote
 
Thomas Richter
Guest
Posts: n/a
 
      02-17-2004
Hi,

> There is really no way to compare the "expense" with C/C++ here, since C has
> explicit pointers there is no (normal) way for the memory subsystem to move
> allocated blocks around at runtime. Hence C malloc() cannot shuffle the heap
> to remove fragmentation. (I'm sure it'd be possible to create a C
> implementation that used an extra level of indirection in it's "pointers" to
> allow such reshuffling, but that largely defeats the point of using C in the
> first place, and I'm not aware of any such implementations).


FYI, and as a side remark, early versions of MacOs did just that. The
corresponding construct was called "a handle", and the kernel (the
memory manager) reshuffled the heap when required. All that in C (or
in pascal).

So long,
Thomas
 
Reply With Quote
 
Tim Ward
Guest
Posts: n/a
 
      02-17-2004
"Thomas Richter" <(E-Mail Removed)-berlin.de> wrote in message
news:c0td44$clp$(E-Mail Removed)-Berlin.DE...
>
> FYI, and as a side remark, early versions of MacOs did just that. The
> corresponding construct was called "a handle", and the kernel (the
> memory manager) reshuffled the heap when required. All that in C (or
> in pascal).


Likewise early versions of Windows.

--
Tim Ward
Brett Ward Limited - www.brettward.co.uk


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-17-2004
Thomas Richter wrote:
> > (I'm sure it'd be
> > possible to create a C implementation that used an extra level of
> > indirection in it's "pointers" to allow such reshuffling, but that
> > largely defeats the point of using C in the first place, and I'm not
> > aware of any such implementations).

>
> FYI, and as a side remark, early versions of MacOs did just that. The
> corresponding construct was called "a handle", and the kernel (the
> memory manager) reshuffled the heap when required.


Interesting, thanks.

And I could add that more modern OSs' virtual memory systems are "really" doing
something very similar, for all it's done in hardware rather than by the
compiler.

-- chris


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
Invoking instance before main and memory allocation for static member Alex Vinokur C++ 1 07-24-2003 12:46 PM



Advertisments