Skip to content

Fix memleak in signal package #1114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2018
Merged

Fix memleak in signal package #1114

merged 2 commits into from
Jul 26, 2018

Conversation

divan
Copy link
Contributor

@divan divan commented Jul 24, 2018

We were investigating memory leak in a server instance of statusd, and here is a brief explanation of findings:
#1067 (comment)

TLDR:
a) we call signals even when running as a server
b) we historically never free memory allocated by C.CString.

I believe this fix should stop memory from growing (or at least notable decrease growth).

Closes #1067

@divan divan changed the title Fix memleak in Cgo code Fix memleak in signal package Jul 24, 2018
This was referenced Jul 24, 2018
Copy link
Contributor

@adambabik adambabik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!


str := C.CString(string(data))
C.StatusServiceSignalEvent(str)
C.free(unsafe.Pointer(str))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you verified that we don't cause any invalid memory reads on Android and iOS? For example, in Android, C.StatusServiceSignalEvent we do if (javaJsonEvent != NULL) (*env)->DeleteLocalRef(env, javaJsonEvent);.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adambabik no, I haven't. But as I understand, CCstring always make a copy and responsibility to free its memory is on the caller.

javaJsonEvent is a separate chunk of memory so it's ok that it is deleted there:

    jstring javaJsonEvent = NULL;
    if (jsonEvent != NULL) {
        javaJsonEvent = (*env)->NewStringUTF(env, jsonEvent);
    }

Copy link
Member

@gravityblast gravityblast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job!

@divan divan self-assigned this Jul 25, 2018
@divan divan added the blocked label Jul 25, 2018
@divan
Copy link
Contributor Author

divan commented Jul 25, 2018

I'll test it on a real server built from the branch before merging.

@divan
Copy link
Contributor Author

divan commented Jul 26, 2018

Update after running fix for ~20 hours. Memory increased from 17MB to 25MB (vs 17MB to 109MB for the same period before the fix).
I'm still not happy with CPU increase (which might be totally unrelated), so going to merge this..

screen shot 2018-07-26 at 12 29 47

@divan divan removed the blocked label Jul 26, 2018
@divan divan merged commit 60249e4 into develop Jul 26, 2018
@adambabik adambabik deleted the memleak_fix branch December 27, 2019 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory Leak in statusd
4 participants