B4J Question How to Decrease Memory Consumption using HugeImageView

xulihang

Well-Known Member
Licensed User
Longtime User
Solution
I updated HugeImageView to use ImageReader to read a sub region of images. The memory usage is decreased from 4000MB to 2400MB. @Erel Could this improvement be added to the library?

Swissmade

Well-Known Member
Licensed User
Longtime User
Why not playing with this parameter starting Java?

-Xms128m -Xmx256m
Also you can check this with VisualVM
 
Upvote 0

xulihang

Well-Known Member
Licensed User
Longtime User
If you set the max memory too low, it won't be able to load the image.

B4X:
PS C:\Users\HP\Downloads\HugeImageView\B4J\Objects> java -Xmx512m  -jar .\HugeImageView.jar
Image size: 0x0
b4xpagesmanager._createpageifneeded (java line: 310)
java.lang.RuntimeException: java.lang.IllegalArgumentException: src cannot be null
        at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:522)
        at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:468)
        at b4j.example.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:310)
        at b4j.example.b4xpagesmanager._showpage(b4xpagesmanager.java:725)
        at b4j.example.b4xpagesmanager._addpage(b4xpagesmanager.java:116)
        at b4j.example.b4xpagesmanager._addpageandcreate(b4xpagesmanager.java:123)
        at b4j.example.b4xpagesmanager._initialize(b4xpagesmanager.java:494)
        at b4j.example.main._appstart(main.java:61)
 
Last edited:
Upvote 0

PaulMeuris

Well-Known Member
Licensed User
The idea is to reduce the Java heap size and let the garbage collector (GC) do its job.
With this statement in the code the memory consumption is managed better than without it:
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
    #VirtualMachineArgs: -Xms2G -Xmx5G -XX:MinHeapFreeRatio=40 -XX:MaxHeapFreeRatio=70
#End Region
I have tested this with the HugeImageView example and the file you pointed to.
Maybe there is a Java specialist reading the forum that knows how to optimize the code further...
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I tried with your image in a basic image view zooming in on a point at 12000,6000 showing an area of 1000 x 1000 pixels. I am using 523MB of memory.
 
Upvote 0

xulihang

Well-Known Member
Licensed User
Longtime User
1766408379189.png


I think to fully load the image (24000x12000), this size of memory is needed. Maybe we can use javax.imageio.ImageReader to only read a sub region of the image instead of loading the entire image.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The code if you wanted to experiment (moving the viewport is trivial)
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
    '#JavaCompilerPath: 26, D:\jdk26\bin\javac.exe
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Private ImageView1 As ImageView
    Private img As Image
    Private rect As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    img.Initialize("C:/temp","Whole_world_-_land_and_oceans_12000.jpg")
Log(img.Width & " x " & img.Height)
    rect.InitializeNewInstance("javafx.geometry.Rectangle2D",Array(12000.0,6000.0,1000.0,1000.0))
    ImageView1.SetImage(img)
    ImageView1.As(JavaObject).RunMethod("setViewport",Array(rect))
   
End Sub

Sub Button1_Click
    ' move viewport and tell imageview to use it
    rect.InitializeNewInstance("javafx.geometry.Rectangle2D",Array(12500.0,6000.0,1000.0,1000.0))
    ImageView1.As(JavaObject).RunMethod("setViewport",Array(rect))
End Sub
 
Last edited:
Upvote 0

xulihang

Well-Known Member
Licensed User
Longtime User
Upvote 0

xulihang

Well-Known Member
Licensed User
Longtime User
I updated HugeImageView to use ImageReader to read a sub region of images. The memory usage is decreased from 4000MB to 2400MB. @Erel Could this improvement be added to the library?
 

Attachments

  • HugeImageView.zip
    324.8 KB · Views: 29
Upvote 0
Solution

xulihang

Well-Known Member
Licensed User
Longtime User
Last edited:
Upvote 0
Top