?
Solved

memory usage

Posted on 2003-03-12
14
Medium Priority
?
361 Views
Last Modified: 2010-03-31
who knows the memory usage of a reference whose value is set to null?
and the usage of the primitive type such as int?
0
Comment
Question by:SeaCosmos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
14 Comments
 

Author Comment

by:SeaCosmos
ID: 8117755
memory usage
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8117788
When reference is set null, that object will be garbage collected by garbage collector whenever it runs and based on the algorithm implemented for garbage collection.

So until object is not garbage collected, it would have its allocation in the memory(heap), though reference has been set to null.

int takes 32 bit in memory, it is used for integer values.
0
 

Expert Comment

by:Rezantis
ID: 8117796
Primitive types such as int are 4 bytes apiece, I believe; long and double are 8 bytes each.

Yeah, byte also takes up 4 bytes.  ^^

Also, I believe java takes up memory in 8 byte blocks, so I'm not exactly clear on how the 4 byte usage of most primitives work in that situation . . .

References to nulls only takes up the memory space of the pointer; which I think is eight bytes.  It's been a while since I took that class.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:ksj786
ID: 8118041
may be this article will help you
http://java.about.com/library/tutorials/bltut-009d.htm
also you can find about non integral types
http://java.about.com/library/tutorials/bltut-009b.htm

best of luck
0
 

Author Comment

by:SeaCosmos
ID: 8118105
so it means that if object is not garbage collected,one
reference would have taken up 8 bytes space in the memory though it was set to null?the memory usage equals to two int primitives...
when i created a 100000 members String array,though i don't new the object of String,it can still take up nearly 1M bytes memory space.
0
 

Author Comment

by:SeaCosmos
ID: 8118116
so it means that if object is not garbage collected,one
reference would have taken up 8 bytes space in the memory though it was set to null?the memory usage equals to two int primitives...
when i created a 100000 members String array,though i don't new the object of String,it can still take up nearly 1M bytes memory space.
0
 

Expert Comment

by:Rezantis
ID: 8118996
If you define an array of String objects, of size 100000, Java will allocate you enough memory for 100,000 Strings as soon as you define that array - even if you don't instantiate them.
0
 
LVL 3

Accepted Solution

by:
allahabad earned 100 total points
ID: 8121452
Reference is object handle, its not the object footprint.
You access object using the reference(handle). So when you make reference=null,
you are making handle null, means this object can'nt be acessed after reference is set to null.
This reference takes 1 bytes( i am may be correct), but depends on JVM implementation.

Now , your object is still on the heap, though you have made the refernece null.
This object space on the heap will be released by garbage collector, whenever it runs.

There are  two steps, one is initialization and other is construction.
Before object construction, object is initialized with default values of the
attributes defined in the class defintion.

public class Test{


 
     String lName;
     String fName;
     int age;
 
 
     public Test(){
        lName = "Last Name";
        fName = "First Name";
        age =32;
 
     }
 
 
}

To call(i have shown just method):

public static void main(String args[]){

   
   
   Test t  = new Test(); // before running this line lName, fName and age will
   take thier memory on heap and initialized with defalt values , in this case
   null for String type and 0 for int type. After that these default values will
   overridden by the values given in the constructor.
   
   
}

when i created a 100000 members String array,
though i don't new the object of String,it can still
take up nearly 1M bytes memory space.

When you declare array:
String[] name = new String[100000];

Though you dot not have any String in the array right now, this name array
will take 8*100000 bits for references of the String.
0
 

Author Comment

by:SeaCosmos
ID: 8124161
thanks to allahabad,ksj786 and Rezantis.^^

thanks for the detailed explanation.

This reference takes 1 bytes( i am may be correct), but depends on JVM implementation.

The reference holds the address of the object,so its memory taken depends on JVM implementation.I think it at least takes 1 byte.and if it is like Rezantis said that java takes up memory in 8 byte blocks,the reference set to null will take at least 8 bytes.

at last may i ask u some questions of English grammar?(i m not from united stats.)

to allahabad:
"So until object is not garbage collected, it would have its allocation in the memory(heap), though reference has been set to null." here,why did you use "until object is not garbage collected" but not "until object is garbage collected"?

to Rezantis:
"It's been a while since I took that class."
what did u mean in that sentence?

0
 
LVL 3

Expert Comment

by:allahabad
ID: 8124326
Yes, my mistake on grammar , line has to be "until object is garbage collected".
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8124358
SeaCosmos, Thanks for points.
0
 

Author Comment

by:SeaCosmos
ID: 8124392
you are welcome.^^
wish to be good friends.
0
 

Expert Comment

by:Rezantis
ID: 8124399
I was saying that it's been a long time since I took that class at university.  A class where I was taught by a lecturer, not a java class.  ^^;
0
 

Author Comment

by:SeaCosmos
ID: 8124461
thanks a lot.

i graduated from university two years ago and started to do some java programming.From then on,the first impression to me , when i read the word of class, is that it must mean java class but not the class which students take.^^
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question