Java socket performance bottleneck: where?

后端 未结 4 1714
面向向阳花
面向向阳花 2020-12-15 12:04

I recently started the development of an application making intensive usage of networking. A first attempt was made using RMI and for a couple of reasons, we switched over

4条回答
  •  [愿得一人]
    2020-12-15 12:51

    The problem that you're running into isn't low throughput with sockets; it's slow default Java serialization.

    When you're sending the same object over and over, it's only really being serialized once, and then a reference to it is being sent each time; this explains why it goes so quickly.

    When you're creating a new object every time, that new object has to be serialized using the relatively slow Java serialization mechanism, which is also probably much more complex than what you need.

    What you can do to improve this is either implement custom serialization code for your class, or make your own object serialization protocol that is external to the class (and use DataOutput instead of ObjectOutputStream).

    This article has a lot of good info, even if it is somewhat dated: http://java.sun.com/developer/technicalArticles/Programming/serialization/ See the last part on Performance Considerations

提交回复
热议问题