OutofMemoryError

Hi,

I have written a JAVA server and a C client. depending on the request coming from client, the server will display pictures, videos or audio files. When i give client request in a loop, I am getting an OutOfMemoryError.

I have written a function that will close all audio or video windows if any are open ie JWindow.dispose()... (means the JWindow object.dispose()). But when run in a loop the windows are not getting disposed. Why is that?

Please help me.

Thank You.

[501 byte] By [javabb] at [2007-9-30 22:00:10]
# 1

U can try setting the JWindow object to null like,

JWindow w=new JWindow();

w=null;

Declaring an object as static may also cause the same problem. Also, u can try calling the garbage collector method at the loop like,

while(...)

{

........

........

........

System.gc(); // last line in the loop

}

Aswin at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 2

> U can try setting the JWindow object to null like,

>

> JWindow w=new JWindow();

> w=null;

>

If there is no reference then the gc will find it.

> System.gc(); // last line in the loop

The gc must run before an out of memory exception is thrown.

jschell at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 3

>

> Please help me.

I am not a gui person but generally you must explicitly dispose of listeners when your windows go away.

However given that you are loading large media formats I would suspect that it has nothing to do with your GUI but rather has something to do with the way you are loading the media types.

jschell at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 4

my declaration is static... my code is somthing like this...

class Global{

static VideoPlayer player;

}

class Main{

public static void main....

{

Main m = new M();

m.openAndPlayVideo();

}

openAndPlayVideo()

{

closeVideo();

..................................

//get filename

..................................

Global.player = new VideoPlayer();

player.playVideo(filename);

}

closeVideo()

{

if(Global.player != null)

{

Global.player.p.stop();

Global.player.dispose();

Global.player = null;

}

}

}//end of Main

class VideoPlayer extends JWindow

{

Player p;

public playVideo(String Filename)

{

p = Manager.createPlayer((new File(filename)).toURL())

ControllerListener listener = new ControllerAdapter() {

/*Listen the action*/

public void realizeComplete(RealizeCompleteEvent event) {

try {

/*define object of component class,load the component of

player, set the visual property*/

Component vc = CrtCommon.player.getVisualComponent();

........................................................

}};

p.addControllerListener(listener);

p.start();

}

}

javabb at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 5
One minor thing i can think of which may not directly be related to your problem is you are constructing your VideoPlayer everytime. Maybe move the constructor to the Global class?class Global{static VideoPlayer player = new VideoPlayer();}
bjon045 at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 6
Pretty sure it is because your windows are not getting cleaned up. Like Jschell said, listeners need to be taken care of, espically since your constructing a new VideoPlayer everytime.
bjon045 at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 7
But if I dont create a new VideoPlayer each time wont it cause an error as I am setting it to null after disposing? Or should I not set it to null?
javabb at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 8
No reason to set it to null. I would like to see the contents of the dispose method though.
bjon045 at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 9
dispose() is the inbuilt function of java Window which releases all allocates resources
javabb at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 10
dispose() is the inbuilt function of java Window which releases all allocates resources
javabb at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 11
dispose() is the inbuilt function of java Window which releases all allocates resources
javabb at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 12
Well your going to need to either overide it or do a lot more before you call dispose. Get rid of the listeners, close any streams etc.
bjon045 at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 13

What are the other scenarios that may cause a memory leak other than unremoved listeners and open stream? I am using several global variables like JLabel. I also have several unreferenced objects created as

System.out.println((new Date).toString());

Will this Date obj created cause any problems?

Thanks for the help

javabb at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...
# 14

> What are the other scenarios that may cause a memory

> leak other than unremoved listeners and open stream? I

> am using several global variables like JLabel.

Not being a GUI person I can't really say if that is a good idea or not, but it certainly seems suspect (given that I would suppose that that is only supposed to exist in a gui.)

>

> System.out.println((new Date).toString());

>

> Will this Date obj created cause any problems?

No. Once it leaves the scope of the call (or the stack frame index is reused) then the object will be available for collection.

jschell at 2007-7-7 3:27:39 > top of Java-index,Administration Tools,Sun Connection...