Solved

design issue

Posted on 2003-12-10
18
373 Views
Last Modified: 2013-12-03
Hi,

I have an object and I have written a mthod where I clear all the instance variables.

public void clear()
{
this.instanceVariabes = null;
}

I am doing this so that i do  not have to create multiple objects of my class but use the same class.
like this
a.make an object
b. use it
c. clear it
d use it again

instead of
a.make an object
b. use it
c.make another object
d. use it

1. Is it a good design?
2. will i save on instance variable memory.

Anything else pros and cons....

Thx for your help
0
Comment
Question by:applekanna
  • 4
  • 4
  • 3
  • +4
18 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9918226
hard to say without knowing how you are going to use it (and why)
you're (probably) going to save on memory, but you need to ensure that the object isn't being referenced elsewhere when you clear it.
0
 
LVL 3

Author Comment

by:applekanna
ID: 9918244
was just looking for what i should be careful of when do such a thing.
0
 
LVL 92

Expert Comment

by:objects
ID: 9918287
clearing it while its still referenced.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 15

Expert Comment

by:Javatm
ID: 9918291
>> 1. Is it a good design?

      It actually depends on the type of program your doing or creating. There are so many ways you
      can make a design but the design should be handled professionally, look on the samples of the
      designs of some programs which is the same as what your doing for you to decide if what your
      doing is appropriate for the program.

>> 2. will i save on instance variable memory.

     This will happen because your targeting to lose an object on its referrence. Just follow on the
     comment of what objects said about it.

Hope its helps . . .
Javatm
0
 
LVL 3

Expert Comment

by:ShawnCurry
ID: 9918393
1.  If you want only one copy of a variable, declare it as static.
2.  Will it save on memory? It depends how it is used.  If it's something that should be created once and referenced many times, you probably don't want to set it to null - at least put a nice comment block above the line you set it to null... so you can find it quickly if you start getting null pointers...
0
 
LVL 3

Expert Comment

by:ShawnCurry
ID: 9918422
It won't let you declare an entire class as being static, but there's nothing stopping you from declaring every single thing inside static.  The only thing to watch out for here is thread safety..
0
 
LVL 2

Expert Comment

by:Tols
ID: 9918639
There is a nice design schema called singleton. It allows to make such class that there could be only one insctance (object) of this class during runtime.
Its something like :

public class SingletonClass
{
    private SingletonClas singleton = null;
   
    private SingletonClass() ///
   {
       //nitialization
   }
   
   public static SingletonClass getSingletonObject()
   {
        if (singleton==null) singleton = new SingletonClass();
        return singleton;
   }
}

If You wanto to recreate object You may provode reset method in class definition:
//....
   prublic static resetSingletonObject()
   {
        singleton = null;
   }
//....

It's good not to keep references to this object in other classes, use only getSingletonObject() method for referencing to the singleton object. This will make all object would referrence always to current instance.
0
 
LVL 2

Expert Comment

by:Tols
ID: 9918641
correnction - singleton have to be static

private static SingletonClass singleton = null;
0
 
LVL 3

Expert Comment

by:ShawnCurry
ID: 9918665
The reason your singleton needs to be static is because you reference it from a static context "public static SingletonClass getSingletonObject()"
0
 
LVL 2

Expert Comment

by:Tols
ID: 9918697
Yes, yes - it was a slip of the tongue
:-)
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9918977
If you want an example of using a single Object, look into the java.awt.GridBagConstraints object.  The recommended approach to using this is to create a single instance of it, then set the values you require on it.  When it is then passed to the layout manager it is cloned and you can modify your created one further without it affecting the ones that will be used by the layout manager.  Perhaps it's something for you to think about anyway!
0
 
LVL 3

Author Comment

by:applekanna
ID: 9922056
Thx for you help
Actually I have a bean which is basically a record of a database table and and when displaying it on the page
i get all the records as follows

for(//while there are more records in iterator sometimes 200 )
{
  MyBean temp = new MyBean();
  //get my values
  temp = null;
}

But I was told on EE that temp = nul does not help in GC so I thought I will use a clear() method here
and i am not sure if singleton will help
0
 
LVL 3

Expert Comment

by:ShawnCurry
ID: 9922710
The best thing you can do in that case is consult the documention on the bean.  If there is some sort of set() method to set the values you need, you could call the constructor once before you enter the loop.. since you want to recycle the same object, it doesn't make sense to call the constructor (and a clear() method) inside the loop.  Everything I've read on the GC indicates that it runs whenever it feels like it..  Setting the variable to null can help GC clean up, but there's no way to guarantee the memory will be freed (before your program exits, anyway).  The best way to guarantee you don't have irrelevant objects floating around in memory is to never create them in the first place!!!

Hope that helps...

Shawn
0
 
LVL 3

Author Comment

by:applekanna
ID: 9923879
WHat happens  in this case


for(//while there are more records in iterator sometimes 200 )
{
  MyBean temp = (MyBean) iterator.next();
  //get my values
  temp = null;
}

Here you see I am not using any constructor
Thx for ur help
0
 
LVL 92

Expert Comment

by:objects
ID: 9923908
> It allows to make such class that there could be only
> one insctance (object) of this class during runtime.

Not entirely true. There is one static per classloader.

0
 
LVL 92

Expert Comment

by:objects
ID: 9923920
> But I was told on EE that temp = nul does not help in GC

Its not that it doesn't help, its that it is unnecessary.

I don't ythink there is anything to get concerned about, or any design changes needed.
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9925341
I do not get it.
after clearing that instance variable I assume you then proced to give it a value again (by creating an object for it to reference)

So all you have really done is to create a level of indirection between your program and that object. It will make litte or no difference in memory use. It may slow down things a bit, but again the difference will be miniscule.

regards JakobA
0
 
LVL 7

Accepted Solution

by:
grim_toaster earned 125 total points
ID: 9926521
Here's an interesting link about GC, have a look at the section on invisible references.

http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#998394

But in your code:

for() {
  MyBean temp = (MyBean) iterator.next();
  temp = null;
}

The object already exists in memory, so setting your reference to null will be of no benefit (the object will still be referenced by whatever the Iterator is iterating over, i.e. a collection, so GC would not clean it up)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript/Java - Changing an image background color 4 66
How are Button Fonts Created? 2 21
hibernate insert example 13 28
Java: anonymous class 4 19
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

773 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