如何为c#进程分配ram [英] How to allocate ram for c# process

查看:105
本文介绍了如何为c#进程分配ram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI朋友,我有c#程序使用文本文件创建excel文件。

但是在较大的文本文件中,文件创建过程将运行很多小时。

我想我可以通过为该进程分配更多内存来解决这个问题。

我试过**堆和堆栈**方法。但我无法做到。

所以请帮助我的朋友。

HI friends, i have c# program that creates excel files using text files.
but in larger text files the file creating process will be run lot of hours.
i think i can solve that problem by allocating more memory to that process.
i tried **heap and stack** methods. but i couldn't make it.
so please help me friends.

推荐答案

好吧,你的评论显示代码标记为'已删除,但我仍然可以看到它,并且我现在可以告诉你,你应该使用StringBuilder来动态构建字符串,只在必要时将StringBuilder转换为'字符串。



建议你移动原始帖子的代码示例,然后使用CP的代码编辑器对其进行格式化。



另外:查看NuGet,搜索'Excel:[ ^ ]:我看到20个库,其中很多,我怀疑,可以提供代码文件读取和映射到Excel的方式类似于你正在做的事情。



我也想知道...看代码示例...如果你能在这里使用XML进行直接的非常快速的转换...但是,也许,你无法控制你使用的文件格式(?)。
Well, your comment showing code is marked 'Deleted, but I can still see it, and I can tell you right now that you should be using a StringBuilder to build strings on the fly, only converting the StringBuilder to 'string when necessary.

Suggest you move the code example to your original post, and then format it with CP's code editor.

Also: look on NuGet, searching for 'Excel: [^]: I see twenty libraries, many of which, I suspect, could provide code for file reading and mapping to Excel in ways similar to what you are doing.

I am also left wondering ... looking at the code sample ... if you could be using XML here for direct very-fast conversion ... but, perhaps, you have no control over the file format(s) you work with (?).


据我了解,你必须使用Win32API内存管理

函数,如VirtualAlloc,VirtualFree,在全局内核内存空间中分配缓冲区。



几年前,我也遇到了同样的问题,并且使用这些功能已经奏效。



既然你'在C#中重新编程你只需要将这些函数导入你的应用程序。



这是MSDN资源的链接 VirtualAlloc [ ^]



解释: 建议使用内存管理API(如VirtualAlloc函数)为内部数据结构分配缓冲区,这些内部数据结构用于存储应用程序正在处理的数据。如果您在应用程序的**堆或堆栈**上分配内存,通常会导致进程内存地址空间耗尽。实际上,* THIS *与MS Excel文件创建无关,可以通过使用Win32 COM API或MFC / C ++或C#/ .NET Framework支持的客户端COM包装器来完成(例如,在这种情况下,我们只需要'实际上需要分配任何缓冲区)。基本上,应用程序(最常见的场景)使用VirtualAlloc函数获取存储在全局内核内存上下文中分配的那些缓冲区中的数据,然后使用从这些应用程序的数据结构中检索的数据生成报告。同样,MS-Excel文件创建与内部数据结构缓冲区分配无关。重要提示:问题是在app的堆和堆栈中分配的多个缓冲区**会扩大正在消耗的进程内存量(!!!)。谢谢。等待你的评论。
As far as I understand, you have to use the Win32API memory management
functions such as VirtualAlloc, VirtualFree to allocate buffers in global kernel memory space.

A few years ago, I've also encountered the same problem and using of these functions had worked.

Since you're programming in C# you just have to import these functions into your application.

Here's a link to MSDN resources VirtualAlloc[^]

Explaination: It's recommended to use memory management API such as VirtualAlloc functions to allocate buffers for the internal data structures that are used to store the data being processed by an application. In case when you're allocating memory on the app's **heap or stack**, it normally causes to pump up the process memory address space. Actually, *THIS* has nothing to do with MS Excel file creation which can be acomplished by using Win32 COM API or client-side COM wrappers supported by either MFC/C++ or C#/.NET Framework (e.g. in this case we simply don't actually need to allocate any buffers). Basically, an application (the most common scenario) fetches the data stored in the those buffers allocated in the global kernel memory context using VirtualAlloc function and then generates reports using the data retrieved from those app's data structures. Again, MS-Excel file creation has nothing to do with internal data structures buffers allocation. Important: The problem is that the multiple buffers allocated in app's **heap and stack** enlarge the amounts of the process memory being consumed (!!!). Thanks. Waiting for your comment.


这篇关于如何为c#进程分配ram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆