Skip to content

Commit

Permalink
fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
icodening committed Jun 29, 2022
1 parent c1a76a0 commit 6c12d55
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public void reset() {

@Override
public void close() throws IOException {
this.mData = new byte[0];
this.mPosition = 0;
this.mLimit = 0;
this.mMark = 0;
}

public int position() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,7 @@ public String toString(String charset) throws UnsupportedEncodingException {

@Override
public void close() throws IOException {
this.mBuffer = new byte[0];
this.mCount = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.dubbo.common.serialize.hessian2;

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.serialize.Cleanable;
import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.serialize.hessian2.dubbo.Hessian2FactoryInitializer;
Expand All @@ -31,6 +33,8 @@
*/
public class Hessian2ObjectInput implements ObjectInput, Cleanable {

private static final Logger LOGGER = LoggerFactory.getLogger(Hessian2ObjectInput.class);

private static ThreadLocal<Hessian2Input> INPUT_TL = ThreadLocal.withInitial(() -> {
Hessian2Input h2i = new Hessian2Input(null);
h2i.setSerializerFactory(Hessian2FactoryInitializer.getInstance().getSerializerFactory());
Expand Down Expand Up @@ -115,6 +119,11 @@ public InputStream readInputStream() throws IOException {
public void cleanup() {
if(mH2i != null) {
mH2i.reset();
try {
mH2i.close();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.dubbo.common.serialize.hessian2;

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.serialize.Cleanable;
import org.apache.dubbo.common.serialize.ObjectOutput;
import org.apache.dubbo.common.serialize.hessian2.dubbo.Hessian2FactoryInitializer;
Expand All @@ -30,6 +32,8 @@
*/
public class Hessian2ObjectOutput implements ObjectOutput, Cleanable {

private static final Logger LOGGER = LoggerFactory.getLogger(Hessian2ObjectOutput.class);

private static ThreadLocal<Hessian2Output> OUTPUT_TL = ThreadLocal.withInitial(() -> {
Hessian2Output h2o = new Hessian2Output(null);
h2o.setSerializerFactory(Hessian2FactoryInitializer.getInstance().getSerializerFactory());
Expand Down Expand Up @@ -112,6 +116,11 @@ public OutputStream getOutputStream() throws IOException {
public void cleanup() {
if(mH2o != null) {
mH2o.reset();
try {
mH2o.close();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
}

0 comments on commit 6c12d55

Please sign in to comment.