duanlu6268 2016-04-26 09:33
浏览 26
已采纳

如何减少[]字符串的内存使用量?

Go is pretty new to me and i have some troubles understanding the memory usage :

I want to load a file similar to csv into an array of rows, each row being a struct composed of a key on 22 char and an array of values (string).
My code look like this : https://play.golang.org/p/hJ4SHjVXaG

Problem is that for a file of 450M it uses around 2G1 of memory.
Does anyone have a solution to reduce that memory use ?

Update using SirDarius solution : https://play.golang.org/p/DBmOFOkZdx still use around 1G9

  • 写回答

4条回答 默认 最新

  • dtftao7249656 2016-04-26 09:50
    关注

    How many lines and fields are there in the file?

    It is plausible that what you are describing is using the minimum amount of memory.

    Looking at the code I think it will use 450MB of memory for the underlying string data.

    It will then slice that up into strings. These consist of a pointer and a length which take 16 bytes on a 64 bit platform.

    So 1.5GB/16 = 93Million.

    So if there are >50 Million fields in your file then the memory use seems reasonable.

    There are other overheads like number of rows etc so this isn't an exact calculation.

    EDIT

    Given
    5 millions row, 10 column each

    That is 50 million string headers of 16 bytes which will take 800MB. Plus the data itself 450MB, plus 5 * 8 * 5 million Rows = 200MB makes 1.45GB

    So I don't think even with perfect memory allocation, you'll be able to reduce the usage below 1.5GB.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算