Possible Memory Leak?

Post here your questions about the Java client / Android API for SFS2X

Moderators: Lapo, Bax

Post Reply
ifernandez
Posts: 21
Joined: 16 Jul 2010, 17:02

Possible Memory Leak?

Post by ifernandez »

Hello,

I'm using the JAVA b3 client, and i'm having issues with the clean up of the app.

I'm creating a standalone, sfs client that will run as a system daemon, this process needs to be alive for a long time, not only while is connected to the server.

My implementation handles some sort of "on demand" connection to the server, the process opens a connection to the server when is needed and closes the connection after that, then moves to a waiting state until a new connection is needed.

After some testing everything is working fine, but when i try to shut down the process, two threads stay in memory and prevent the process to finish completely.

I have checked my code and found that those 2 threads are created when a connection to SFS is made, and a Event is received by the client.

I'm using the following code to clean up the SmartFox instance and perform a disconnection, perhaps an incorrect clean up?

Code: Select all

sfs.removeAllEventListeners();
sfs.disconnect();
I looked at the docs but i can't find there any example of another disconnection method, or a correct dispose of the objects.

Ivan
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

The way you are doing it seems ok.
Are you reusing the same SmartFox object each time? Or do you create a new instance on each attempt?

BTW... opening/closing connections continuously to the server will defeat the purpose of using sockets which provide the fastest way of communication. Are you sure you really need this approach?
Keeping a long running connection to the server is no big issue, actually that's what SFS is designed for ;)
Lapo
--
gotoAndPlay()
...addicted to flash games
ifernandez
Posts: 21
Joined: 16 Jul 2010, 17:02

Post by ifernandez »

I reuse the same object always, i don't create new instances of SFS each time i need a connection.

I have a "flag" that checks the state of SFS when a connection is needed, if the server is already connected, i use it right away, if the server is not connected i create a instance if its the first time, or re-connect in case an instance already exists.

The work load the service will handle, is widely separated at this point of time, maybe once a day or so, so i thought that keeping a connection active, was overwhelming and unnecessary. That is why i designed in a way that if the load is high a connection is always open (when the time between the "requests" are less than the time for client disconnect, and i modified the default disconnect time in that case to be longer than default)


That was my rationale behind the current implementation, if you advice against it, i will be glad to discuss another approach,and modify the service if necessary.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Sure! If that's the frequency of the connection then it makes a lot of sense to do that way.
I will forward this post to Thomas so he can take a look and see if there's some bug in the internal state of the API that leaks objects when reused.

Anyways try creating a new instance of SmartFox and see if it the GC eventually gets rid of the old ones.
Of course with such a low object creation rate it might take ages before the GC kicks in and kills those old instances.

We'll take a look and let you know
Lapo
--
gotoAndPlay()
...addicted to flash games
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

Will take a look - cant remember if we have a static in there that might prevent garbage collection.

Will check and report back. Might take a few days

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
ifernandez
Posts: 21
Joined: 16 Jul 2010, 17:02

Follow Up

Post by ifernandez »

As a follow up in my previous question, I'm doing some testing, and i have strange behavior.

I developed my program using Eclipse in Windows, and so far it works just fine (if the possible leak is ignored)

But the deployment server runs linux.

Both are x86_64 platforms.

When i run the client in windows it works ok, and everything behave as expected, BUT when running in linux a strange error appears. And one event in the chain is not raised.

Ill post a log from both consoles to show the issue:

The log in the windows box is as follows:

Code: Select all

11:23:46,163 INFO  [ActiveMQ Session Task-1] sfsInterface.SFSManager     - Attempting Connection to Smartfox Server




11:23:47,465 INFO  [New I/O client worker #1-1] core.EventDispatcher     - Dispatching event OnConnect to 1 listeners
11:23:47,466 INFO  [New I/O client worker #1-1] core.EventDispatcher     - Dispatching event connect to 1 listeners
11:23:47,842 INFO  [New I/O client worker #1-1] core.SFSProtocolCodec     - Writing message Binary size: 36
12 00 03 00 03 62 69 6E 01 01 00 02 63 6C 08 00 	.....bin....cl..
06 4A 61 76 61 3A 3A 00 03 61 70 69 08 00 05 30 	.Java::..api...0
2E 39 2E 38                                     	.9.8            

11:23:47,853 INFO  [New I/O client worker #1-1] core.SFSIOHandler     - Data written: Binary size: 53
12 00 03 00 01 61 03 00 00 00 01 63 02 00 00 01 	.....a.....c....
70 12 00 03 00 03 62 69 6E 01 01 00 02 63 6C 08 	p.....bin....cl.
00 06 4A 61 76 61 3A 3A 00 03 61 70 69 08 00 05 	..Java::..api...
30 2E 39 2E 38                                  	0.9.8           

11:23:48,191 INFO  [New I/O client worker #1-1] core.EventDispatcher     - Dispatching event OnData to 1 listeners
11:23:48,191 INFO  [New I/O client worker #1-1] core.SFSIOHandler     - Data Read: Binary size: 80
80 00 4D 12 00 03 00 01 61 03 00 00 00 01 63 02 	..M.....a.....c.
00 00 01 70 12 00 03 00 02 74 6B 08 00 20 38 38 	...p.....tk...88
65 62 38 32 37 34 37 34 30 39 64 35 65 63 36 39 	eb82747409d5ec69
35 66 36 65 39 66 64 66 64 30 38 31 63 37 00 02 	5f6e9fdfd081c7..
63 74 04 00 00 04 00 00 02 6D 73 04 00 7A 12 00 	ct.......ms..z..

11:23:48,191 INFO  [New I/O client worker #1-1] core.SFSIOHandler     - Handling New Packet of size 80
11:23:48,192 INFO  [New I/O client worker #1-1] core.SFSIOHandler     - Handling Header Size. Length: 79 (small)
11:23:48,192 INFO  [New I/O client worker #1-1] core.SFSIOHandler     - Data size is 77
11:23:48,192 INFO  [New I/O client worker #1-1] core.SFSIOHandler     - Handling Data: 77, previous state: 0/77
11:23:48,193 INFO  [New I/O client worker #1-1] core.SFSIOHandler     - <<< Packet Complete >>>
11:23:48,194 INFO  [New I/O client worker #1-1] controllers.SystemController     - Message: ResHandshake { Message id: 0 }
{ Dump: }

	(utf_string) tk: 88eb82747409d5ec695f6e9fdfd081c7
	(int) ct: 1024
	(int) ms: 8000000
	

11:23:48,197 INFO  [New I/O client worker #1-1] client.SmartFox     - Handshake response: tk => {0}, ct => {1}
11:23:48,197 INFO  [New I/O client worker #1-1] core.EventDispatcher     - Dispatching event connection to 1 listeners



11:23:48,198 INFO  [New I/O client worker #1-1] eventHandlers.SFSEventHandler     - Connected to Server. Now sending login request.

The console output from the linux box

Code: Select all

19 Sep 2011 12:38:51,326 INFO  [ActiveMQ Session Task-1] blg.sfsInterface.SFSManager     - Attempting Connection to Smartfox Server



19 Sep 2011 12:38:51,366 INFO  [New I/O client worker #1-1] client.core.EventDispatcher     - Dispatching event OnConnect to 1 listeners
19 Sep 2011 12:38:51,367 INFO  [New I/O client worker #1-1] client.core.EventDispatcher     - Dispatching event connect to 1 listeners
19 Sep 2011 12:38:51,376 INFO  [New I/O client worker #1-1] client.core.SFSProtocolCodec     - Writing message Binary size: 36
12 00 03 00 03 62 69 6E 01 01 00 02 63 6C 08 00         .....bin....cl..
06 4A 61 76 61 3A 3A 00 03 61 70 69 08 00 05 30         .Java::..api...0
2E 39 2E 38                                             .9.8

19 Sep 2011 12:38:51,386 INFO  [New I/O client worker #1-1] client.core.SFSIOHandler     - Data written: Binary size: 53
12 00 03 00 01 61 03 00 00 00 01 63 02 00 00 01         .....a.....c....
70 12 00 03 00 03 62 69 6E 01 01 00 02 63 6C 08         p.....bin....cl.
00 06 4A 61 76 61 3A 3A 00 03 61 70 69 08 00 05         ..Java::..api...
30 2E 39 2E 38                                          0.9.8

19 Sep 2011 12:38:51,388 INFO  [New I/O client worker #1-1] client.core.EventDispatcher     - Dispatching event OnData to 1 listeners
19 Sep 2011 12:38:51,388 INFO  [New I/O client worker #1-1] client.core.SFSIOHandler     - Data Read: Binary size: 80
80 00 4D 12 00 03 00 01 61 03 00 00 00 01 63 02         ..M.....a.....c.
00 00 01 70 12 00 03 00 02 74 6B 08 00 20 66 35         ...p.....tk...f5
37 34 62 31 31 35 36 66 64 61 37 34 66 61 30 65         74b1156fda74fa0e
36 61 65 30 32 33 63 62 64 32 64 61 31 66 00 02         6ae023cbd2da1f..
63 74 04 00 00 04 00 00 02 6D 73 04 00 7A 12 00         ct.......ms..z..

19 Sep 2011 12:38:51,389 INFO  [New I/O client worker #1-1] client.core.SFSIOHandler     - Handling New Packet of size 80
19 Sep 2011 12:38:51,389 INFO  [New I/O client worker #1-1] client.core.SFSIOHandler     - Handling Header Size. Length: 79 (small)
19 Sep 2011 12:38:51,389 INFO  [New I/O client worker #1-1] client.core.SFSIOHandler     - Data size is 77
19 Sep 2011 12:38:51,389 INFO  [New I/O client worker #1-1] client.core.SFSIOHandler     - Handling Data: 77, previous state: 0/77
19 Sep 2011 12:38:51,390 INFO  [New I/O client worker #1-1] client.core.SFSIOHandler     - <<< Packet Complete >>>
19 Sep 2011 12:38:51,391 INFO  [New I/O client worker #1-1] client.controllers.SystemController     - Message: ResHandshake { Message id: 0 }
{¬â Dump: }

        (utf_string) tk: f574b1156fda74fa0e6ae023cbd2da1f
        (int) ct: 1024
        (int) ms: 8000000


19 Sep 2011 12:38:51,391 WARN  [New I/O client worker #1-1] client.controllers.SystemController     - No handler exists for this event: 0
19 Sep 2011 12:39:20,923 INFO  [New I/O client worker #1-1] client.core.EventDispatcher     - Dispatching event OnDisconnect to 1 listeners
19 Sep 2011 12:39:20,923 INFO  [New I/O client worker #1-1] client.core.EventDispatcher     - Dispatching event disconnect to 1 listeners
19 Sep 2011 12:39:20,923 INFO  [New I/O client worker #1-1] client.core.EventDispatcher     - Dispatching event connectionLost to 1 listeners
19 Sep 2011 12:39:20,923 INFO  [New I/O client worker #1-1] sfsInterface.eventHandlers.SFSEventHandler     - Disconnected from SmartFox Server, Connection Dropped

The bold logs are from my classes.

As you can see in the linux box the response from the server is not properly handled, and my class never receives the event to continue the process and the connection is dropped.

The line missing from the Linux run is the one marked

Code: Select all

client.SmartFox     - Handshake response: tk => {0}, ct => {1} 

And the process stalls and never continues, this is similar to the issue in the C#/Unity API some threads talk about.

The Server itself is running in Linux (the same box i was testing the client in).

Please some help with this.

Ivan
ifernandez
Posts: 21
Joined: 16 Jul 2010, 17:02

Post by ifernandez »

Also,

Any news about the previous issue?

Ivan
Sarevok
Posts: 75
Joined: 12 Apr 2011, 22:12

Post by Sarevok »

There is exact same issue running C# client on Windows 7 64 bit. I run in parallel two clients (one is AS3, and other is C#). AS3 works fine, while C# client hangs during handshake, the same way ifernandez already explained.

After client sends connection request to server, registered event listeners are never triggered.

p.s. I run latest version of server, RC3 patch, and I used client API from patch folder.

p.s.2 I just realized this is android section. I found this thread through google. Sorry for posting in wrong section, but issue is still there nevertheless :P

p.s.3 It was simple problem. You just have to turn off thread safety on your sfs instance, because it is turned on by default.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

ifernandez
Is the client also running on Linux?
What happens if you test with a Windows client pointing at the Linux64 server?
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply