B4J Question outofmemoryerror java heap space

Nokia

Active Member
Licensed User
Longtime User
I get this error when trying to encrypt a file over 100 mb or trying to encrypt to many files at the same time. Is there a way to tell the app to get or set a bigger heap space?

or could this be from a memory leak from the encryption?
 

Nokia

Active Member
Licensed User
Longtime User
I can see from task menu every time I encrypt a file it adds memory, but does not release it..
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
when I try to use the try and catch to get the message nothing happens. ... no message and the app still closes..
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
You cannot catch out of memory errors.

what would you normally do then?

I have put a size restriction on how big a file that can be encrypted but I have noticed.. if you don't have a full dedicated 1024 mb of ram that it will show the out of memory error on a smaller file. on my laptop with 3 gig ram I can run 250mb file but on desktop with 2 gig of ram. I can only do a 100 mb file.

is there a way in code to check available memory?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to get the reported max heap size:
B4X:
Sub MaxHeapSize As Long
   Dim jo As JavaObject
   Return jo.InitializeStatic("java.lang.Runtime").RunMethodJO("getRuntime", Null).RunMethod("maxMemory", Null)
End Sub
what would you normally do then?
You should avoid this situation. Recovering from out of memory errors is problematic.
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
You can use this code to get the reported max heap size:
B4X:
Sub MaxHeapSize As Long
   Dim jo As JavaObject
   Return jo.InitializeStatic("java.lang.Runtime").RunMethodJO("getRuntime", Null).RunMethod("maxMemory", Null)
End Sub

You should avoid this situation. Recovering from out of memory errors is problematic.


does this code give you the max heap size the app is using or does it give you the max that is available beside what the app is using?

or does it give you how much memory the computer is running?
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Is it possible to change the max mem size from the b4j application ? I checked the status and it is 256MB and I would like to increase it in a special case that needs more.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Thank you Daestrum, It solves the problem when running from the IDE but Erel writes there :
Note that #VirtualMachineArgs attribute only affects the program when you run it from the IDE.
I have checked and it is correct so it is not a solution for a user that gets the jar file.
I need a solution that will change the heap as part of the jar file code.
 
Upvote 0

Daestrum

Well-Known Member
Licensed User
Longtime User
If you find the setting removes the error, you can supply the same parameters when the user runs the jar.

If you start the jar from a batch file just add the same parameters as the VirtualMachineArgs.

I don't think you can change it from within a jar, as by that stage the JVM has been created.
 
Upvote 0
Top