Skip to content
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

Memory overflow while parsing json #1600

Open
GuangNian10000 opened this issue Jan 13, 2023 · 3 comments
Open

Memory overflow while parsing json #1600

GuangNian10000 opened this issue Jan 13, 2023 · 3 comments
Labels

Comments

@GuangNian10000
Copy link

Fatal Exception: java.lang.OutOfMemoryError Failed to allocate a 124606360 byte allocation with 50331648 free bytes and 80MB until OOM, target footprint 503225168, growth limit 536870912 com.squareup.moshi.CollectionJsonAdapter.fromJson

@JakeWharton
Copy link
Collaborator

Does your JSON contain a 124MiB string or something? I think an OOM here is a perfectly reasonable behavior, and it should be fully recoverable.

@GuangNian10000
Copy link
Author

Does your JSON contain a 124MiB string or something? I think an OOM here is a perfectly reasonable behavior, and it should be fully recoverable.

The json string is very short, less than 100 in length, and only contains strings. I cannot repeat this problem. It is an online bug, how should I fix it?

image

DefaultIfNullFactory.java

class DefaultIfNullFactory : JsonAdapter.Factory {
override fun create(
type: Type,
annotations: MutableSet,
moshi: Moshi
): JsonAdapter<*>? {

    val delegate = moshi.nextAdapter<Any>(this, type, annotations)

    if (!annotations.isEmpty()) return null
    if (type === Boolean::class.javaPrimitiveType) return delegate
    if (type === Byte::class.javaPrimitiveType) return delegate
    if (type === Char::class.javaPrimitiveType) return delegate
    if (type === Double::class.javaPrimitiveType) return delegate
    if (type === Float::class.javaPrimitiveType) return delegate
    if (type === Int::class.javaPrimitiveType) return delegate
    if (type === Long::class.javaPrimitiveType) return delegate
    if (type === Short::class.javaPrimitiveType) return delegate
    if (type === Boolean::class.java) return delegate
    if (type === Byte::class.java) return delegate
    if (type === Char::class.java) return delegate
    if (type === Double::class.java) return delegate
    if (type === Float::class.java) return delegate
    if (type === Int::class.java) return delegate
    if (type === Long::class.java) return delegate
    if (type === Short::class.java) return delegate
    if (type === String::class.java) return delegate
  //  if (type === ArrayMap::class.java) return delegate

    return object : JsonAdapter<Any>() {

        override fun fromJson(reader: JsonReader): Any? {

            try {
                return if (reader.peek() == JsonReader.Token.BEGIN_OBJECT) {
                    delegate.fromJson(JsonReaderSkipNullValuesWrapper(reader))
                } else {
                    delegate.fromJson(reader)
                }
            }catch (e:Exception){}

            return null
        }

        override fun toJson(writer: JsonWriter, value: Any?) {
            return delegate.toJson(writer, value)
        }
    }
}

}

@JakeWharton
Copy link
Collaborator

It looks like the heap is wildly fragmented, but Moshi is trying to append an item to a list that it's creating which is growing in size and instantiating a 124MiB backing array. That indicates a huge JSON payload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants