hive
hive copied to clipboard
Unhandled Exception: Out of Memory
StackTrace
E/flutter ( 8556): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Out of Memory
E/flutter ( 8556): #0 new Uint8List (dart:typed_data-patch/typed_data_patch.dart:2221:3)
E/flutter ( 8556): #1 BufferedFileReader.loadBytes (package:hive/src/io/buffered_file_reader.dart:73:18)
E/flutter ( 8556): #2 _KeyReader._load (package:hive/src/io/frame_io_helper.dart:89:40)
E/flutter ( 8556): #3 _KeyReader.readKeys (package:hive/src/io/frame_io_helper.dart:69:31)
E/flutter ( 8556): #4 _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter ( 8556): #5 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 8556): #6 _FutureListener.handleValue (dart:async/future_impl.dart:159:18)
E/flutter ( 8556): #7 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:766:45)
E/flutter ( 8556): #8 Future._propagateToListeners (dart:async/future_impl.dart:795:13)
E/flutter ( 8556): #9 Future._completeWithValue (dart:async/future_impl.dart:601:5)
E/flutter ( 8556): #10 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:262:13)
E/flutter ( 8556): #11 _KeyReader._load (package:hive/src/io/frame_io_helper.dart)
E/flutter ( 8556): #12 _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter ( 8556): #13 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 8556): #14 _FutureListener.handleValue (dart:async/future_impl.dart:159:18)
E/flutter ( 8556): #15 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:766:45)
E/flutter ( 8556): #16 Future._propagateToListeners (dart:async/future_impl.dart:795:13)
E/flutter ( 8556): #17 Future._completeWithValue (dart:async/future_impl.dart:601:5)
E/flutter ( 8556): #18 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:262:13)
E/flutter ( 8556): #19 BufferedFileReader.loadBytes (package:hive/src/io/buffered_file_reader.dart)
E/flutter ( 8556): #20 _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter ( 8556): #21 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 8556): #22 _FutureListener.handleValue (dart:async/future_impl.dart:159:18)
E/flutter ( 8556): #23 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:766:45)
E/flutter ( 8556): #24 Future._propagateToListeners (dart:async/future_impl.dart:795:13)
E/flutter ( 8556): #25 Future._completeWithValue (dart:async/future_impl.dart:601:5)
E/flutter ( 8556): #26 Future._asyncCompleteWithValue.
Sometime after put random string data, the error start trigger then will be never can be open/read and delete after that Version [✓] Flutter (Channel stable, 2.10.5, on macOS 13.0 22A380 darwin-arm, locale en-MY) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
Any solution for this issue?
@Bwolfs2 @zainozzaini
I've also encountered this today. After a bit of debugging, it seems that when trying to read a list in BinaryReaderImpl, it tries to allocate a new list with size 2433417361 (in my case), which obviously throws an OutOfMemory error. However, I have not yet found a solution to this problem.
UPDATE: After more debugging, I found that this might have to do with the generation of type adapters for empty classes. If you have a class like this
@HiveType(typeId: 123)
class MyClass { }
the generated type adapter will look like this:
class MyClassAdapter extends TypeAdapter<MyClass> {
@override
final int typeId = 123;
@override
MyClass read(BinaryReader reader) {
return MyClass();
}
@override
void write(BinaryWriter writer, MyClass obj) {
writer.writeByte(0);
}
// Some hashCode and == overrides
}
Now, I guess that the problem is in the generated read method. This is how I interpret it: The writer writes a byte (0, the length of the bytes to come), and the reader does not consume this byte, which causes the next reads of other objects to fail since the byte offset is now wrong. This is what leads to absurd numbers like 2433417361.
From my understanding, the generated code should look like this:
@override
MyClass read(BinaryReader reader) {
reader.readByte(); //<-- Here
return MyClass();
}
Therefore this seems to be a problem with hive_generator and not hive itself.
Would be great if someone could confirm this and see if this fixes your issue. If this is the case, I would recommend this issue to be moved to hive_generator (it there is a separate repository for this).
Having similar issue when using Hive as graphql store:
Fatal Exception Out of Memory
0 ??? 0x0 new Map._fromLiteral (dart:core)
1 ??? 0x0 BinaryReaderImpl.readMap (binary_reader_impl.dart)
2 ??? 0x0 BinaryReaderImpl.read (binary_reader_impl.dart)
3 ??? 0x0 BinaryReaderImpl.readList (binary_reader_impl.dart)
4 ??? 0x0 BinaryReaderImpl.read (binary_reader_impl.dart)
5 ??? 0x0 BinaryReaderImpl.readMap (binary_reader_impl.dart)
6 ??? 0x0 BinaryReaderImpl.read (binary_reader_impl.dart)
7 ??? 0x0 BinaryReaderImpl.readFrame (binary_reader_impl.dart)
8 ??? 0x0 FrameHelper.framesFromBytes (frame_helper.dart)
9 ??? 0x0 FrameIoHelper.framesFromFile (frame_io_helper.dart)
#7 BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart)
#6 BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart)
#5 BinaryReaderImpl.readMap (package:hive/src/binary/binary_reader_impl.dart)
#4 BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart)
#3 BinaryReaderImpl.readList (package:hive/src/binary/binary_reader_impl.dart)
#2 BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart)
#1 BinaryReaderImpl.readMap (package:hive/src/binary/binary_reader_impl.dart)
#0 new Map._fromLiteral (dart:core-patch/map_patch.dart)
Another when using Hive as a graphql store
Crashed in non-app: compact_hash.dart in _LinkedHashMapMixin._init
compact_hash.dart in _LinkedHashMapMixin._rehash
compact_hash.dart in _LinkedHashMapMixin._insert
compact_hash.dart in _LinkedHashMapMixin._set
binary_reader_impl.dart in BinaryReaderImpl.readMap within hive
In App
binary_reader_impl.dart in BinaryReaderImpl.read within hive
In App
binary_reader_impl.dart in BinaryReaderImpl.readMap within hive
In App
binary_reader_impl.dart in BinaryReaderImpl.read within hive
In App
binary_reader_impl.dart in BinaryReaderImpl.readList within hive
In App
binary_reader_impl.dart in BinaryReaderImpl.read within hive
In App
binary_reader_impl.dart in BinaryReaderImpl.readMap within hive
In App
binary_reader_impl.dart in BinaryReaderImpl.read within hive
In App
binary_reader_impl.dart in BinaryReaderImpl.readList within hive
In App
Still having the same error, someone could fix it?
We see several users affected by this (or a very similar issue) with out Flutter app in production.
OutOfMemoryError at buffered_file_reader.dart in BufferedFileReader.loadBytes within hive
frame_io_helper.dart in _KeyReader._load within hive
frame_io_helper.dart in _KeyReader.readKeys within hive
Called from: async_patch.dart in _SuspendState._createAsyncStarCallback.<fn>
future_impl.dart in _FutureListener.handleValue
future_impl.dart in Future._propagateToListeners.handleValueCallback
future_impl.dart in Future._propagateToListeners
future_impl.dart in Future._completeWithValue
async_patch.dart in _SuspendState._createAsyncStarCallback.<fn>
future_impl.dart in _FutureListener.handleValue
future_impl.dart in Future._propagateToListeners.handleValueCallback
future_impl.dart in Future._propagateToListeners
future_impl.dart in Future._completeWithValue
async_patch.dart in _SuspendState._createAsyncStarCallback.<fn>
future_impl.dart in _FutureListener.handleValue
future_impl.dart in Future._propagateToListeners.handleValueCallback
future_impl.dart in Future._propagateToListeners
future_impl.dart in Future._completeWithValue
future_impl.dart in Future._asyncCompleteWithValue.<fn>
schedule_microtask.dart in _microtaskLoop
schedule_microtask.dart in _startMicrotaskLoop
Interestingly, according to our Sentry reports, this seems to only occur on Android. Here is a list of affected models: